C ++函数的时间测量 [英] C++ Time measurement of functions
问题描述
我需要测量C ++程序的时间,尤其是某些递归函数的整体运行时间.其他函数内部有很多函数调用.我的第一个想法是在实际代码中实现一些时间测量功能.
I need to measure the time of a C++ programs, especially the overall running time of some recursive functions. There are a lot of function calls inside other functions. My first thought was to implement some time measurements functions in the actual code.
gprof的问题在于,它会打印出数据类型的类运算符的时间,但我只需要有关功能的信息集,而"-f func_name prog_name"将无法工作.
The problem with gprof is, that it prints out the time of class operators of a datatype, but i only need the infomartion about the functions and "-f func_name prog_name" wont work.
那么,科学中最常用的测量数字程序时间的方法是什么?
So, what is the most common way in science to measure time of a numerical program?
是这样的:
function2()
{
}
function1()
{
function2();
funtcion1();
}
int main(){
function1();
}
推荐答案
如果您使用的是GNU软件包(即gcc),则可以尝试使用gprof.只需使用-g和-pg标志编译程序,然后运行
gprof <your_program_name>
If you're using the GNU package, i.e. gcc, you can try gprof. Just compile your program with -g and -pg flags and then run
gprof <your_program_name>
gprof: http://www.cs.utah. edu/dept/old/texinfo/as/gprof_toc.html
为了提高详细程度,您可以将gprof与其他标志一起运行:
In order to increase the level of detail you can run gprof with other flags:
-l(--line)启用逐行概要分析,使直方图命中可以计入单独的代码行,而不是功能.
-l (--line) enables line by line profiling, giving you a histogram hits to be charged to individual lines of code, instead of functions.
-a在输出中不包含私有函数.
-a Don’t include private functions in the output.
-e <function>
排除功能<function>
的输出.当某些功能无法更改时,请使用此功能.例如,某些网站的源代码已获得监管机构的批准,即使效率低下,代码也将保持不变.
-e <function>
Exclude output for a function <function>
. Use this when there are functions that won’t be changed. For example, some sites have source code that’s been approved by a regulatory agency, and no matter how inefficient, the code will remain unchanged.
-E <function>
还要从百分比表中排除在该功能上花费的时间.
-E <function>
Also exclude the time spent in the function from the percentage tables.
-f <function>
与-e相反:仅跟踪<function>
中的时间.
-f <function>
The opposite of -e: only track time in <function>
.
-F <function>
在计算百分比时仅使用<function>
中的时间.
-F <function>
Only use the time in <function>
when calculating percentages.
-b不要打印说明文字.如果您更有经验,可以使用此选项.
-b Don’t print the explanatory text. If you’re more experienced, you can appreciate this option.
-s累积样本.通过多次运行该程序,有可能获得 更好地了解花在哪里的时间.例如,一个慢例程可能不会被调用 对于所有输入值,因此您可能会误导阅读在哪里找到 性能问题.
-s Accumulate samples. By running the program several times, it’s possible to get a better picture of where time is spent. For example, a slow routine may not be called for all input values, and therefore you maybe mislead reading where to find performance problems.
这篇关于C ++函数的时间测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!