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.