简体   繁体   English

如何基于配置文件优化应用程序的性能?

[英]How can I optimize the performance of my application based on a profile?

I have profiled my application and found out that not my functions are causing the delay I see but the winform functions. 我已经分析了我的应用程序,发现不是我的功能导致了我看到的延迟,而是Winform函数。 How can I correct this? 我该如何纠正? For explanation see this: 有关说明,请参见:

And this is the result of the profiling: 这是分析的结果:

You can't fix that. 您无法解决该问题。

The framework is calling down to the DispatchMessage function exposed by the Windows API, which is used to dispatch a message retrieved by a call to the GetMessage function to the window procedure for a particular window. 该框架正在调用Windows API公开的DispatchMessage函数 ,该函数用于将对GetMessage函数的调用所检索的消息调度到特定窗口的窗口过程。

The "slow" part here is Windows itself. 这里的“慢”部分是Windows本身。 When that becomes your bottleneck, your application is sufficiently optimized, and there's nothing more you can do. 当这成为瓶颈时,您的应用程序已得到充分优化,您无能为力。

Also, those profile results aren't necessarily telling you that this function is slow . 此外,这些配置文件结果不一定告诉您此功能很 Rather, they're telling you that it gets called a lot ("Hit Count"). 相反,他们告诉您它被称为很多(“命中计数”)。 The idea is that functions that get called a lot are "hot points" in your code, and it's worth taking some extra time to optimize their implementation (more bang for your buck). 这个想法是,被大量调用的函数是代码中的“热点”,值得花一些额外的时间来优化它们的实现(更多的花销)。 In this case, though, that function gets called a lot because it's how Windows processes messages for your app. 但是,在这种情况下,该函数被称为很多,因为Windows就是如何为您的应用程序处理消息的。 In the world of unmanaged code and the native Windows API, messages are kind of like the events you use in .NET code. 在非托管代码和本机Windows API的世界中,消息有点像您在.NET代码中使用的事件。 Since an event has to be raised for anything interesting to happen, the function that's responsible for calling or dispatching those events (messages) is bound to get called a lot. 由于必须引发一个事件才能发生任何有趣的事情,因此负责调用或调度这些事件(消息)的函数必然会被调用很多。

Windows apps generally contain a top-level loop where they wait for external events like mouse movements/clicks and keyboard hits, or internally generated events. Windows应用程序通常包含一个顶级循环,在其中等待外部事件(例如鼠标移动/点击和键盘点击)或内部生成的事件。 When an event occurs, it calls the appropriate handler, which may do a little or a lot. 当事件发生时,它会调用适当的处理程序,该处理程序可能做的很少或很多。 Typically it walks a pretty extensive and deep call tree, but if it finishes quickly it just goes back to waiting. 通常情况下,它会走很宽的深度调用树,但是如果快速完成,它将回到等待状态。

An app that appears to perform well is spending most of the wall-clock time waiting for the next external event. 表现良好的应用正在花费大部分时间,等待下一个外部事件。

An app that appears to perform poorly is spending most of it's time walking call trees in response to events. 表现不佳的应用程序大部分时间都花在了响应事件上的呼叫树上。

The way to improve its performance is to locate bottlenecks and remove them. 改善其性能的方法是找到瓶颈并将其消除。 Bottlenecks nearly always consist of function calls in the call tree, in your code, that you didn't know were expensive. 瓶颈几乎总是由调用树中的函数调用(在您的代码中)组成,而您并不知道这些调用是昂贵的。 Parts of the call tree that are not in your code are things you can't do anything about, but if you can avoid calling them, you have an opportunity to get a speedup. 调用树中不在代码中的部分是您无能为力的事情,但是如果您可以避免调用它们,那么您就有机会获得加速。

Just as if you were a manager trying to see if your employees are wasting time, you can just drop in unannounced and see what they are doing. 就像您是一位经理,试图查看您的员工是否在浪费时间一样,您可以不加通知地看看他们在做什么。 In software, this is how you can do that . 在软件中, 这是您可以执行的操作

Be careful of profilers that confuse you with things like 1) telling you "self time" of routines, 2) telling you how many times a function is called, 3) give you a massive but mostly irrelevant graph or table, or 4) lots of interesting but not usually relevant clues, like cache misses and thread switches. 要小心分析器,这些分析器会使您感到困惑,例如1)告诉例程例程的“自我时间”,2)告诉函数被调用多少次,3)给您大量但几乎不相关的图或表,或4)很多有趣但通常不相关的线索,例如缓存未命中和线程切换。

Finding bottlenecks is very easy, because if they are small they are not really bottlenecks, and if they are big, during the time they are wasting, they are on the stack, just waiting for you to notice. 查找瓶颈非常容易,因为如果它们很小,那么它们并不是真正的瓶颈;如果它们很大,那么在浪费的时间里,它们就在堆栈中,只是在等您注意。 Here's more on that subject. 关于这个主题的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM