Home > Web Front-end > JS Tutorial > For loop performance comparison to improve the efficiency of for loop_javascript skills

For loop performance comparison to improve the efficiency of for loop_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 18:55:14
Original
1390 people have browsed it

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:

Copy the code The code is as follows:

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();
}
}

For loop performance comparison to improve the efficiency of for loop_javascript skills

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

Related labels:
for
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Ask for code, ask for code, ask for code!!
From 1970-01-01 08:00:00
0
0
0
php - What can a for loop be used for?
From 1970-01-01 08:00:00
0
0
0
for nested loop
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template