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

On Monday 11 August 2003 13:10, Robert Cummings wrote:
> On Mon, 2003-08-11 at 13:29, Chris W. Parker wrote:
> > * while loop
> >
> > $ctr = 0;
> > $val = 0;
> > while($ctr<100000)
> > {
> >     $val++;
> >     $ctr++;
> > }
> >
> > ** for loop
> >
> > $val = 0;
> > for($ctr=0;$ctr<100000;$ctr++)
> > {
> >     $val++;
> > }
>
> I get the following results (very consistently +/- .1) which agrees with
> your results and also shows an improvement over your own optimization.
>
> for( $i = 0; $i < 10000000; $i++ ){}
>
> > time php foo.php
>
> Content-type: text/html
> X-Powered-By: PHP/4.3.2
>
> 4.42user 0.03system 0:04.60elapsed 96%CPU (0avgtext+0avgdata
> 0maxresident)k
> 0inputs+0outputs (528major+216minor)pagefaults 0swaps
>
> -----------------------------------------------------------
>
> $i = 0;
> while( $i < 10000000 ){ $i++; }
>
> > time php fee.php
>
> Content-type: text/html
> X-Powered-By: PHP/4.3.2
>
> 4.22user 0.01system 0:04.43elapsed 95%CPU (0avgtext+0avgdata
> 0maxresident)k
> 0inputs+0outputs (527major+216minor)pagefaults 0swaps
>
> -----------------------------------------------------------
>
> $i = 0;
> while( $i++ < 10000000 ){}
>
> > time php foo.php
>
> Content-type: text/html
> X-Powered-By: PHP/4.3.2
>
> 2.97user 0.00system 0:03.09elapsed 96%CPU (0avgtext+0avgdata
> 0maxresident)k
> 0inputs+0outputs (526major+216minor)pagefaults 0swaps
>
> -----------------------------------------------------------
>
> Notice the large difference when the incrementation occurs within the
> while check itself.
>
> Cheers,
> Rob.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to