Author |
Message |
Raven
Site Admin/Owner
![](modules/Forums/images/avatars/45030c033f18773153cd2.gif)
Joined: Aug 27, 2002
Posts: 17088
|
Posted:
Mon Nov 24, 2003 7:26 am |
|
In my journeys this week I came across a neat and nifty little routine known as Duff's Device. You can read about the origin Only registered users can see links on this board! Get registered or login!.
I will provide a PHP equivalent here to demonstrate the dramatic performance gain. Here is the script I wrote to test 5,000,000 iterationsCode:<?
function Bench(){
//set the number of iterations
$iterations = 5000000;
$testVal = 1;
for ( $n = 0; $n < $iterations; $n++) {
$testVal++;
}
return($testVal);
}
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function DuffsDeviceBench(){
//set the number of iterations
$iterations = 5000000;
$testVal = 1;
$n = $iterations % 8;
while ($n--) $testVal++;
$n = (int) $iterations / 8;
while ($n--) {
$testVal++;
$testVal++;
$testVal++;
$testVal++;
$testVal++;
$testVal++;
$testVal++;
$testVal++;
}
return($testVal);
}
$time_start = getmicrotime();
$testVal = Bench();
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo 'Standard Loop = '.$time.' -- '.$testVal;
$time_start = getmicrotime();
$testVal = DuffsDeviceBench();
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo '<br /><br />In-Line Loop = '.$time.' -- '.$testVal;
?>
|
Here are the resultsCode:Standard Loop = 8.4020010232925 -- 5000001
In-Line Loop = 2.4259361028671 -- 5000001
| A 6 second gain!
I then ran it for 50,000,000 iterationsCode:Standard Loop = 84.608292937279 -- 50000001
In-Line Loop = 24.540007948875 -- 50000001
| A 60 second gain! Obviously this is for large transaction handling/looping. But even on a smaller scale you still will see comparable results. |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
bugsTHoR
Involved
![Involved Involved](modules/Forums/images/ranks/4stars.gif)
![](modules/Forums/images/avatars/568e5f374449c41c190ed.gif)
Joined: Apr 05, 2006
Posts: 263
|
Posted:
Wed Jul 12, 2006 12:56 pm |
|
what is looping for in PHP , if ya don`t mind me asking, need to speed my site up
i put this code in ht.access
php_value memory_limit 50M
Both me and my members have noticed the difference , still need it quicker for some reason. |
_________________ LUV RAVEN DISTROBUTION BEBE
Clanthemes.com are great (free advertisements for now until i get to 20,000 posts LoL) |
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
gregexp
The Mouse Is Extension Of Arm
![](modules/Forums/images/avatars/458c161744a70db912a6e.jpg)
Joined: Feb 21, 2006
Posts: 1497
Location: In front of a screen....HELP! lol
|
Posted:
Thu Jul 13, 2006 5:23 pm |
|
Looping is when a script will run through a set of code until something is set to false or no further results can be gained.
For example
$x=15;
While($x>=1){
$x=$x-1;
echo $x;
}
this script will output
14
13
12
and so on till it reaches 1
and will stop there, then go onto the next code,
One other form of looping is to use list
Hope this helps you understand. |
_________________ For those who stand shall NEVER fall and those who fall shall RISE once more!! |
|
![ICQ Number ICQ Number](themes/RavenIce/forums/images/lang_english/icon_icq_add.gif) |
![](themes/RavenIce/forums/images/spacer.gif) |
evaders99
Former Moderator in Good Standing
![](modules/Forums/images/avatars/803d73f6452557b947721.jpg)
Joined: Apr 30, 2004
Posts: 3221
|
Posted:
Sat Jul 15, 2006 12:57 am |
|
Looks like what its doing is essentially a form of loop unrolling. It is helpful in messy loops especially, where it is much easier to unroll the loop into seperate code lines. The assembly code will optimize it better.
In this specific case, I believe optimization is obtained because
$testVal++;
is detected to be used 8 times.
The compiler will see this and say hey, "that's just $testVal = $testVal + 8;" done once
So that's what it does
I could be wrong, but that's my understanding of most compilers. I don't know if PHP works that way to do code optimizations. |
_________________ - Only registered users can see links on this board! Get registered or login! -
Need help? Only registered users can see links on this board! Get registered or login! |
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
bugsTHoR
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Wed Sep 13, 2006 10:24 pm |
|
is this added to raven distro 2.02.20 btw ?
and if not where would i put it ?
my site needs speeding up alot and the 50mb line in .htaccess works some but still
it`s slow still.
what could i use it for and a working example here would be muchly apreciated. thx |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
evaders99
![](modules/Forums/images/avatars/gallery/blank.gif)
|
Posted:
Thu Sep 14, 2006 3:28 pm |
|
This is sample code and not something directly that can be used in RavenNuke
Really the code cannot be optimized much further. For significant performance gains, install a cache system or upgrade your web/database server |
|
|
|
![](themes/RavenIce/forums/images/spacer.gif) |
|