.NET发布版本的工作比调试慢版本、工作、NET

2023-09-05 00:14:51 作者:千喜i

我经历了最不可思议的事情,在过去几天中。我发现我的发布版本实际上执行慢比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。问题

我终于在我的Windows窗体的exe剥夺了所有的东西从我的入口点(主),只留下这样的:

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());
 }

所以,我其实并没有实例化任何形式了,只测试。 的TestClass 是一个小班,只有三所公立 INT 性质,别无其他。我的主要.EXE(Windows窗体)是〜1Mb的大,如果说有什么区别。

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。结果

在调试模式下,我经过时间为200毫秒〜,而将其释放大约需要1.2秒。

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

3。附加信息

但奇怪的是,当我尝试设置在该解决方案的启动项目的一些其他的项目,因为在这种情况下,它的工作速度快(如出一辙code如上)。

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。快速破解

要修复这个bug尽快,我在我的解决方案,它实例通过参考我的第一项项目运行的主要申请表创建一个新的.exe文件的启动项目。在它的工作原理再次快速这种情况下,我进入EXE现在只有24KB大,只包含一个静态的主要方法。

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.

有没有人遇到过类似的行为?如果我在这个别的地方迷迷糊糊地,通过查看code以上,我可能会presume有一个静态初始化的地方,做吨的工作在一个单独的线程(但不是这里的情况,我没有这方面的东西),此外仅在发布版本运行?

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?

更多信息:据我所知,XmlSerializer的产生IL code在运行时,但我的实际问题是,为什么它的工作原理慢在这种情况下比其他情况下。当我只有基准的实际系列化,它是慢3倍的发行(但只有当我从最初的项目运行它)。

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).

[更新] 现在永远最怪异的一部分:经过几次修改/重建的步骤,我的新进入的项目开始表现为第一个 - 启动速度慢,加载速度慢。我改变项目名称和GUID和重建它,它的速度快工作了。

[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.

推荐答案

我觉得可能这是因为XmlSerializer的确实在启动时NGEN上下的工作,发布模式,而不是在调试模式。见例如,

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

有关的一些细节。