Over a million developers have joined DZone.

PHP Performance: Bitwise Division

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Recently I wrote about binary search and then I said that in some languages, like PHP, bitwise division by two is not faster than the typical “/” operator. However I decided to make some experiments and here are the results.

Important Note

It’s very important to say that the following results are dependant from the machine and the environment!

Source Code

Here’s the PHP source code.

function divide($n = 1) 
	$a = microtime(true);
	for ($i = 0; $i < $n; $i++) {
	echo microtime(true) - $a;

and bitwise …

function bitwise($n = 1)
	$a = microtime(true);
	for ($i = 0; $i < $n; $i++) {
		300 >> 1;
	echo microtime(true) - $a;

Note that each method was called 6 times with the same parameter. This means that divide(100) was called 6 times and then I used the average value of these six times.


I said back then in my binary search post, that in PHP the bitwise operator “>> 1″ is not faster than the typical division with the “/” operator. However the results tells us that using bitwise division is slightly faster, as you can see at the diagram bellow.

n		">>"			"/"
100		0.0002334912618		0.000311803817749
1000		0.001911004384359	0.007335503896078
10000		0.013423800468445	0.039460102717081
100000		0.14417803287506	0.21413381894429
1000000		1.15839115778605	1.17152162392935
10000000	10.556711634		11.0911625623705
PHP Performance: Bitwise division is slightly faster!

Bitwise division is slightly faster!


Although bitwise division is a bit faster the difference is so small that you should work with very large data in order to gain some performance.


Source: http://www.stoimen.com/blog/2012/01/05/php-performance-bitwise-division/



Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}