title: .Net Release build working slower than Debug
body: <p>I am experiencing a weirdest thing for the last couple of days. I found out that my Release build actually executes <strong>slower</strong> than the Debug version.</p>
  
  <p><strong>1. The problem</strong></p>
  
  <p>I finally stripped all stuff from my entry point (Main) in my Windows Forms exe, leaving only this:</p>
  
  <pre><code> [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());
   }
  </code></pre>
  
  <p>So I am actually not instantiating any Forms anymore, only testing. <code>TestClass</code> is a small class with only three public <code>int</code> properties and nothing else. My main .exe (Windows Forms) is ~1Mb large, if that makes any difference.</p>
  
  <p><strong>2. Results</strong></p>
  
  <p>In Debug mode, my Elapsed time is ~200ms, while in Release it takes ~1.2s.</p>
  
  <p><strong>3. Additional info</strong></p>
  
  <p>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).</p>
  
  <p><strong>4. Quick hack</strong></p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p><strong>[Edit]</strong> 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).</p>
  
  <p><strong>[Update]</strong> 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.</p>