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

May 16, 2016 pm 06:55 PM
for

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

17 ways to solve the kernel_security_check_failure blue screen 17 ways to solve the kernel_security_check_failure blue screen Feb 12, 2024 pm 08:51 PM

Kernelsecuritycheckfailure (kernel check failure) is a relatively common type of stop code. However, no matter what the reason is, the blue screen error causes many users to be very distressed. Let this site carefully introduce 17 types to users. Solution. 17 solutions to kernel_security_check_failure blue screen Method 1: Remove all external devices When any external device you are using is incompatible with your version of Windows, the Kernelsecuritycheckfailure blue screen error may occur. To do this, you need to unplug all external devices before trying to restart your computer.

How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer Feb 13, 2024 pm 12:30 PM

Can Win10 skype be uninstalled? This is a question that many users want to know, because many users find that this application is included in the default program on their computers, and they are worried that deleting it will affect the operation of the system. Let this website help users Let’s take a closer look at how to uninstall Skype for Business in Win10. How to uninstall Skype for Business in Win10 1. Click the Windows icon on the computer desktop, and then click the settings icon to enter. 2. Click "Apply". 3. Enter "Skype" in the search box and click to select the found result. 4. Click "Uninstall". 5

How to use for to find the factorial of n in JavaScript How to use for to find the factorial of n in JavaScript Dec 08, 2021 pm 06:04 PM

How to use for to find n factorial: 1. Use the "for (var i=1;i<=n;i++){}" statement to control the loop traversal range to "1~n"; 2. In the loop body, use "cj *=i" Multiply the numbers from 1 to n, and assign the product to the variable cj; 3. After the loop ends, the value of the variable cj is the factorial of n, and then output it.

What is the difference between foreach and for loop What is the difference between foreach and for loop Jan 05, 2023 pm 04:26 PM

Difference: 1. for loops through each data element through the index, while forEach loops through the data elements of the array through the JS underlying program; 2. for can terminate the execution of the loop through the break keyword, but forEach cannot; 3. for can control the execution of the loop by controlling the value of the loop variable, but forEach cannot; 4. for can call loop variables outside the loop, but forEach cannot call loop variables outside the loop; 5. The execution efficiency of for is higher than forEach.

What are the common flow control structures in Python? What are the common flow control structures in Python? Jan 20, 2024 am 08:17 AM

What are the common flow control structures in Python? In Python, the flow control structure is an important tool used to determine the execution order of the program. They allow us to execute different blocks of code based on different conditions, or to execute a block of code repeatedly. The following will introduce common process control structures in Python and provide corresponding code examples. Conditional statements (if-else): Conditional statements allow us to execute different blocks of code based on different conditions. Its basic syntax is: if condition 1: #when condition

Use the Go language for loop to quickly implement the flip function Use the Go language for loop to quickly implement the flip function Mar 25, 2024 am 10:45 AM

Implementing the flip function using Go language can be implemented very quickly through a for loop. The flip function is to reverse the order of elements in a string or array, and can be applied in many scenarios, such as string flipping, array element flipping, etc. Let's take a look at how to use the for loop of Go language to realize the flip function of strings and arrays, and attach specific code examples. String flipping: packagemainimport(&quot;fmt&quot;)fun

How to avoid exceptions in simple for loops in JAVA? How to avoid exceptions in simple for loops in JAVA? Apr 26, 2023 pm 12:58 PM

Introduction In actual business project development, everyone should be familiar with the operation of removing elements that do not meet the conditions from a given list, right? Many students can immediately think of many ways to achieve it, but are all the ways you think of harmless to humans and animals? Many seemingly normal operations are actually traps, and many novices may fall into them if they are not careful. If unfortunately, an exception is thrown and an error is reported when the code is running, this is a blessing. At least the code can be discovered and solved in time without an error being reported, but various strange problems appear inexplicably in the business logic. This is more tragic. Because if you don't pay attention to this problem, it may cause hidden dangers for subsequent business. So, what are the implementation methods? What implementations might

6 examples, 8 code snippets, detailed explanation of For loop in Python 6 examples, 8 code snippets, detailed explanation of For loop in Python Apr 11, 2023 pm 07:43 PM

Python supports for loops, and its syntax is slightly different from other languages ​​(such as JavaScript or Java). The following code block demonstrates how to use a for loop in Python to iterate over the elements in a list: The above code snippet prints three letters in separate lines. You can limit the output to the same line by adding a comma "," after the print statement (if you specify a lot of characters to print, it will "wrap"), the code is as follows: When you want to display it in one line instead of multiple lines For text content, you can use the above form of code. Python also provides built-in

See all articles