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; On Sat, 2007-03-24 at 01:54 +0100, Tijnema ! wrote: > On 3/24/07, Richard Lynch <ceo@...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 03-24-2007
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

On Sat, 2007-03-24 at 01:54 +0100, Tijnema ! wrote:
> 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's not optimization if you just happen to write optimal code out of
habit. It's already done.

Cheers,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
Reply With Quote
  #12 (permalink)  
Old 03-24-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

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

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


I believe we are seeing times for 10000000 iterations around 4 seconds?

Or was it 26 seconds?

Whatever.

That means that at 1000 iterations, you are "saving" how much time?

..00026 seconds?
..00004 seconds?

Puhleaze.

--
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
  #13 (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 Fri, March 23, 2007 7:54 pm, Tijnema ! wrote:
> > On 3/24/07, Richard Lynch <ceo@l-i-e.com> 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.

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

>
> I believe we are seeing times for 10000000 iterations around 4 seconds?
>
> Or was it 26 seconds?
>
> Whatever.
>
> That means that at 1000 iterations, you are "saving" how much time?
>
> .00026 seconds?
> .00004 seconds?
>
> Puhleaze.


Well, if you execute this script 1000 times, you would get a
difference of 2.6 seconds?
But if every microseconds counts for your script, then you should now
about this.

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
  #14 (permalink)  
Old 03-24-2007
Sancar Saran
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

Hi,
I'm using so much FOR loops in my code, after seeing discussion I try to test.
My findigs it was giving %2 to %10 percent performance boost.

So ?

My recent template addon to this code cost me around %5 to %10 percent of
total page process..

I think It was nice trade off.

Thank you guys.

Just changing two chars.

Regards

Sancar
Reply With Quote
  #15 (permalink)  
Old 03-24-2007
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

On Sat, 2007-03-24 at 10:21 -0400, tedd wrote:
> 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.


I highly doubt it. Computers run a lot these days. There are times when
squeezing that last nanosecond makes a difference and the devotion to
finding how to squeeze it out is well worth it. Admittedly they may be
few and far between, but they do exist.

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


The time you spend can be large, but here we all know now how to speed
things up, if we just apply the principles that are easy to apply in all
the code we write, then such things will already be optimal and so in
hindsight we will already be reaping the rewards without any extra
effort.

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


They generally teach algorithmic analysis that improves the algorithm's
order of magnitude in contrast to teaching constant multiplicative
gains. However, that's not to say writing efficient code isn't a good
thing. If I have an algorithm that runs in X time, I'm sure if X is 2
years and I can shrink it to x/2 then nobody will quibble about the
constant gain since it happens to be 1 year of savings. Now depending on
the calculations you're doing, shaving off a nanosecond can indeed have
a large enough impact to make it worth your time.

Cheers,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
Reply With Quote
  #16 (permalink)  
Old 03-24-2007
tedd
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

At 2:24 PM -0400 3/24/07, Robert Cummings wrote:
>On Sat, 2007-03-24 at 10:21 -0400, tedd wrote:
>> 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.

>
>I highly doubt it. Computers run a lot these days. There are times when
>squeezing that last nanosecond makes a difference and the devotion to
>finding how to squeeze it out is well worth it. Admittedly they may be
>few and far between, but they do exist.


And, I highly doubt that the difference is worth considering -- and
it's becoming less of a consideration each day.

Remember, we were talking about the differences between a "for" vs
"while" loop.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
  #17 (permalink)  
Old 03-24-2007
Robert Cummings
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

On Sat, 2007-03-24 at 14:38 -0400, tedd wrote:
> At 2:24 PM -0400 3/24/07, Robert Cummings wrote:
> >On Sat, 2007-03-24 at 10:21 -0400, tedd wrote:
> >> 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.

> >
> >I highly doubt it. Computers run a lot these days. There are times when
> >squeezing that last nanosecond makes a difference and the devotion to
> >finding how to squeeze it out is well worth it. Admittedly they may be
> >few and far between, but they do exist.

>
> And, I highly doubt that the difference is worth considering -- and
> it's becoming less of a consideration each day.
>
> Remember, we were talking about the differences between a "for" vs
> "while" loop.


