0

So if i just want to see what JIT compiles while debuggin I can go into Debug -> Windows -> Disassembly and watch everything I want to. But JIT doesn't generate optimized code when executing under debugger. For example:

    private static void Foo()
    {
        var sw = Stopwatch.StartNew();
        for (int i = int.MinValue; i < int.MaxValue; i++) {}
        sw.Stop();
        Console.WriteLine(sw.Elapsed);
    }

this code takes 12 seconds (on my i7-3770) when is running under debugger but only 1 second if isn't.

So it's quite initeresting: if cycle isn't removed from the code - why it runs 10 times faster. And if it's removed (because of code uselessness), why it takes this second, not several ticks?..


What is the question?

How to see final x86 optimized code without using NGen ?

Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
  • 1
    1) What is the question? 2) This is completely normal that attaching the debugger and creating it's address map takes much longer than running the compiled code 3) When the code is compiled, that kind of useless loop WILL be deleted. – Kamil T May 28 '14 at 13:33
  • If you want to see what JIT produces in release mode, don't use the debugger to do it. – Panagiotis Kanavos May 28 '14 at 13:36
  • I got 9.5 seconds (debug) and 8.8 seconds (release) when run under VS in an ASP.NET application. This is on a Xeon processor. I'm not sure why you're seeing a bigger difference, unless the loop is being optimized out - or why it's not on my setup. – David Crowell May 28 '14 at 13:36
  • Running as a console application in release, it does show up at 1.1 seconds. – David Crowell May 28 '14 at 13:39
  • Additional info: debug mode uses `clt` while release mode uses `blt.s`. Other than that just a few `nop`s. Related: http://stackoverflow.com/questions/21438751/c-sharp-compiler-optimizations – Caramiriel Jan 06 '16 at 20:19

1 Answers1

0

So, answer is pretty simple, You should just uncheck Suppress jit optimization on module load checkbox in Visual Studio and you will be able to see optimized code, but be careful, because in this mode some breakpoints would never hit. For example if method was inlined it will be never called thereafter breakpoints within won't work.

Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151