Page 2 of 2

Posted: Mon Jul 03, 2006 8:14 am
by CoderGoblin
Personally when needing a for loop I tend to use a range...

Code: Select all

foreach (range(1,10) as $num) {
  echo($num.'<br />');
}

Posted: Mon Jul 24, 2006 12:01 am
by John Cartwright
Robert Plank wrote:
That's odd.. I just made a quick test suite
I guess it would be more fair if our code actually checked an array instead of counting up numbers.

results:

Code: Select all

test_ForeachWithIndice() 79.1614859104 seconds
test_ForeachWithoutIndice() 69.2696409225 seconds
test_For() 71.0619039536 seconds
Sorry it's taken me so long to reply. I just ran your test suite and these were my results.

Code: Select all

test_ForeachWithIndice() 75.1973428726 seconds
test_ForeachWithoutIndice() 70.8360300064 seconds
test_For() 66.339318037 seconds
I can't really explain why your for() is slower than a foreach(). From all the results I've ever processed and debates seen for() was always the fastest.

Posted: Mon Jul 24, 2006 12:04 am
by John Cartwright
CoderGoblin wrote:Personally when needing a for loop I tend to use a range...

Code: Select all

foreach (range(1,10) as $num) {
  echo($num.'<br />');
}
Seems like a waste of rescources, very minimal although but still not ideal in my books. You can hammer a nail with a sludge hammer -- or you can use the right tool for the job.