启动 VS Profiler 可将应用程序性能提高 20 倍?

发布于 2024-10-27 02:31:05 字数 1448 浏览 1 评论 0原文

编辑1
我完全不排除这可能是由使用探查器的一些非常基本的副作用引起的(我的“常规”项目中的一些错误设置)

我想改善我的应用程序中的计算时间,所以我决定进行彻底的概要分析。 因此,我刚刚启动了 .Net 内存分配分析来分析我的应用程序。
看到计算速度快了 20 倍,我完全惊呆了!

应用程序包括使用BackgroundWorkers 从二进制文件读取数据、处理它们,并将结果存储到MSSQL 数据库中。每轮通常需要 20 秒,而分析时几乎只需要 1 秒。我检查并确保两种情况下的结果都是一致的。

一位尝试过 .Net 的朋友告诉我,探查器优化了线程,并“以某种方式”找到了通过线程锁和瓶颈的方法,但我简直不敢相信。

所以我的问题是:

  1. 究竟发生了什么、如何发生以及为什么发生?
  2. 如何使我的代码本身表现得像那样?

编辑2
知道这听起来疯狂且难以置信。我自己还是很怀疑。但这是真的。
当分析器运行时,相同的代码运行得非常快。 我使用相同的测试数据,并观察相同的计算输出。 我无法给出一个简单的复制项目,因为它是一个相对较大的框架。 我正在使用 Visual Studio 2010 探查器。

我将在流程上提供尽可能多的详细信息,并且一旦找到线索,我肯定会立即发布线索。

