您如何找到程序中最不优化的部分?

时间:2022-09-15 21:41:16

Are there any tools to give some sort of histogram of where most of the execution time of the program is spent at?

是否有任何工具可以为程序的大部分执行时间花费在哪里给出某种直方图?

This is for a project using c++ in visual studio 2008.

这是在visual studio 2008中使用c ++的项目。

5 个解决方案

#1


13  

The name you're after is a profiler. Try Find Application Bottlenecks with Visual Studio Profiler

你追求的名字是一个分析器。尝试使用Visual Studio Profiler查找应用程序瓶颈

#2


6  

You need a profiler.

你需要一个分析器。

Visual Studio Team edition includes a profiler (which is what you are looking for) but you may only have access to the Professional or Express editions. Take a look at these threads for alternatives:

Visual Studio Team版本包含一个分析器(您正在寻找),但您可能只能访问Professional或Express版本。看看这些线程的替代品:

What's your favorite profiling tool (for C++) What are some good profilers for native C++ on Windows?

你最喜欢的分析工具是什么(对于C ++)什么是Windows上原生C ++的优秀分析器?

You really shouldn't optimize ANY parts of your application until you've measured how long they take to run. Otherwise you may be directing effort in the wrong place, and you may be making things worse, not better.

在测量运行所需的时间之前,您真的不应该优化应用程序的任何部分。否则你可能会把工作指向错误的地方,而你可能会让事情变得更糟,而不是更好。

#3


1  

I have used a profiler called "AQ Time" which gives every detail you want to know about about the performance of your code. It's not free though..

我使用了一个名为“AQ Time”的分析器,它提供了您想要了解的有关代码性能的每个细节。虽然它不是免费的..

#4


1  

You could get a histogram of the program counter, but it is practically useless unless you are doing something dumb like spending time in a bubble sort of a big array of ints or doubles.

你可以获得一个程序计数器的直方图,但它实际上是无用的,除非你做一些愚蠢的事情,比如花时间在一大堆的整数或双打中。

If you do something as simple as a bubble sort of an array of strings, the PC histogram will only tell you that you have a hotspot in the string compare routine. That's not much help, is it?

如果你做一些像字符串数组的冒泡那样简单的事情,那么PC直方图只会告诉你字符串比较例程中有一个热点。这没什么帮助,是吗?

I know you wouldn't do such a bubble sort, but just for fun, let's assume you did, and it was taking 90% of your time. (i.e. if you fixed it, it could go up to 10 times faster.)

我知道你不会做这样的冒泡,但只是为了好玩,让我们假设你做了,而且它占用了90%的时间。 (即如果你修好它,它可以快10倍。)

It's actually a very easy thing to find, because if you just hit the pause button in the debugger, you will almost certainly see that it stops in the string compare routine. Then if you look up the stack one level, you will be looking directly at the bubble sort loop which is your bug. If you're not sure you've really spotted the problem, just pause it several times. The number of times you see the problem tells you how costly it is.

这实际上是一个非常容易找到的东西,因为如果你只是点击调试器中的暂停按钮,你几乎肯定会看到它在字符串比较例程中停止。然后,如果你向上查找一个级别,你将直接查看冒泡排序循环,这是你的错误。如果您不确定自己是否真的发现了问题,请暂停几次。您看到问题的次数告诉您它的成本是多少。

Any line of code that appears on the call stack on multiple pauses, is something that is begging you to fix it. Some you can't, like "call _main", but if you can you will get a good speedup, guaranteed.

在多个暂停时调用堆栈上出现的任何代码行都是请求您修复它的内容。有些你不能,比如“打电话_main”,但如果可以的话,你会得到一个很好的加速,保证。

Then do it again, and again.

然后再做一次。

When you run out of things you can fix, then you've really tuned the program within an inch of its life.

当你用完你可以修复的东西时,你就已经在一生的一寸之内调整了程序。

It's that simple.

就这么简单。


You could also use the profiler in Visual Studio. It is a nice tool but be aware of these shortcomings:

您还可以在Visual Studio中使用分析器。这是一个很好的工具,但要注意这些缺点:

  • Confusing you with "exclusive time", which if you concentrate on line-level information, is almost meaningless.

    让您对“独家时间”感到困惑,如果您专注于线路信息,那么几乎毫无意义。

  • If your program is wasting time doing I/O, it won't see that, because when it stops to do I/O, the samples stop, unless you use instrumentation.

    如果您的程序浪费时间进行I / O,则不会看到,因为当它停止进行I / O时,样本会停止,除非您使用检测。

  • But if you use instrumentation, you won't get line-level information, only function-level. That's OK if your functions are all small.

    但是,如果使用检测,则不会获得行级信息,只能获得功能级别的信息。如果你的功能都很小,那就没关系。

  • Confusing you with the "call tree". What matters for a line of code is how many stack samples it is on. If it is in many branches of the call tree, the call tree won't show you what it really costs.

    让你对“呼叫树”感到困惑。对于一行代码而言重要的是它所处的堆栈样本数量。如果它位于调用树的许多分支中,则调用树将不会显示它的实际成本。

  • If it tells you a line is costly, it cannot tell you why. For that you want to see as much state information on each sample as you need, rather than just summaries.

    如果它告诉你一条线是昂贵的,它不能告诉你为什么。为此,您希望根据需要查看每个样本的状态信息,而不仅仅是摘要。

  • It's hard to tell it when you want to do samples, and when not. You want it to sample when you're waiting for the app, not when it's waiting for you.

    当你想做样品时,很难说出它,何时不是。您希望它在您等待应用程序时进行采样,而不是在等待您时进行采样。

#5


0  

