We can read this comment at php.net
[color=blue]
>jon
>05-Nov-2003 10:06
>According to my tests, the "do...while " control structure actually seems to[/color]
be ~40% faster than the "for" control structure. At least using PHP v4.0.6[color=blue]
>on a few fairly modern Linux machines.[/color]
[color=blue]
>I used a the getmicrotime function as defined at[/color]
[color=blue]
>I tested the following 5 scenarios, including two different ways of calling[/color]
a "for" loop. The less ordinary way to call "for", I'll call "altfor".
Here >are the code scenarios for different ways from counting from
0->($num-1), in my case $num = 10000:
[color=blue]
>// 'altfor':
> $a=0;
> for (; $a<$num;) { $a++;}[/color]
[color=blue]
>// 'for':
> for ($a=0; $a<$num; $a++) {}[/color]
[color=blue]
>// 'foreach':
> foreach (range(0,$num-1) as $a ) {}[/color]
[color=blue]
>// 'while':
> $a = 0;
> while ($a < $num) {$a++;}[/color]
[color=blue]
>// 'do':
> $a = 0;
> do {} while (++$a < $num);[/color]
[color=blue]
>Each scenario is called in random order, and here is a typical result:[/color]
[color=blue]
>Average times for 100 iterations of each loop: altfor, do, for, foreach,[/color]
while.[color=blue]
>All loops are incremented from 0 to 9999.[/color]
[color=blue]
> altfor: 0.0368828332424 16 seconds, 138.25 percent of ~ 0.0267 seconds
> do: 0.0266782820224 76 seconds, 100.00 percent of ~ 0.0267 seconds
> for: 0.0373089981079 10 seconds, 139.85 percent of ~ 0.0267 seconds
>foreach: 0.0648571395874 02 seconds, 243.11 percent of ~ 0.0267 seconds
> while: 0.0365088176727 29 seconds, 136.85 percent of ~ 0.0267 seconds
>
>...so to be clear, I ran "for", et all, from 0->9999. This was done 100[/color]
times. Above are the average times for those 100 tests of loops of 10,000.
I >have run my script multiple time on several different Linux systems here
at my work. All w/ similar results...
As anyone tryed to see if thats true and do...while is about 40% faster than
for ?
[color=blue]
>jon
>05-Nov-2003 10:06
>According to my tests, the "do...while " control structure actually seems to[/color]
be ~40% faster than the "for" control structure. At least using PHP v4.0.6[color=blue]
>on a few fairly modern Linux machines.[/color]
[color=blue]
>I used a the getmicrotime function as defined at[/color]
[color=blue]
>I tested the following 5 scenarios, including two different ways of calling[/color]
a "for" loop. The less ordinary way to call "for", I'll call "altfor".
Here >are the code scenarios for different ways from counting from
0->($num-1), in my case $num = 10000:
[color=blue]
>// 'altfor':
> $a=0;
> for (; $a<$num;) { $a++;}[/color]
[color=blue]
>// 'for':
> for ($a=0; $a<$num; $a++) {}[/color]
[color=blue]
>// 'foreach':
> foreach (range(0,$num-1) as $a ) {}[/color]
[color=blue]
>// 'while':
> $a = 0;
> while ($a < $num) {$a++;}[/color]
[color=blue]
>// 'do':
> $a = 0;
> do {} while (++$a < $num);[/color]
[color=blue]
>Each scenario is called in random order, and here is a typical result:[/color]
[color=blue]
>Average times for 100 iterations of each loop: altfor, do, for, foreach,[/color]
while.[color=blue]
>All loops are incremented from 0 to 9999.[/color]
[color=blue]
> altfor: 0.0368828332424 16 seconds, 138.25 percent of ~ 0.0267 seconds
> do: 0.0266782820224 76 seconds, 100.00 percent of ~ 0.0267 seconds
> for: 0.0373089981079 10 seconds, 139.85 percent of ~ 0.0267 seconds
>foreach: 0.0648571395874 02 seconds, 243.11 percent of ~ 0.0267 seconds
> while: 0.0365088176727 29 seconds, 136.85 percent of ~ 0.0267 seconds
>
>...so to be clear, I ran "for", et all, from 0->9999. This was done 100[/color]
times. Above are the average times for those 100 tests of loops of 10,000.
I >have run my script multiple time on several different Linux systems here
at my work. All w/ similar results...
As anyone tryed to see if thats true and do...while is about 40% faster than
for ?
Comment