View Single Post

  #5 (permalink)  
Old 03-22-2007
Tijnema !
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

On 3/22/07, Jon Anderson <janderson@gamingsolutions.ca> wrote:
> Your test isn't exactly fair. The for loop has no statements in it, and
> the while loop has one. Your tests show while as approx 7% faster, while
> a modified test shows an approximate 30% speed improvement:
>
> Do this:
>
> for ($i=0;$i<10000000;$i++) {}
>
> v.s.:
>
> $i = 0;
> while ($i++ < 10000000) {}
>
> The above while loop is 30% faster than the for. A little teaking on the
> for loop, it's faster still:


I was comparing the way people normal use while and for. But this does
confirms what i said, while is faster.
>
> for ($i=10000000;$i--;) {}
>
> A little tweaking on the while, and it's faster yet:
>
> $i = 10000000;
> while ($i--) {}
>
> The last while loop is around 60% faster on my hardware than the first
> for loop.
>
> jon
>
> Tijnema ! wrote:
> > On 3/22/07, Jake McHenry <linux@nittanytravel.com> wrote:
> >> does this help?
> >>
> >> http://www.php.lt/benchmark/phpbench.php
> >>
> >> Jake

> >
> > Well, there wasn't a test between For and While, so i did it myself,
> > and i'm quite confused about the result.
> > I let PHP count from 0 to 100000000 on my 1ghz AMD Athlon.
> > While did it in a average time of 55 seconds.
> > For did it in a average time of 59 seconds.
> > It's not a big difference, if you see what number it needs to go to,
> > but there is a difference.
> >
> > code used:
> > <?php
> > set_time_limit(100000);
> > echo date("H:i:s");
> > for($i = 0; $i < 100000000;$i++)
> > {
> > }
> > echo date("H:i:s");
> > $x = 0;
> > while($x < 100000000)
> > {
> > $x++;
> > }
> > echo date("H:i:s");
> > ?>
> >
> > So, use while loops instead of for loops ;)
> >
> > Tijnema
> >
> > ps. I also did the while loop before the for loop, doesn't matter.
> >
> >>
> >>
> >>
> >> > -----Original Message-----
> >> > From: Tijnema ! [mailto:tijnema@gmail.com]
> >> > Sent: Thursday, March 22, 2007 4:38 PM
> >> > To: PHP
> >> > Subject: [php] Performance: While or For loop
> >> >
> >> > Hi,
> >> >
> >> > Does somebody has benchmarks of what is faster, while or for loop?
> >> >
> >> > Thanks,
> >> >
> >> > Tijnema
> >> >
> >> > --
> >> > PHP General Mailing List (http://www.php.net/)
> >> > To unsubscribe, visit: http://www.php.net/unsub.php
> >> >
> >> > --
> >> > No virus found in this incoming message.
> >> > Checked by AVG Free Edition.
> >> > Version: 7.5.446 / Virus Database: 268.18.17/730 - Release
> >> > Date: 3/22/2007 7:44 AM
> >> >
> >> >
> >>
> >> --
> >> No virus found in this outgoing message.
> >> Checked by AVG Free Edition.
> >> Version: 7.5.446 / Virus Database: 268.18.17/730 - Release Date:
> >> 3/22/2007
> >> 7:44 AM
> >>
> >>
> >> --
> >> PHP General Mailing List (http://www.php.net/)
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >>
> >>

> >

>
>

Reply With Quote