什么是非常简单的 C++ 分析器 (VC++)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2624667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's a very easy C++ profiler (VC++)?
提问by Mr. Boy
I've used a few profilers in the past and never found them particularly easy. Maybe I picked bad ones, maybe I didn't really know what I was expecting! But I'd like to know if there are any 'standard' profilers which simply drop in and work? I don't believe I need massively fine-detailed reports, just to pick up major black-spots. Ease of use is more important to me at this point.
我过去使用过一些分析器,但从未发现它们特别容易。也许我选择了不好的,也许我真的不知道我在期待什么!但我想知道是否有任何“标准”分析器可以简单地插入并工作?我不相信我需要大量详细的报告,只是为了找出主要的黑点。在这一点上,易用性对我来说更重要。
It's VC++ 2008 we're using (I run standard edition personally). I don't suppose there are any tools in the IDE for this, I can't see any from looking at the main menus?
我们使用的是 VC++ 2008(我个人运行标准版)。我认为 IDE 中没有为此提供任何工具,从主菜单中看不到任何工具?
采纳答案by Brian R. Bondy
VS built in:
VS 内置:
If you have team edition you can use the Visual Studio profiler.
如果您有团队版,则可以使用Visual Studio 探查器。
Other options:
其他选项:
Creating your own easily:
轻松创建自己的:
I personally use an internally built one based on the Win32 API QueryPerformanceCounter. You can make something nice and easy to use within a hundred lines of code or less.
我个人使用基于 Win32 API QueryPerformanceCounter的内部构建的。您可以在一百行或更少的代码中制作出漂亮且易于使用的东西。
The process is simple: create a macro at the top of each function that you want to profile called PROFILE_FUNC() and that will add to internally managed stats. Then have another macro called PROFILE_DUMP() which will dump the outputs to a text document.
过程很简单:在您要分析的每个函数的顶部创建一个宏,称为 PROFILE_FUNC(),并将添加到内部管理的统计信息中。然后有另一个名为 PROFILE_DUMP() 的宏,它将输出转储到文本文档。
PROFILE_FUNC() creates an object that will use RAII to log the amount of time until the object is destroyed. Both the constructor of this RAII object and the destructor will call QueryPerformanceCounter
. You could also leave these lines in your code and control the behavior via a #define PROFILING_ON
PROFILE_FUNC() 创建一个对象,该对象将使用 RAII 来记录该对象被销毁之前的时间量。此 RAII 对象的构造函数和析构函数都将调用QueryPerformanceCounter
. 您也可以将这些行保留在您的代码中并通过#define PROFILING_ON
回答by Michael Myers
I suggest a very simple method (which I learned from reading Mike Dunlavey's posts on SO):
我建议一个非常简单的方法(我从阅读Mike Dunlavey的关于 SO 的帖子中学到的):
Just pause the program.
只需暂停程序。
Do it several times to get a reasonable sample. If a particular function is taking half of your program's execution time, the odds are that you will catch it in the act very quickly.
多做几次以获得合理的样本。如果某个特定函数占用了程序执行时间的一半,则很有可能您很快就会在操作中捕捉到它。
If you improve that function's performance by 50%, then you've just improved overall execution time by 25%. And if you discover that it's not even needed at all (I have found several such cases in the short time I've been using this method), you've just cut the execution time in half.
如果您将该函数的性能提高了 50%,那么您就将整体执行时间缩短了 25%。如果您发现根本不需要它(我在使用这种方法的短时间内发现了几个这样的情况),那么您只是将执行时间减少了一半。
I must confess that at first I was quite skeptical of the efficacy of this approach, but after trying it for a couple of weeks, I'm hooked.
我必须承认,起初我对这种方法的有效性持怀疑态度,但在尝试了几个星期后,我被迷住了。
回答by Matteo Italia
I always used AMD CodeAnalyst, I find it quite easy to use and gives interesting results. I always used the time based profile, in which I found that it cooperates well with my apps' debug information, letting me find where the time is spent at procedure, C++ instruction and single assembly instruction level.
我一直使用 AMD CodeAnalyst,我发现它很容易使用并且给出了有趣的结果。我一直使用基于时间的配置文件,我发现它与我的应用程序的调试信息配合得很好,让我可以找到程序、C++ 指令和单汇编指令级别的时间花费在哪里。