Why CSC doesn't optimize this?

the code as this:




using System;
class test
{
 public static void Main( )
 {
  int b = 0;

  DateTime t1 = DateTime.Now;
  int c = 1000000000;
  for(int i = 0; i < c; ++i)
   ++b;
  TimeSpan ts1 = DateTime.Now - t1;
  Console.WriteLine(ts1.Milliseconds);

 }
}

 



the time is about 870 on my computer.
if c = 1000 the output is 0
if c = 100000000 the output may 93

so,that shows that the loop dos not be optimized.
in VC++(release) the b can be optimied to a const,but why
CSC not do this

This sample is simple, but what about complicated problem

what whould I do to optimize my code.....




Answer this question

Why CSC doesn't optimize this?

  • zookeeper

    actually,I am not worray about the loop,I am warray about the needless-waste of performance of CS.Maybe the worry is unnecessary,but I'm working with it.... hope cs can be better...

    oh.and,thank a lot.



  • Moogy

    The C# compiler does very little optimization at all. It leaves most of it to the JIT compiler. So a better question would be to ask why the JIT compiler doesn't optimize it.


    >This sample is simple

    It's also pretty unrealistic, I don't see why you would write code like that. Wouldn't it be more interesting to compare perf of code that actually does something useful


  • ebailey

    Compiler optimization is a tricky thing.  Not only must it generate faster code (or else what is the use) but it also can't change the semantics of the original code and it can't take too long to run.  In the case you gave I could see where the compiler could optimize the code but I don't see that it would be worth the effort needed to actually check for that case.  In order for the compiler to optimize the for loop (it can't optimize the rest of the code) the following conditions would have to hold:

    1. The loop invariant must be defined within the loop construct (as it is here) and not changed inside the loop (except in the loop header) nor its address assigned to any value that might be accessible outside the loop.  Alternatively it could be defined as a local var outside the loop but then there are issues if the value is changed and therefore not even worth pursuing.
    2. The loop condition can not change during the iteration of the loop.
    3. There must be no premature exits from the loop nor any method or property calls.
    4. There can be no change in state within the loop.  For example you can't create a var inside the loop.  Creation of such a var would have a side effect which could not be optimized away.

    There are probably more but those are what come to mind.  The first case is not too hard to check nor is the second.  However once you get into having to analyze the statements within the for-loop the performance is going to start going down hill.  Since a loop optimization like you would like to see is so picky and the odds of the compiler actually finding a for-loop that meets those requirements is so low there isn't much of a benefit to trying to make the optimization.  To be fair the C++ compiler has been around for over a decade and therefore has had some optimizations added over time.  Perhaps one day the C# compiler will try to optimize this case but for now it doesn't. 

    For for-loops compilers normally do the following optimizations:

    1. Loop unrolling - Unrolls the loop, or a portion of the loop, to optimize the number of jumps that must occur.  Very common and simple optimization.
    2. Loop-invariant factoring - Removes any complex, const expressions from the inside the loop and moves them outside the loop.  This includes the conditional check if appropriate.  For example the following loop:

    int a = 3;
    int b = 4;
    for (int i = 0; i < a + b; ++i)
    {
       ACoffee = 4 + a + b;
    }

    can be refactored to:
    int a = 3;
    int b = 4;
    int tmp = a + b;
    for (int i = 0; i < tmp; ++i)
       ACoffee = 4 + tmp;  //Or even 11

    3.Dead-code removal - Uncommon but very easy to detect.  This occurs when a block of code won't execute at all.  In this case it can be safely removed.  An example is an IF statement that is always true.  The IF condition can be removed.  If the IF statement is always false then the entire block can be removed.

    There are others but these are trivial compiler optimizations.  Whether C# does it or not I'm not sure.  Also be aware that if your vars are const (i.e. const int i = 1000000) then the compiler can optimize even further under normal circumstances.

    As someone already mentioned the C# compiler optimizes some but MS recommends not doing too much and letting the JIT optimize the code for the target platform.  Although the JIT can't do too many aggressive optimizations due to time constraints it can still do some of the common ones and is where you'll probably see the most optimizations.

    Michael Taylor - 10/20/05

  • Arieth

    yeah, why JIT doesn't optimize it

    and, you known, some time the useful code human maybe  cann't find out whether the compiler opt or not. Although it's very simple and useless, we can easily find that compiler(csc or JIt both) does not optimize it, so I afraid that compiler does not optimize other usefull-code either.


  • Why CSC doesn't optimize this?