PHP Web Host - Quality Web Hosting For All PHP Applications Just Great Software
  Login or Register
 • Home • Downloads • Your Account • Forums • 

View next topic
View previous topic


Google
 
Web RavenPHPScripts (This Site)
Post new topic   Reply to topic
Author Message
Raven
Site Admin/Owner


Joined: Aug 27, 2002
Posts: 15062
Location: Kansas

PostPosted: Mon Nov 24, 2003 7:26 am Reply with quote Back to top

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 to the forums!
.
I will provide a PHP equivalent here to demonstrate the dramatic performance gain. Here is the script I wrote to test 5,000,000 iterations
Code:
<?
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 results
Code:
Standard Loop = 8.4020010232925 -- 5000001

In-Line Loop = 2.4259361028671 -- 5000001
A 6 second gain!

I then ran it for 50,000,000 iterations
Code:
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.
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger
bugsTHoR
Worker
Worker


Joined: Apr 05, 2006
Posts: 172

PostPosted: Wed Jul 12, 2006 12:56 pm Reply with quote Back to top

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.
View user's profile Send private message Visit poster's website
gregexp
The Mouse Is Extension Of Arm


Joined: Feb 21, 2006
Posts: 1472
Location: In front of a screen....HELP! lol

PostPosted: Thu Jul 13, 2006 5:23 pm Reply with quote Back to top

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.
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
evaders99
Moderator


Joined: Apr 30, 2004
Posts: 2796

PostPosted: Sat Jul 15, 2006 12:57 am Reply with quote Back to top

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 Smile

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.
View user's profile Send private message Visit poster's website
bugsTHoR
Worker
Worker


Joined: Apr 05, 2006
Posts: 172

PostPosted: Wed Sep 13, 2006 10:24 pm Reply with quote Back to top

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
View user's profile Send private message Visit poster's website
evaders99
Moderator


Joined: Apr 30, 2004
Posts: 2796

PostPosted: Thu Sep 14, 2006 3:28 pm Reply with quote Back to top

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
View user's profile Send private message Visit poster's website
Display posts from previous:       
Post new topic   Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Forums ©
 

All logos and trademarks in this site are property of their respective owner.
The comments are property of their posters, all the rest © 2002-2008 by Raven
Proud to be listed at Lobo Links Web Directory

You can syndicate our news using the file xml

CSE HTML Validator Helped Clean up This Page! [Valid RSS] valid RSS 2.0 Valid robots.txt Stop Spam Harvesters, Join Project Honey Pot

Website engines core code is © copyright by PHP-Nuke but has been heavily patched and modified by myself and others.
PHP-Nuke is a free software released under the GNU/GPL.


:: fisubice phpbb2 style by Daz :: PHP-Nuke theme by www.nukemods.com ::

:: fisubice Theme Recoded To 100% W3C CSS & HTML 4.01 Transitional Compliance by Raven and 64bitguy ::

:: W3C CSS Compliance Validation :: W3C HTML 4.01 Transitional Compliance Validation ::

zerosum