Page 1 of 1
Ask a question.
Posted: Sat Jul 20, 2002 3:26 am
by kaily
IF you know or have any idea plaese reply.
code1.
Code: Select all
for ($i=0;$i<9119;$i++){
do_something_here();
}
code2.
Code: Select all
$i=0;
while ($i<9119){
do_something_here();
$i++;
}
Can code2 instead of code1 anyway?
Which code is faster(or better)?
Posted: Sat Jul 20, 2002 4:29 am
by gnu2php
If I had to guess, I'd say the while() loop is slightly faster--since it has a simpler construct. But since this is PHP, the code has to go through the parser, so any performance increase is probably lost by the fact that the while() code has 6 more characters.
In any case, if you're concerned about performance, you might consider creating a
PHP extension for time-critical functions--but note that this can be a very difficult task.
Posted: Sat Jul 20, 2002 5:29 am
by twigletmac
If you're worried about speed why not add a bit of code that works out how long it takes to execute (search the forums there's a lot of that about). As for which is better, well the for loop was designed to do the job so why not use it for what it is intended.
Mac
Posted: Sat Jul 20, 2002 12:53 pm
by gnu2php
FYI--there was an interesting thing I tried. I ran a little performance test on my computer by setting each loop to run 10000000 times, and print one character every time. Since I have PHP stop after 30 seconds, it didn't loop endlessly. I found that the while() loop is actually slightly faster than the other. It produced a 1.50 MB file, and the for() loop only did a 1.35 MB file.