Performance: While or For loop

This is a discussion on Performance: While or For loop within the PHP General forums, part of the PHP Programming Forums category; <!&!AAAAAAAAAAAYAAAAAAAAAN8daGFLXzBPhXulF4C17xvCgA AAEAAAABW7otzHg2xFhphLJ8 bqJHcBAAAAAA==@nittanytravel.com> <d8269d910703221439k2284ed4ex139cb7379662ac42@mail .gmail.com> <4602FEB1.4090406@gamingsolutions.ca> &...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-01-1970
 
Posts: n/a
Default No Subject

<!&!AAAAAAAAAAAYAAAAAAAAAN8daGFLXzBPhXulF4C17xvCgA AAEAAAABW7otzHg2xFhphLJ8
bqJHcBAAAAAA==@nittanytravel.com>
<d8269d910703221439k2284ed4ex139cb7379662ac42@mail .gmail.com>
<4602FEB1.4090406@gamingsolutions.ca>
<d8269d910703221514x7dadf98eid059add34be955e@mail. gmail.com>
<54390.216.230.84.67.1174697249.squirrel@www.l-i-e.com>
Date: Sat, 24 Mar 2007 10:21:07 -0400
To: php-general@lists.php.net
Content-Type: text/plain; charset="us-ascii" ; format="flowed"
Subject: Re: [php] Performance: While or For loop
From: bogus@news.php.net (Unknown Sender)
Xref: number1.nntp.dca.giganews.com php.general:140494

At 7:47 PM -0500 3/23/07, Richard Lynch wrote:
>Folks:
>
>How often do you use a loop of any kind in PHP with enough iterations
>that this is even significant?
>
>Write the code that makes sense.
>
>Optimize the biggest bottleneck until performance is acceptable.


Absolutely -- the time we take discussing these types of concerns
probably cost more cycles than the sum total of all the "optimized"
savings in the lifetime of all the computers on earth.

The time you spend trying to make something faster in those terms
clearly cost more time in development and maintenance. It's a
trade-off that's not worth it.

Q: Do they teach this stuff in college now or are the teachers still
only measuring performance in myopic terms of saving memory and time?
Not a criticism, just a question.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
  #2 (permalink)  
Old 03-22-2007
Tijnema !
 
Posts: n/a
Default Performance: While or For loop

Hi,

Does somebody has benchmarks of what is faster, while or for loop?

Thanks,

Tijnema
Reply With Quote
  #3 (permalink)  
Old 03-22-2007
Jake McHenry
 
Posts: n/a
Default RE: [PHP] Performance: While or For loop

does this help?

http://www.php.lt/benchmark/phpbench.php

Jake



> -----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

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

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
  #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
  #6 (permalink)  
Old 03-23-2007
Travis Doherty
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

Tijnema ! wrote:

> 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) {}

>


This has been asked many times, probably likewise for every language.
Search for the same question on the C programming language for a more in
depth discussion of this and to find out why one way is faster than the
other...

Major factor: Don't forget the difference between pre and post increment
operators. $i++ and ++$i.

For reference this is my PHP test script and results:

{{{
tdoherty@gamma tdoherty $ cat ./forwhile.php
<?
$i=0; $start = microtime(TRUE);
for ($i=0; $i<100000; ++$i) {}
echo sprintf("For pre-increment ($i): %0.3f\n",microtime(TRUE) - $start);

$i=0; $start = microtime(TRUE);
for ($i=0; $i<100000; $i++) {}
echo sprintf("For post-increment ($i): %0.3f\n",microtime(TRUE) - $start);

$i=0; $start = microtime(TRUE);
while (++$i < 100000) {}
echo sprintf("While pre-increment ($i): %0.3f\n",microtime(TRUE) - $start);

$i=0; $start = microtime(TRUE);
while ($i++ < 100000) {}
echo sprintf("While post-increment ($i): %0.3f\n",microtime(TRUE) - $start);
?>
tdoherty@gamma tdoherty $ php ./forwhile.php
For pre-increment (100000): 0.035
For post-increment (100000): 0.060
While pre-increment (100000): 0.029
While post-increment (100001): 0.056
}}}

After multiple runs I see that the for pre-increment loop is fastest.
Note that the while loop with a post-increment runs once more than with
a pre-increment.

Everytime I run, the results are *very* different, though still fall
within similar comparitive domains.

