C++ code performance

馋奶兔 提交于 2020-01-14 03:44:06

问题


When is about writing code into C++ using VS2005, how can you measure the performance of your code?

Is any default tool in VS for that? Can I know which function or class slow down my application?

Are other external tools which can be integrated into VS in order to measure the gaps in my code?


回答1:


If you have the Team System edition of Visual Studio 2005, you can use the built-in profiler.

  • http://msdn.microsoft.com/en-gb/library/z9z62c29(VS.80).aspx



回答2:


AMD CodeAnalyst is available for free for both Windows and Linux and works on most x86 or x64 CPUs (including Intel's).

It has extra features available when you have an AMD processor, of course. It also integrates into Visual Studio.

I've had pretty good luck with it.


Note that there are generally at least two common forms of profiler:

  • instrumenting: alters your build to record information at the beginning and end of certain areas (usually per function)
  • sampling: periodically looks at what code is running to record information

The types of information recorded can include (but are not limited to): elapsed time, # of CPU cycles, cache hits/misses, etc.

Instrumenting can be specific to certain areas of the code (just certain files or just code you compile, not libraries you link to). The overhead is much higher (you're adding code to the project, which takes time to execute, so you're altering timing; you may change program behavior for e.g. interrupt handlers or other timing-dependent code). You're guaranteed that you will get information about the functions/areas you instrument, though.

Sampling can miss very small or very sporadic functions, but modern machines have hardware help to allow you to sample much more thoroughly. Note that some sampling systems may still inject timing differences, although they generally will be much much smaller.

Some profiling tools support a mixture of the above, depending on how you use them.




回答3:


You could also use Intel VTune.




回答4:


You want a tool called a profiler. For a free one that covers most simple cases, I recommend Very Sleepy. It works by sampling the application's current call stack at regular intervals.




回答5:


You can always measure the time and performance of you code yourself. Consult MSDN about the the following functions QueryPerformanceCounter() and QueryPerformanceFrequency().

For more in depth analysis of memory allocation and execution times we use Memory Validator and Performance Validator from Software Verify. They have support for several languages other than C++.




回答6:


I think measuring performance, and locating code to optimize, are different problems, and require different methods.

To locate code to optimize, I swear by this simple method, which is orthogonal to accepted wisdom about profiling, and does not require you to buy or install any tools.

To measure performance, I'm content with the simple process of running the subject code in a loop and timing it.

EDIT: BTW, I just looked at Very Sleepy, and it appears to be on the right track. It samples the entire call stack, and retains each stack. What I can't tell is if it gives you, for each call instruction or regular instruction, the fraction of stack samples containing that instruction. In my opinion, that is the most valuable statistic, and it does not need to be very precise.

dotTrace, on the other hand, also looks like maybe it retains stack samples, but its UI presentation of call-stack info seems to be a call-tree. What I would look for is something that shows the stack-residence percentage of individual instructions (or statements), because they could be in different branches of the call-tree, and thus the call-tree could miss their importance.




回答7:


For intrusive measurement, use the performance counters. Since you're using C++, you should use a facade over this slightly painful API. STLSoft has a family of such things, with different pros and cons. I suggest winstl::performance_counter for highest resolution, or winstl::threadtimes_counter if you want to monitor the performance of a particular thread regardless of other activity in your process(es). There was an article about this in Dr Dobb's several years ago, in which the design rationale behind the facades was described in detail.

For non-intrusive measurement, you can't go past VTune.




回答8:


We use Rational quantify which comes as a part of Rational PurifyPlus set of tools.

Its an excellent tool for profiling application performance.




回答9:


I've recently tried JetBrains dotTrace profiler and it looks very good. It helped me locate a number "black holes" in existing C++ code quite easily.

It works fine in Visual Studio 2005 Professional in a solution which mixes C# and C++ - it uses the right function names for both pieces of code and does an integrated analysis. You can trace for time or memory.

It will be a pity when the evaluation period expires :)




回答10:


We've had good results from AQTime. It's not free but is cheaper than Visual Studio ;-)



来源:https://stackoverflow.com/questions/906915/c-code-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!