Skip to main content

shift bits vs multiply in PHP



I have the following code:







<?php

$start = 1;



$timestart = microtime(1);

for ($i = 0; $i < 1000000; $i++) {

$result1 = $start * 4;

}

echo "\n";

echo microtime(1) - $timestart;

echo "\n";



$timestart = microtime(1);

for ($i = 0; $i < 1000000; $i++) {

$result2 = $start << 2;

}

echo "\n";

echo microtime(1) - $timestart;

echo "\n";







This outputs:







0.14027094841003



0.12061500549316







I found on the Internet a Google interview question (which I wanted to apply for a developer, but I realize I can't), and one of the questions asked what the fastest way was to multiply a number. My first thought was to use the * sign, so I tested it.





My question is, why is shifting bits faster than multiplication?


Comments

  1. Because bit shifting is something the computer does all the time in hardware, it's a no-brainer for the CPU. Multiplying arbitrary numbers is something more difficult, because it can't necessarily be done using simple bit shifting but requires actual work. Multiplying a small integer by 4 happens to be an operation that's identical to a left-shift by 2. But even if the compiler/runtime/CPU optimizes this operation down to a bit shift, some code first needs to recognize that it can be optimized this way, which is more work than a simple bit shift itself.

    Either way, it's simply more work because the two operations do entirely different things, even if the outcome of certain operations is the same.

    ReplyDelete
  2. Because a bit shift is an operation that can be implemented directly in hardware, whereas hardware rarely has multiplication operations implemented directly. Multiplication by a power of two can be achieved with a few simple logic gates, whereas multiplication by arbitrary multiplicands requires at the very least several multiplications by powers of two plus an add-to-self operation stacked on top of each other (5 = 2 * 2 + 1). I don't know if the PHP language specifically implements a shift operation by using whatever low-level calls are available, but I would be surprised if it doesn't.

    Source: years of experience + computer science education

    ReplyDelete
  3. On Intel sandybrigde CPUs it seems a shift with immediate costs about 1 clock cycle while a multiplication takes about 3-4 cycles. Apparently the whole program performance is affected by more factors than just the raw multiplication but it is enough of making a difference. Most compilers these days optimize multiplication by constants 2^n to shifts (compiler writers love to optimize your code :)) but maybe the PHP interpreter doesn't.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.