Travis Doherty
Reply With Quote
  #7 (permalink)  
Old 03-23-2007
Mario Guenterberg
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

On Fri, Mar 23, 2007 at 12:24:45AM -0500, Travis Doherty wrote:
> After multiple runs I see that the for pre-increment loop is fastest.
> Note that the while loop with a post-increment runs once more than with
> a pre-increment.
>
> Everytime I run, the results are *very* different, though still fall
> within similar comparitive domains.


Hi...

I used your script and my results are different from yours.
The post-while increment is generally the fastest result on my
machine (Core 2 Duo, php 5.2.1 and apache 2.2.4).

I ran the script multiple times:

[schnipp ]

For pre-increment (100000): 0.066
For post-increment (100000): 0.066
While pre-increment (100000): 0.028
While post-increment (100001): 0.025

[schnipp ]

Greetings
Mario

--
-----------------------------------------------------
| havelsoft.com - Ihr Service Partner für Open Source |
| Tel: 033876-21 966 |
| Notruf: 0173-277 33 60 |
| http://www.havelsoft.com |
| |
| Inhaber: Mario Günterberg |
| Mützlitzer Strasse 19 |
| 14715 Märkisch Luch |
-----------------------------------------------------
Reply With Quote
  #8 (permalink)  
Old 03-23-2007
Tijnema !
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

On 3/23/07, Mario Guenterberg <mg@havelsoft.com> wrote:
> On Fri, Mar 23, 2007 at 12:24:45AM -0500, Travis Doherty wrote:
> > After multiple runs I see that the for pre-increment loop is fastest.
> > Note that the while loop with a post-increment runs once more than with
> > a pre-increment.
> >
> > Everytime I run, the results are *very* different, though still fall
> > within similar comparitive domains.

>
> Hi...
>
> I used your script and my results are different from yours.
> The post-while increment is generally the fastest result on my
> machine (Core 2 Duo, php 5.2.1 and apache 2.2.4).
>
> I ran the script multiple times:
>
> [schnipp ]
>
> For pre-increment (100000): 0.066
> For post-increment (100000): 0.066
> While pre-increment (100000): 0.028
> While post-increment (100001): 0.025
>
> [schnipp ]
>
> Greetings
> Mario


Well, myh results are a little bit confusing again, if using
pre-increment, for shows up faster now. But while post-increment is
the fastest :) Is there any logic why we all have different results?
does it has to do something with the type of CPU, i'm using AMD
Athlon. 1800+ (clocked down from 133 to 100 * 11.5)

For pre-increment (100000): 0.026
For post-increment (100000): 0.030
While pre-increment (100000): 0.029
While post-increment (100001): 0.023

Tijnema

ps. @ Mario: Youre C2D got owned by a AMD 1.15Ghz :)


>
> --
> -----------------------------------------------------
> | havelsoft.com - Ihr Service Partner für Open Source |
> | Tel: 033876-21 966 |
> | Notruf: 0173-277 33 60 |
> | http://www.havelsoft.com |
> | |
> | Inhaber: Mario Günterberg |
> | Mützlitzer Strasse 19 |
> | 14715 Märkisch Luch |
> -----------------------------------------------------
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply With Quote
  #9 (permalink)  
Old 03-24-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

On Thu, March 22, 2007 5:14 pm, Tijnema ! wrote:
> 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:


Folks:

How often do you use a loop of any kind in PHP with enough iterations
that this is even significant?

Write the code that makes sense.

Optimize the biggest bottleneck until performance is acceptable.

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
Reply With Quote
  #10 (permalink)  
Old 03-24-2007
Tijnema !
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

On 3/24/07, Richard Lynch <ceo@l-i-e.com> wrote:
> On Thu, March 22, 2007 5:14 pm, Tijnema ! wrote:
> > 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:

>
> Folks:
>
> How often do you use a loop of any kind in PHP with enough iterations
> that this is even significant?
>
> Write the code that makes sense.
>
> Optimize the biggest bottleneck until performance is acceptable.


It was more likely to get an idea if there was a real difference or
not, and apparently there is not really a big difference.

But well if you are going to create a script where 1000+ loops are,
you might get a few seconds faster script :)

Tijnema
>
> --
> Some people have a "gift" link here.
> Know what I want?
> I want you to buy a CD from some indie artist.
> http://cdbaby.com/browse/from/lynch
> Yeah, I get a buck. So?
>
>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:57 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0