IntroductionProgramLoop, usually think of while or for to control.
However, apart from the well-known control structures, the two have different operational efficiency. Recently, we have made a simple and boring comparison and found that the operation efficiency of the two is indeed slightly different.
BeforeBloomberg As I have read in the game programming guide, for (;) is faster than while (1. The description in the book is C ++.
To verify the author's opinion, I used C # To write a simple test Statement (run on vs2008) and compare it. The result is the opposite.
Let's take a look at a simple test program.
1. Use A WHILE LOOP
Datetime dt1 = datetime. now;
For (long I = 0; I <100000000; I ++)
{
While (true) // 812.5
{
If (I> = 0)
Break;
}
}
Datetime dt2 = datetime. now;
Console. writeline ("Time: {0} millisecond", (dt2-dt1). totalmilliseconds );
Input result: Time: 812.5 Ms
2. Simple user for Loop Test Program
Datetime dt1 = datetime. now;
For (long I = 0; I <100000000; I ++)
{
For (;) // 828.125
{
If (I> = 0)
Break;
}
}
Datetime dt2 = datetime. now;
Console. writeline ("Time: {0} millisecond", (dt2-dt1). totalmilliseconds );
Input result: Time: 828.125 Ms
the difference is about 15 milliseconds. I don't know if it is my test program or the author is wrong? What is the true kernel scheduling between the two? Hope you can give me some advice.