Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Release build working slower than Debug

I am experiencing a weirdest thing for the last couple of days. I found out that my Release build actually executes slower than the Debug version.

1. The problem

I finally stripped all stuff from my entry point (Main) in my Windows Forms exe, leaving only this:

 [STAThread]
 static void Main(params string[] args)
 {
     Stopwatch sw = Stopwatch.StartNew();
     System.Xml.Serialization.XmlSerializer xmlS =
         new System.Xml.Serialization.XmlSerializer(typeof(TestClass));
     sw.Stop();
     MessageBox.Show(sw.Elapsed.ToString());
 }

So I am actually not instantiating any Forms anymore, only testing. TestClass is a small class with only three public int properties and nothing else. My main .exe (Windows Forms) is ~1Mb large, if that makes any difference.

2. Results

In Debug mode, my Elapsed time is ~200ms, while in Release it takes ~1.2s.

3. Additional info

The strange thing is when I try to set some other project in that solution as Startup project, because in that case it works fast (exactly the same code as above).

4. Quick hack

To fix this bug as quickly as possible, I created a new .exe Startup project in my solution, which instantiates and runs the main Application Form by referencing my first entry project. In that case it works fast again, my entry exe is now only 24kb large, containing only a static Main method.

Has anyone encountered similar behavior before? If I had stumbled upon this somewhere else, by looking at the code above, I would probably presume that there is a static initializer somewhere, doing tons of work in a separate thread (but it's not the case here, I don't have that stuff), and furthermore running only in Release build?

[Edit] More info: I am aware that XmlSerializer generates IL code in runtime, but my actual question is why it works slower in this case than in other cases. When I benchmark only the actual serialization, it is 3x slower in Release (but only if I run it from my initial project).

[Update] Now for the weirdest part ever: After a couple of modify/rebuild steps, my new entry project started to behave as the first one - slow startup, slow loading. I changed the project name and GUID and rebuilt it and it's working fast again.

like image 504
Groo Avatar asked Apr 21 '09 09:04

Groo


People also ask

Is release build faster than debug?

Debug Mode: In debug mode the application will be slow. Release Mode: In release mode the application will be faster. Debug Mode: In the debug mode code, which is under the debug, symbols will be executed. Release Mode: In release mode code, which is under the debug, symbols will not be executed.

Is debug slower than release?

It is well-known that Debug build in Visual C++ is very slow compared to Release build, with the typical ratio about 10-20 times. Various reasons behind it are often stated, and some people even believe that it is inevitable because it is caused by lack of compiler optimizations.

Can you debug in release mode?

You can now debug your release build application. To find a problem, step through the code (or use Just-In-Time debugging) until you find where the failure occurs, and then determine the incorrect parameters or code.

What is the difference between release and debug 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.


1 Answers

I think possibly this is because the XmlSerializer does NGEN-ish work at startup time in Release mode, but not in Debug mode. See e.g.

http://blogs.msdn.com/billwert/archive/2008/02/23/use-of-sgen-exe-to-avoid-common-xmlserializer-performance-pitfalls.aspx

for some details.

like image 160
Brian Avatar answered Sep 28 '22 03:09

Brian