Ahh, but it was the pre-increment versus post-increment that made the
difference. every bit counts, it's the same problem as energy savings.
If you only do it in one place you don't really see a savings, if you do
it everywhere, it begins to have an impact.

Cheers,
Rob.
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
Reply With Quote
  #18 (permalink)  
Old 03-26-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

On Sat, March 24, 2007 4:52 am, Tijnema ! wrote:

>> That means that at 1000 iterations, you are "saving" how much time?
>>
>> .00026 seconds?
>> .00004 seconds?
>>
>> Puhleaze.

>
> Well, if you execute this script 1000 times, you would get a
> difference of 2.6 seconds?
> But if every microseconds counts for your script, then you should now
> about this.


No.

Re-read the benchmards.

At 1000 iterations you're looking at .000xx seconds.

At a MILLION iterations, you're looking at 0.xx seconds

At a TEN MILLION iterations, you've broken the 1-second barrier.

How many PHP scripts do a for or while loop 10 million iterations?

xx is either 26 or 4, depending on whose benchmarks you believe.

Running tests with 10 million iterations to benchmark is great for
removing overhead margin of error. It's also great to look at exactly
how many times you'd have to do something to make a significant
savings.

--
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
  #19 (permalink)  
Old 03-27-2007
Jake Gardner
 
Posts: n/a
Default Re: [PHP] Performance: While or For loop

He said if you run the /script/ itself 1000 times, not a loop with 1000
iterations. This is quite possible; I am fairly certain there are websites
out there that get accessed well over 1000 times a minute, yes?

So every minute, that website is saving a total of 2.6 seconds to do...
whatever it is websites do in their free time.

In reality, scripts rarely get executed once and then are deleted; they are
used repetitively, and the more a script is used, the more significant the
gain. Claiming to look practically on a small gain /within one execution of
a script/ is impractical in itself.

On 3/26/07, Richard Lynch <ceo@l-i-e.com> wrote:
>
> On Sat, March 24, 2007 4:52 am, Tijnema ! wrote:
>
> >> That means that at 1000 iterations, you are "saving" how much time?
> >>
> >> .00026 seconds?
> >> .00004 seconds?
> >>
> >> Puhleaze.

> >
> > Well, if you execute this script 1000 times, you would get a
> > difference of 2.6 seconds?
> > But if every microseconds counts for your script, then you should now
> > about this.

>
> No.
>
> Re-read the benchmards.
>
> At 1000 iterations you're looking at .000xx seconds.
>
> At a MILLION iterations, you're looking at 0.xx seconds
>
> At a TEN MILLION iterations, you've broken the 1-second barrier.
>
> How many PHP scripts do a for or while loop 10 million iterations?
>
> xx is either 26 or 4, depending on whose benchmarks you believe.
>
> Running tests with 10 million iterations to benchmark is great for
> removing overhead margin of error. It's also great to look at exactly
> how many times you'd have to do something to make a significant
> savings.
>
> --
> 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?
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


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

Jake Gardner wrote:

> He said if you run the /script/ itself 1000 times, not a loop with 1000
> iterations. This is quite possible; I am fairly certain there are
> websites
> out there that get accessed well over 1000 times a minute, yes?
>
> So every minute, that website is saving a total of 2.6 seconds to do...
> whatever it is websites do in their free time.
>
> In reality, scripts rarely get executed once and then are deleted;
> they are
> used repetitively, and the more a script is used, the more significant
> the
> gain. Claiming to look practically on a small gain /within one
> execution of
> a script/ is impractical in itself.


I still wouldn't go around telling people to re-write all of their code
to use for loops instead of while loops (or whatever was faster for
whatever architecture.)

Keep in mind that .000xx seconds in performance improvement certainly
does make a difference on a site that is accessed millions of times a
day, however, one bug caused by writing code that reads poorly instead
of writing clean code can cost a *lot* more in the end.

- Use what reads easier when deciding if a for/while loop is best.
- Profile your code and find the right places to optimize.

Optimizing code that takes .0001 seconds to run down to .00001 seconds
is great, 10x improvement! Who cares. Find the chunk that takes 0.5
seconds to run and optimize that to 0.05 seconds. 10x improvement still,
except that this time it actually makes a practical difference.

Travis Doherty
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

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

BB 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 08:39 PM.


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