Compared with for(int i=1,n=tempUser.length;i for(int i=1,;i I am used to for(int i=1,;i I immediately I tested it and found that the performance difference is very small, and the latter is even more efficient.
Although I got this result, it was completely opposite to what I thought. Of course, the answer is pretty much the same.
Today I found another senior’s blog that had a response to this post. He did a test, and the content was roughly that the problem in the previous post should be similar, but he conducted a more in-depth test and came to the conclusion: in the case of for (int i=0;iI also did a test:
class Program {
static void Main(string[] args) {
string s;
s = Console.ReadLine();
while(s != "0") {
Person p = new Person(10000000);
Console.WriteLine("for(int i=0;iConsole.WriteLine("Starting.. .");
long l1 = DateTime.Now.Ticks;
for(int i = 0; i < p.Child.Alias.Length; i ) {
//Console. Write(lenArray[i]);
}
long l2 = DateTime.Now.Ticks;
Console.WriteLine("Ending.nTime is : " (l2 - l1). ToString());
Console.WriteLine("for(int i=0,n=lenArray.length;iConsole.WriteLine("Starting... ");
long l3 = DateTime.Now.Ticks;
for(int i = 0, n = p.Child.Alias.Length; i < n; i ) {
/ /Console.Write(lenArray[i]);
}
long l4 = DateTime.Now.Ticks;
Console.WriteLine("Ending.nTime is : " (l4 - l3). ToString());
s = Console.ReadLine();
}
Console.Read();
}
}
The larger the amount of data, the larger the class, and the more obvious the difference.
Conclusion: The impact should be that the variable must access the next-level variable in each loop. If it is just a simple a.length, the compiler may have optimized such a loop operation and saved a.length to memory or a place larger than memory, but multi-level is not so lucky.
We all know the for(int i=0;i