So now that you know you need a profiler, you might not have the Visual Studio one, so Very Sleepy might be of help.

所以既然你知道你需要一个分析器,你可能没有Visual Studio,所以Very Sleepy可能会有所帮助。

#1


13  

The name you're after is a profiler. Try Find Application Bottlenecks with Visual Studio Profiler

你追求的名字是一个分析器。尝试使用Visual Studio Profiler查找应用程序瓶颈

#2


6  

You need a profiler.

你需要一个分析器。

Visual Studio Team edition includes a profiler (which is what you are looking for) but you may only have access to the Professional or Express editions. Take a look at these threads for alternatives:

Visual Studio Team版本包含一个分析器(您正在寻找),但您可能只能访问Professional或Express版本。看看这些线程的替代品:

What's your favorite profiling tool (for C++) What are some good profilers for native C++ on Windows?

你最喜欢的分析工具是什么(对于C ++)什么是Windows上原生C ++的优秀分析器?

You really shouldn't optimize ANY parts of your application until you've measured how long they take to run. Otherwise you may be directing effort in the wrong place, and you may be making things worse, not better.

在测量运行所需的时间之前,您真的不应该优化应用程序的任何部分。否则你可能会把工作指向错误的地方,而你可能会让事情变得更糟,而不是更好。

#3


1  

I have used a profiler called "AQ Time" which gives every detail you want to know about about the performance of your code. It's not free though..

我使用了一个名为“AQ Time”的分析器,它提供了您想要了解的有关代码性能的每个细节。虽然它不是免费的..

#4


1  

You could get a histogram of the program counter, but it is practically useless unless you are doing something dumb like spending time in a bubble sort of a big array of ints or doubles.

你可以获得一个程序计数器的直方图,但它实际上是无用的,除非你做一些愚蠢的事情,比如花时间在一大堆的整数或双打中。

If you do something as simple as a bubble sort of an array of strings, the PC histogram will only tell you that you have a hotspot in the string compare routine. That's not much help, is it?

如果你做一些像字符串数组的冒泡那样简单的事情,那么PC直方图只会告诉你字符串比较例程中有一个热点。这没什么帮助,是吗?

I know you wouldn't do such a bubble sort, but just for fun, let's assume you did, and it was taking 90% of your time. (i.e. if you fixed it, it could go up to 10 times faster.)

我知道你不会做这样的冒泡,但只是为了好玩,让我们假设你做了,而且它占用了90%的时间。 (即如果你修好它,它可以快10倍。)

It's actually a very easy thing to find, because if you just hit the pause button in the debugger, you will almost certainly see that it stops in the string compare routine. Then if you look up the stack one level, you will be looking directly at the bubble sort loop which is your bug. If you're not sure you've really spotted the problem, just pause it several times. The number of times you see the problem tells you how costly it is.

这实际上是一个非常容易找到的东西,因为如果你只是点击调试器中的暂停按钮,你几乎肯定会看到它在字符串比较例程中停止。然后,如果你向上查找一个级别,你将直接查看冒泡排序循环,这是你的错误。如果您不确定自己是否真的发现了问题,请暂停几次。您看到问题的次数告诉您它的成本是多少。

Any line of code that appears on the call stack on multiple pauses, is something that is begging you to fix it. Some you can't, like "call _main", but if you can you will get a good speedup, guaranteed.

在多个暂停时调用堆栈上出现的任何代码行都是请求您修复它的内容。有些你不能,比如“打电话_main”,但如果可以的话,你会得到一个很好的加速,保证。

Then do it again, and again.

然后再做一次。

When you run out of things you can fix, then you've really tuned the program within an inch of its life.

当你用完你可以修复的东西时,你就已经在一生的一寸之内调整了程序。

It's that simple.

就这么简单。


You could also use the profiler in Visual Studio. It is a nice tool but be aware of these shortcomings:

您还可以在Visual Studio中使用分析器。这是一个很好的工具,但要注意这些缺点:

  • Confusing you with "exclusive time", which if you concentrate on line-level information, is almost meaningless.

    让您对“独家时间”感到困惑,如果您专注于线路信息,那么几乎毫无意义。

  • If your program is wasting time doing I/O, it won't see that, because when it stops to do I/O, the samples stop, unless you use instrumentation.

    如果您的程序浪费时间进行I / O,则不会看到,因为当它停止进行I / O时,样本会停止,除非您使用检测。

  • But if you use instrumentation, you won't get line-level information, only function-level. That's OK if your functions are all small.

    但是,如果使用检测,则不会获得行级信息,只能获得功能级别的信息。如果你的功能都很小,那就没关系。

  • Confusing you with the "call tree". What matters for a line of code is how many stack samples it is on. If it is in many branches of the call tree, the call tree won't show you what it really costs.

    让你对“呼叫树”感到困惑。对于一行代码而言重要的是它所处的堆栈样本数量。如果它位于调用树的许多分支中,则调用树将不会显示它的实际成本。

  • If it tells you a line is costly, it cannot tell you why. For that you want to see as much state information on each sample as you need, rather than just summaries.

    如果它告诉你一条线是昂贵的,它不能告诉你为什么。为此,您希望根据需要查看每个样本的状态信息,而不仅仅是摘要。

  • It's hard to tell it when you want to do samples, and when not. You want it to sample when you're waiting for the app, not when it's waiting for you.

    当你想做样品时,很难说出它,何时不是。您希望它在您等待应用程序时进行采样,而不是在等待您时进行采样。

#5


0  

So now that you know you need a profiler, you might not have the Visual Studio one, so Very Sleepy might be of help.

所以既然你知道你需要一个分析器,你可能没有Visual Studio,所以Very Sleepy可能会有所帮助。