On Monday 20 May 2002 12:03, Martin Towell wrote:
> I was doing some timing of a function on the weekend and in one of the
> loops, I had to multiply a variable by 2 - easy enough - $i*2
>
> but then I remembered that in C, it's quicker to do a left shift - so $i<<1
>
> but this actually took longer to execute in php than the $i*2 - can anyone
> confirm my test? I iterated ~28,000 times. Each time, there was 2 places
> where I was doing the left shift. The time (including all the other stuff I
> was doing in the function) for $i*2 was ~12secs, and $i<<1 was ~19secs.
>
> I am running PHP4.0.6 (download cgi version from php.net) on a win98 system
> (500MHz Celeron if that changes anything)
My tests don't show much difference, shifting is slightly faster.
<?php
ini_set('max_execution_time', 600);
function gettime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$start=gettime();
$j = 24;
for($i = 1; $i <= 1000000; $i++) {
$p = $j * 2; #echo $p;
}
$end=gettime();
echo $end - $start, "\n";
$start=gettime();
$j = 24;
for($i = 1; $i <= 1000000; $i++) {
$p = $j << 1; #echo $p;
}
$end=gettime();
echo $end - $start, "\n";
?>
Results
=======
[jason@x27 jason]$ php -q doo.php
7.4377170801163
7.2966409921646
[jason@x27 jason]$ php -q doo.php
7.4303779602051
7.2926670312881
--
Jason Wong -> Gremlins Associates -> www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *
/*
When in doubt, tell the truth.
-- Mark Twain
*/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php