|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Interesting piece of php trivia
Did you know that foreach($ar as $key=>$val) {} is two times faster than the while(list($key,$val) = each($ar)) alternative? *gasp* i can't imagine why i insisted on using while(...) to iterate thru my arrays...
|
|
#2
|
|||||
|
|||||
|
RE: Interesting piece of php trivia
That is interesting..I guess the extra function calls just cause the overhead...here's my results:
Code:
foreach: 4.04250204563 while: 7.35851705074 php Code:
|
|
#3
|
|||
|
|||
|
RE: Interesting piece of php trivia
I thought I was just being lazy by using foreach()...
Josh |
|
#4
|
|||
|
|||
|
RE: Interesting piece of php trivia
why is this so surprising to you? when some funcion is built in the language, it means that it is implemented in C, so it has to be faster.
php doesn't have to have so many functinos, especialy string and array functions, since all or most of them can be done in php, but one of the reasons it does, is because their implementation is oh-so-much-faster than in php... |
|
#5
|
|||
|
|||
|
RE: Interesting piece of php trivia
i just found it strange, coz foreach() makes a copy of the array (so two foreach()'s inside each other working on the same array will work, and while() wont) so i assumed, quite wrongly, that it has to be slower than just resetting the array..
another cool thing is that with foreach() you don't have to use reset().. yeah, i forgot to take into account the compiled code advantage ;) shoot me. |
|
#6
|
|||
|
|||
|
RE: Interesting piece of php trivia
BANG!
|
|
#7
|
|||
|
|||
|
RE: Interesting piece of php trivia
ahh.. That feels much better, thanks.
|
![]() |
| Viewing: Codewalkers Forums > General > General Chat > Interesting piece of php trivia |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|