常规运行日志:
2011 年 3 月 23 日 18:04:34 | 180434.621 |模拟设置 [5]-[1]-[5 PC-1 0 [设置 1/48]
03/23/2011 18:05:01 | 180501.271 | 处理时间:00:00:26.6515244
等等..

探查器运行日志:
2011 年 3 月 24 日 11:38:15 | 113815.592 |模拟设置 [5]-[1]-[5 PC-1 0 [设置 1/48]
2011 年 3 月 24 日 11:38:17 | 113817.350 | 处理时间:00:00:01.7581005

等等..

编辑3:线索
好吧好吧好吧我的坏处(我在编辑 1 上警告过这种可能性,因为这太令人难以置信了。抱歉)@Watts 建议检查我是否处于调试或发布模式。 我已经完成了。但是@SnowBear指出有两种不同的事情:运行软件的调试版本和在调试器下运行软件 我确保活动配置在构建和执行中都是 RELEASE 是 VS2010。然而,由于我简直要疯了,我决定直接从 bin/release 中的 exe 文件启动应用程序。瞧……每个进程花费 1 秒。 运行探查器可以让您退出调试模式(无论您处于发布模式还是调试模式) 这就是让我陷入困惑的原因。
感谢所有案件已结。

EDIT 1
I do not exclude at all this might be caused by something very basic side effect of using the Profiler (some faulty setting in my "regular" project)

I wanted to improve Computing Time in my Application, so I decided to go through a thorough profiling analysis.
So I just launched a .Net Memory Allocation Profiling to analyze my App.
I was completely stunned to watch computing go x20 times faster !

Application consists of Reading Data from Binary Files with BackgroundWorkers, process them,
and store results into an MSSQL DB. Each round usually takes 20 seconds, and while profiling it barely takes 1 sec. I checked and made sure results are coherent in both cases.

A .Net experimented friend told me the profiler optimizes threading and "somehow" finds its way through thread Locks and Bottlenecks, but I just can't believe it.

So my questions are :

  1. WHAT EXACTLY HAPPENS, HOW, and WHY ?
  2. How to make my code behave like that natively ?

EDIT 2
I KNOW this sounds crazy and unbelievable. I myself am still very suspicious.But it's TRUE.
The SAME code runs really fast when run by the profiler.
I use the SAME test data, and watch the SAME computing output.
I am not able to give out a simple reproducing project, as it is a relatively big framework.
I am using the Visual Studio 2010 Profiler.

I'll be giving as much details as I can on the flow and definitely will post a clue as soon as I find it.

Regular RUN LOGS :
03/23/2011 18:04:34 | 180434.621 | SIMULATING SET [5]-[1]-[5 PC-1 0 [SET 1/48]
03/23/2011 18:05:01 | 180501.271 | PROCESSING TIME : 00:00:26.6515244

etc..

Profiler Run LOGS :
03/24/2011 11:38:15 | 113815.592 | SIMULATING SET [5]-[1]-[5 PC-1 0 [SET 1/48]
03/24/2011 11:38:17 | 113817.350 | PROCESSING TIME : 00:00:01.7581005

etc..

EDIT 3 : The Clue
Ok ok ok My BAD(I warned on such a possibility on edit 1, as this was too unbelievable. Sorry) @Watts suggested to check if I was in debug or release mode.
Which I had Already done. BUT @SnowBear pointed out that there are two different things : to run debug version of software and to run a software under debugger
I made sure that the active configuration was RELEASE in both Build And Execution is VS2010. However, as I was just going nuts I decided to launch the Application directly from the exe file in the bin/release. And voilà... processes were taking 1 second each.
Running the Profiler get you out of debug mode (whether you're in release or debug mode)
that's what put me into confusion.
Thanks to All Case Closed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

药祭#氼 2024-11-03 02:31:05

使用调试器运行会禁用 jit 优化。如果正常运行 exe,jit 优化将会启用。将调试器附加到此类正在运行的应用程序允许您通过启用的优化来调试它。

Release-Build 与 Debug-Build 有两个结果:

  1. 条件编译器符号被(未)定义
  2. 它启用/禁用 C# => 中的优化IL编译。

Running with the debugger disables jit optimizations. If you run the exe normally jit optimizations will be enabled. Attaching a debugger to such a running application allows you to debug it with enabled optimizations.

Release-Build vs Debug-Build has two consequences:

  1. A conditional compiler symbol is (un)defined
  2. It enables/disables optimizations in the C# => IL compilation.
最单纯的乌龟 2024-11-03 02:31:05

我已经遇到过很多次了......对此有一个非常简单的解释。分析器不会处置对象,因此分析时不会产生对象处置的成本。

因此,如果您想提高性能以使其与分析的性能相匹配,请找出实例化所有这些短期对象的位置,然后重构代码。

我还不知道有什么好方法可以立即找到有问题的代码,但我可以帮助您缩小范围。如果您分析代码,打开报告,选择“函数”作为当前视图,然后按包含示例排序,您将看到最重要的方法...对象实例化的性能问题可能出在其中之一具有最具包容性样本的方法。

I've come across this many times... There is a very simple explanation for it. The profiler doesn't dispose objects, so the cost of object-disposal is not incurred while profiling.

Thus, if you want to improve performance to make it match the profiled performance, figure out where you're instantiating all these short-lived objects, and refactor the code.

I don't yet know of a really great way to immediately find the offending code, but I can help you narrow it down. If you profile your code, open the report, select "Functions" as your Current View, and then sort by Inclusive Samples, you will see the top methods... Your performance problem with object-instantiations is likely to be in one of those methods with the most Inclusive Samples.

烟花易冷人易散 2024-11-03 02:31:05

如果您在代码中使用线程,Windows 计时器分辨率也是造成这种情况的一个原因。

默认 Windows 定时器分辨率为 15.6ms

当您使用探查器运行应用程序时,该分辨率设置为 1 毫秒,并且应用程序运行得更快。查看这个答案

If you are using threads within your code, the windows timer resolution is also a reason for this.

Default windows timer resolution is 15.6ms

When you run your application with the profiler, this is set to 1ms and the application runs faster. Checkout this answer

度的依靠╰つ 2024-11-03 02:31:05

我不知道为什么会发生这种情况。我很想说只有微软知道,但实际上,我什至对此表示怀疑。不过,我可以告诉您一个可能的修复方法:

转到项目属性的“调试”选项卡,并确保“启用非托管代码调试”未选中

I do not know exactly why this is happening. I would be tempted to say that only Microsoft knows, but actually, I doubt even that. However, I can tell you one possible fix:

Go to the "Debug" tab of your project properties and make sure that "Enable unmanaged code debugging" is unchecked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文