You can get the same output with for and while loops:
While:
$i = 0;
while ($i <= 10){
print $i.\"\\n\";
$i++;
};
I also tried to benchmark the different kinds of loop in C#. I used the same code as Shane, but I also tried with a do-while and found it to be the fastest. This is the code:
using System;
using System.Diagnostics;
public class Program
{
public static void Main()
{
int max = 9999999;
Stopwatch stopWatch = new Stopwatch();
Console.WriteLine("Do While Loop: ");
stopWatch.Start();
DoWhileLoop(max);
stopWatch.Stop();
DisplayElapsedTime(stopWatch.Elapsed);
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("While Loop: ");
stopWatch.Start();
WhileLoop(max);
stopWatch.Stop();
DisplayElapsedTime(stopWatch.Elapsed);
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("For Loop: ");
stopWatch.Start();
ForLoop(max);
stopWatch.Stop();
DisplayElapsedTime(stopWatch.Elapsed);
}
private static void DoWhileLoop(int max)
{
int i = 0;
do
{
//Performe Some Operation. By removing Speed increases
var j = 10 + 10;
j += 25;
i++;
} while (i <= max);
}
private static void WhileLoop(int max)
{
int i = 0;
while (i <= max)
{
//Performe Some Operation. By removing Speed increases
var j = 10 + 10;
j += 25;
i++;
};
}
private static void ForLoop(int max)
{
for (int i = 0; i <= max; i++)
{
//Performe Some Operation. By removing Speed increases
var j = 10 + 10;
j += 25;
}
}
private static void DisplayElapsedTime(TimeSpan ts)
{
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");
}
}
and these are the results of a live demo on DotNetFiddle:
Do While Loop:
00:00:00.06While Loop:
00:00:00.13For Loop:
00:00:00.27
I find the fastest loop is a reverse while loop, e.g:
var i = myArray.length;
while(i--){
// Do something
}
Isn't a For Loop technically a Do While?
E.g.
for (int i = 0; i < length; ++i)
{
//Code Here.
}
would be...
int i = 0;
do
{
//Code Here.
} while (++i < length);
I could be wrong though...
Also when it comes to for loops. If you plan to only retrieve data and never modify data you should use a foreach. If you require the actual indexes for some reason you'll need to increment so you should use the regular for loop.
for (Data d : data)
{
d.doSomething();
}
should be faster than...
for (int i = 0; i < data.length; ++i)
{
data[i].doSomething();
}
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.
I was wondering the same thing so i googled and ended up here. I did a small test in python (extremely simple) just to see and this is what I got:
For:
def for_func(n = 0):
for n in range(500):
n = n + 1
python -m timeit "import for_func; for_func.for_func()" > for_func.txt
10000 loops, best of 3: 40.5 usec per loop
While:
def while_func(n = 0):
while n < 500:
n = n + 1
python -m timeit "import while_func; while_func.while_func()" > while_func.txt
10000 loops, best of 3: 45 usec per loop
As for infinite loops for(;;)
loop is better than while(1)
since while
evaluates every time the condition but again it depends on the compiler.