note 104031 deleted from language.operators.bitwise by danbrown

From: Date: Wed, 18 May 2011 14:43:43 +0000
Subject: note 104031 deleted from language.operators.bitwise by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-179852@lists.php.net to get a copy of this message
Note Submitter: phil ---- A general comment in response to Josh: reconsider using pow() when a simple power of 2 is needed, especially in cases where this operation must be done many times. Bit-shift operations are much faster than resorting to chained multiplication or floating-point operations, which is what I assume the pow() function does. To benchmark the differences, run this program: <?php $tick = microtime(true); for ($i = 0; $i < 1000000; ++$i) { for ($j = 0; $j < 64; ++$j) { $k = pow(2, $j); } } $tock = microtime(true); print "Time using pow: " . ($tock - $tick) . " seconds\n"; $tick = microtime(true); for ($i = 0; $i < 1000000; ++$i) { for ($j = 0; $j < 64; ++$j) { $k = (1 << $j); } } $tock = microtime(true); print "Time using <<: " . ($tock - $tick) . " seconds\n"; ?> On my system, I find that the bit-shift operator is about 4 times faster than pow() when calculating powers of 2.

« previous php.notes (#179852) next »