none
Performance of loops RRS feed

  • Question

  • I study Computer Science, and thus often need fast application execution. .NET delivers that (I like your new Nummerics namespace very much). But during development of a barebones application in C# i noticed very slow execution of loops (for, while) compared to Java.

    I understand, that C# is not intendet for such low-level use, but then, neither is Java.

    Here is an example:

    for (int i = 0; i < 2147483647; i++)
    {
    }
    
    The code will run in both Java and C#, but Java will execute the loop in ~3.5 sec, while .NET takes ~8 sec. That is about 2.3 times slower. This will quickly add up in algorithms, making Java superior. Using a decomplier I verified, that neither Java nor .NET optimizes the code in any way.

    The questions is, why is .NET slower and how can I improve the performance?

    Alexei Mihalchuk
    Friday, November 20, 2009 3:56 PM

Answers

  • C# actually does do quite a few optimizations for loops, but it does them in the JIT compiler.

    A couple of things when you're doing this type of test.

    1) Run your sample 2 (or more) times in the code.  The first time, you'll have some overhead due to the JIT optimizations turning on.
    2) Make sure, in both cases, you're running with optimizations enabled (Release mode build, by default in C#), and that you are not running inside of the debugger or inside of the Visual Studio testing host process.  This typically means building in Release, and then using Ctrl+F5 instead of just F5 to start the application (or running from a command line). 

    You'll find that, for simple loops, C# makes many optimizations.  Your loop is a bit contrived, so it may not hit the optimizations, but even simple things like this will:

    int[] arr = new int[1000000];
    for (int i=0;i<arr.Length;++i)
    {
        arr[i] = 3*i;
    }

    As an example, in the above case, bounds checking will be removed from the array accesses.


    Given your timings, I'm guessing point #2 above is the problem.  For example, on my system, if I run this under the debugger, I get 5.2 seconds, then 5.1 seconds on the second run.  If I switch to release and run outside of the test host, it changes to 1.3 sec both times.  Given that you're taking 8 seconds, it sounds like you're running under the debugger, which disables every optimization and slows things down dramatically.

    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, November 20, 2009 7:38 PM
    Moderator

All replies

  • C# actually does do quite a few optimizations for loops, but it does them in the JIT compiler.

    A couple of things when you're doing this type of test.

    1) Run your sample 2 (or more) times in the code.  The first time, you'll have some overhead due to the JIT optimizations turning on.
    2) Make sure, in both cases, you're running with optimizations enabled (Release mode build, by default in C#), and that you are not running inside of the debugger or inside of the Visual Studio testing host process.  This typically means building in Release, and then using Ctrl+F5 instead of just F5 to start the application (or running from a command line). 

    You'll find that, for simple loops, C# makes many optimizations.  Your loop is a bit contrived, so it may not hit the optimizations, but even simple things like this will:

    int[] arr = new int[1000000];
    for (int i=0;i<arr.Length;++i)
    {
        arr[i] = 3*i;
    }

    As an example, in the above case, bounds checking will be removed from the array accesses.


    Given your timings, I'm guessing point #2 above is the problem.  For example, on my system, if I run this under the debugger, I get 5.2 seconds, then 5.1 seconds on the second run.  If I switch to release and run outside of the test host, it changes to 1.3 sec both times.  Given that you're taking 8 seconds, it sounds like you're running under the debugger, which disables every optimization and slows things down dramatically.

    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, November 20, 2009 7:38 PM
    Moderator
  • This is actually a bit embarrassing. :P I've always used to change to Release, but for some reason, I completely forgot about it today. Guess it's Friday. :D

    About the execution, I did run the code outside the debugger. But I am quite surprised about the difference between debug and release. Release is about 5 to 6 times faster. Did expect some improvements, but not that much. :D

    Now the code runs faster than Java! :D

    Thanks for help and have a great weekend,
    Alexei Mihalchuk
    Friday, November 20, 2009 8:49 PM