Re: Freelance code optimizations

From: Date: Mon, 11 Aug 2003 20:01:09 +0000
Subject: Re: Freelance code optimizations
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-159185@lists.php.net to get a copy of this message
Comparison to a variable is expectedly slower since the engine must perform a lookup into the running script's variable list. Since a constant doesn't require a lookup it's access time is O( 1 ) whereas the lookup of a variable will lie someplace between O( 1 ) and O( lg n ) depending on the algorithm used. Cheers, Rob. On Mon, 2003-08-11 at 15:44, Ray wrote: > also, if you can compare to a constant, there is a notable difference as well. > > <?php > $i = 0; > $a = 10000000; > while( $i++ < $a ){} > ?> > real 0m10.268s > user 0m10.000s > sys 0m0.030s > > <?php > $i = 0; > $a = 10000000; > while( $i++ < 10000000 ){} > ?> > real 0m7.057s > user 0m6.880s > sys 0m0.020s > > so if $a is something like the size of an array or another thing that affects > the number of times the loop needs to be run. then if you can, reverse the > order of the loop so you can compare to 0 (or another constant) > > <?php > $a = 10000000; > $i = $a; > while( $i-- > 0 ){} > ?> > real 0m7.111s > user 0m6.870s > sys 0m0.040s > > YMMV > -- .---------------------------------------------. | Worlds of Carnage - http://www.wocmud.org | :---------------------------------------------: | Come visit a world of myth and legend where | | fantastical creatures come to life and the | | stuff of nightmares grasp for your soul. | `---------------------------------------------'

« previous php.general (#159185) next »