Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:58:14+00:00 2026-05-27T23:58:14+00:00

Out of interest, I tested if there was any difference to a for loop

  • 0

Out of interest, I tested if there was any difference to a for loop and a while loop doing the same thing. What causes the while loop to take about 2-2.5 seconds longer on my computer (AMD Phenom II X6 1090T @ 3.20GHz) than the for loop? Aren’t they doing the same thing? Do you get similar results?

Also, when I replace the x = null; statement from the loops with just an empty statement, the while loop will be significantly faster. What’s going on here?

Sure, the number of iterations is pretty high, but isn’t the difference still pretty significant?

static void Main(string[] args)
{
    String x;
    const Int64 FIVE_BN = 5000000000;
    Int64 i = 0;

    DateTime start = DateTime.Now;
    for (; FIVE_BN > i; i++)
        x = null; //Replace with only ; in both loops and the for loop is faster
    Console.Out.WriteLine(FIVE_BN.ToString() + " times (for): " + (DateTime.Now - start));

    i = 0;

    start = DateTime.Now;
    while(FIVE_BN > i++)
        x = null; //Replace with only ; in both loops and the for loop is faster
    Console.Out.WriteLine(FIVE_BN.ToString() + " times (while): " + (DateTime.Now - start));

    Console.Read();
    return;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:58:15+00:00Added an answer on May 27, 2026 at 11:58 pm

    While this is entirely a micro optimization that would never be the performance bottleneck. It interesting that the two are actually different, interestingly when you extract methods both the loops with VS2010 I get the following:

    private static String forLoop(ref Int64 i)
    {
        String x;
    
        for (; FIVE_BN > i; i++)
            x = null; //Replace with only ; in both loops and the for loop is faster
        return x;
    }
    
    private static void whileloop(ref String x, ref Int64 i)
    {
        while (FIVE_BN > i++)
            x = null; //Replace with only ; in both loops and the for loop is faster
    }
    

    And that is quite interesting… it shows that the two functions are indeed different.

    now when we replace the logic in the loop with ; we get the following extracted methods instead:

    private static Int64 forLoopShort(Int64 i)
    {
    
        for (; FIVE_BN > i; i++)
            ; //Replace with only ; in both loops and the for loop is faster
        return i;
    }
    
    private static Int64 whileLoopShort(Int64 i)
    {
    
        while (FIVE_BN > i++)
            ; //Replace with only ; in both loops and the for loop is faster
        return i;
    }
    

    Which indicates why the loops run basically the same with this configuration.

    To work out how they’re different when inline (and not extracted into methods) we need to see what the optimized CLR coded looks like (though the optimizer might actually remove any significant differences between the two functions).. That’s something for a later edit.

    Edit:

    The CIL reveals the differences:

    The For loop has .maxstack 2 but the while loop has .maxstack 4, otherwise there’s a little diference in the order of operations due to the fact that the increment for the while happens at the start of the loop but the for operation happens at the end of the loop (change the content of the loop to Console.WriteLine(i) and see that the While loop will print from 1 but the For loop will print from 0 (both do the same number of loop iterations though).

    When the loop contents is just a ; both loops are 2 lines shorter in CIL with the following lines removed (for both loops):

    IL_0006:  ldnull
    IL_0007:  stloc.0
    

    However when we build in release the code is very different:

    The difference between x = null; and ; is nothing, for either of the loops, as the optimizer has noticed that the value never changes to being not-null.

    The difference between the optimised for and while loops are as follows:

    CIL for loop:

    IL_0000:  ldc.i4.0
    IL_0001:  conv.i8
    IL_0002:  stloc.0
    IL_0003:  br.s       IL_000a
    IL_0005:  ldloc.0
    IL_0006:  ldc.i4.1
    IL_0007:  conv.i8
    IL_0008:  add
    IL_0009:  stloc.0
    IL_000a:  ldc.i8     0x12a05f200
    IL_0013:  ldloc.0
    IL_0014:  bgt.s      IL_0005
    IL_0016:  ret
    

    And CIL while loop:

    IL_0000:  ldc.i4.0
    IL_0001:  conv.i8
    IL_0002:  stloc.0
    IL_0003:  ldc.i8     0x12a05f200
    IL_000c:  ldloc.0
    IL_000d:  dup
    IL_000e:  ldc.i4.1
    IL_000f:  conv.i8
    IL_0010:  add
    IL_0011:  stloc.0
    IL_0012:  bgt.s      IL_0003
    IL_0014:  ret
    

    So we can see that an optimised while loop is faster than a for loop by 2 operations, however it uses more stack space.

    The difference between these two seems entirely related to the difference in where the i++ occurs.

    Indeed this is confirmed by making a new method:

    private static void forLoopVeryShort()
    {
        string x;
        Int64 i = 0;
    
        for (; FIVE_BN > i++;)
            ; //Replace with only ; in both loops and the for loop is faster
    }
    

    The CIL code for this for method when built (in either release or debug) is identical to that of the while loop.

    There in lies your difference. For loops perform exactly the same as while loops when they’re doing the exact same behaviour. The difference you noted is entirely due to running the code in debug and not release, combined with the JIT not always being as efficient as the release code optimizer.

    I have enjoyed this question, I learnt something out of it; I hope others do as well. +1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just out of interest, are there any easy to setup and work with code
Out of interest and because it infuriates me, I was wondering if SOmebody here
More out of interest than anything else, but can you compile a DirectX app
This question is just out of interest, and perhaps could be useful for my
I'm trying to answer the following question out of personal interest: What is the
Out of interest - Does a default exist or is it different on each
This question is mostly just out of academic interest. I started using YUI 3
I am just asking this out of interest, I did a Google search and
I made this program just out of interest and wanted to make it better.
I have just started to learn Haskell out of interest. I follow learnyouahaskell.com .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.