A detailed description of the for vs foreach in C #

Source: Internet
Author: User

Today by accident and advice do not use for, readability is poor performance and low, but I personally think to be based on the specific situation, after all, for in most language keywords, readability is not possible because of a keyword substitution, and most depend on the design and coding habits.
As for performance, it is intended to write a piece of code to test them separately.

var arr = enumerable.range (0, 100000000). ToList (); var sw = Stopwatch.startnew (); for (var i = 0;i < arr. Count; i++) {}sw. Stop (); Console.WriteLine ("For Loop takes:" + SW. elapsedticks); sw = Stopwatch.startnew (); foreach (var x in arr) {}SW. Stop (); Console.WriteLine ("For Loop takes:" + SW. Elapsedticks);


First look at the generated IL

Part of the For loop:

... il_0018:  ldc.i4.0    il_0019:  stloc.2     //iil_001a:  br.s        il_0022il_001c:  nop         Il_ 001D:  nop         il_001e:  ldloc.2     //iil_001f:  ldc.i4.1    il_0020:  add         il_0021:  stloc.2     //iil_0022:  ldloc.2     //iil_0023:  ldloc.0     //arril_0024:  callvirt    system.collections.generic.list<system.int32>.get_countil_0029:  clt         il_002b:  Stloc.s     //cs$4$0000il_002d:  ldloc.s     //cs$4$0000il_002f:  brtrue.s    il_001c ...


In addition to the callvirt in il_0024 will trigger the method virtual table query, there is almost no high consumption of instructions. Then look at the IL portion of the foreach:

 

You can see a number of method calls first, and the method MoveNext is called every time inside the loop body. Finally, you can see that there is an end finally, which means that the loop is wrapped around a layer of try-finally. From IL, foreach is much slower than for.

Now look at the results of stopwatch's execution.

For loop Takes:764538for Loop takes:1311252

For almost twice as fast.

Conclusion: The key word provided by programming language is to solve some kind of problem, it needs to decide the specific business scenario, and it must take out the data to talk about performance.

The above is C #, for vs foreach content, more about topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.