You can get the same output with for and while loops:
WHILE
$i = 0;
while ($i <= 10){
print $i."\n";
$i++;
};
FOR
for ($i = 0; $i <= 10; $i++){
print $i."\n";
}
But which one is faster?
You can get the same output with for and while loops:
WHILE
$i = 0;
while ($i <= 10){
print $i."\n";
$i++;
};
FOR
for ($i = 0; $i <= 10; $i++){
print $i."\n";
}
But which one is faster?
That clearly depends on the particular implementation of the interpreter/compiler of the specific language.
That said, theoretically, any sane implementation is likely to be able to implement one in terms of the other if it was faster so the difference should be negligible at most.
Of course, I assumed while
and for
behave as they do in C and similar languages. You could create a language with completely different semantics for while
and for
Depends on the language and most likely its compiler, but they should be equivalent in most languages.
They should be equal. The for loop you wrote is doing exactly the same thing that the while loop is doing: setting $i=0
, printing $i
, and incrementing $i
at the end of the loop.
If that were a C program, I would say neither. The compiler will output exactly the same code. Since it's not, I say measure it. Really though, it's not about which loop construct is faster, since that's a miniscule amount of time savings. It's about which loop construct is easier to maintain. In the case you showed, a for loop is more appropriate because it's what other programmers (including future you, hopefully) will expect to see there.
It shouldn't matter which is faster. If it does matter then benchmark it using your real code and see for yourself.
The answers to this other question might be useful as well: http://stackoverflow.com/questions/3586633
That will depend on the language implementation of said loop, compiler and what not.
Most compiler will compile to the exact same executable code for example in CIL (.NET) they definitely do.
Source: vcsjones @ http://forums.asp.net/t/1041090.aspx
Either way, the body of the loop is where the processing time will be spent not the way you iterate.
Set the loop iterations to 10,000.
Find the time in milliseconds>Run Loop>find time in milliseconds and subtract the first timer.
Do it for both codes, what ever one has the lowest milliseconds it runs faster. You might want to run the test multiple times and average them out to reduce the likelihood of background processes influencing the test.
You are likely to get really similar times on both of them, but I am interested to see if one is always just slightly faster.
As others have said, any compiler worth its salt will generate practically identical code. Any difference in performance is negligible - you are micro-optimizing.
The real question is, what is more readable? And that's the for
loop (at least IMHO).
I find the fastest loop is a reverse while loop, e.g:
var i = myArray.length; while(i--){ // Do something }
In C#, the For loop is slightly faster.
For loop average about 2.95 to 3.02 ms.
The While loop averaged about 3.05 to 3.37 ms.
Quick little console app to prove:
class Program
{
static void Main(string[] args)
{
int max = 1000000000;
Stopwatch stopWatch = new Stopwatch();
if (args.Length == 1 && args[0].ToString() == "While")
{
Console.WriteLine("While Loop: ");
stopWatch.Start();
WhileLoop(max);
stopWatch.Stop();
DisplayElapsedTime(stopWatch.Elapsed);
}
else
{
Console.WriteLine("For Loop: ");
stopWatch.Start();
ForLoop(max);
stopWatch.Stop();
DisplayElapsedTime(stopWatch.Elapsed);
}
}
private static void WhileLoop(int max)
{
int i = 0;
while (i <= max)
{
//Console.WriteLine(i);
i++;
};
}
private static void ForLoop(int max)
{
for (int i = 0; i <= max; i++)
{
//Console.WriteLine(i);
}
}
private static void DisplayElapsedTime(TimeSpan ts)
{
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine(elapsedTime, "RunTime");
}
}
Some optimizing compilers will be able to do better loop unrolling with a for loop, but odds are that if you're doing something that can be unrolled, a compiler smart enough to unroll it is probably also smart enough to interpret the loop condition of your while loop as something it can unroll as well.