Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug vs. Release performance

I've encountered the following paragraph:

“Debug vs. Release setting in the IDE when you compile your code in Visual Studio makes almost no difference to performance… the generated code is almost the same. The C# compiler doesn’t really do any optimization. The C# compiler just spits out IL… and at the runtime it’s the JITer that does all the optimization. The JITer does have a Debug/Release mode and that makes a huge difference to performance. But that doesn’t key off whether you run the Debug or Release configuration of your project, that keys off whether a debugger is attached.”

The source is here and the podcast is here.

Can someone direct me to a Microsoft article that can actually prove this?

Googling "C# debug vs release performance" mostly returns results saying "Debug has a lot of performance hit", "release is optimized", and "don't deploy debug to production".

like image 601
sagie Avatar asked Mar 15 '10 09:03

sagie


People also ask

How much faster is release than debug?

But the reality is debug build is much faster than release build. The release build normally takes more than 0.30ms, while the debug build takes under 0.3.

Is debug slower than release?

Release mode is way more slower than debug mode for user defined functions.

What is the difference between debug and release?

By default, Debug includes debug information in the compiled files (allowing easy debugging) while Release usually has optimizations enabled. As far as conditional compilation goes, they each define different symbols that can be checked in your program, but they are language-specific macros.

What is the difference between debug and release build in Visual Studio?

Visual Studio projects have separate release and debug configurations for your program. You build the debug version for debugging and the release version for the final release distribution. In debug configuration, your program compiles with full symbolic debug information and no optimization.

What is the difference between debug and release?

As far as I know, the only difference between these configurations if you don't change it manually is that Debug have the DEBUG constant defined, and Release have the Optimize code checked of. Are there much performance differences between these two configurations.

Why is it so hard to debug a release build?

These bugs are EXTREMELY difficult to debug (by definition of Release mode, ironically). Still its a problem if the application is tested and approved with the Debug settings, even if it suppress errors, if that causes the release build to fail during deployment.

Why do debug builds have more variables than normal builds?

Since you probably have a lot more variables than CPU registers, this means the debug compiler will emit instructions to copy those values to RAM. While in a release build, if the value isn't used again, the optimizer will just throw it away. Show activity on this post. In general, it depends.

Why are there bugs in release version not present in debug?

Since the IL (intermediate language) is optimized in Release mode, there exists the possibility of bugs that would not have manifested in Debug mode. There are other SO questions covering this problem: Common reasons for bugs in release version not present in debug mode


2 Answers

Partially true. In debug mode, the compiler emits debug symbols for all variables and compiles the code as is. In release mode, some optimizations are included:

  • unused variables do not get compiled at all
  • some loop variables are taken out of the loop by the compiler if they are proven to be invariants
  • code written under #debug directive is not included, etc.

The rest is up to the JIT.

Full list of optimizations here courtesy of Eric Lippert.

like image 74
Adrian Zanescu Avatar answered Oct 18 '22 02:10

Adrian Zanescu


There is no article which "proves" anything about a performance question. The way to prove an assertion about the performance impact of a change is to try it both ways and test it under realistic-but-controlled conditions.

You're asking a question about performance, so clearly you care about performance. If you care about performance then the right thing to do is to set some performance goals and then write yourself a test suite which tracks your progress against those goals. Once you have a such a test suite you can then easily use it to test for yourself the truth or falsity of statements like "the debug build is slower".

And furthermore, you'll be able to get meaningful results. "Slower" is meaningless because it is not clear whether it's one microsecond slower or twenty minutes slower. "10% slower under realistic conditions" is more meaningful.

Spend the time you would have spent researching this question online on building a device which answers the question. You'll get far more accurate results that way. Anything you read online is just a guess about what might happen. Reason from facts you gathered yourself, not from other people's guesses about how your program might behave.

like image 39
Eric Lippert Avatar answered Oct 18 '22 02:10

Eric Lippert