Gprof:具体函数时间 [英] Gprof: specific function time

查看:218
本文介绍了Gprof:具体函数时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解一个特定功能在我的程序中花费的时间。为了这个目的,我使用gprof。我使用以下命令获取特定功能的时间,但仍然日志文件显示程序中存在的所有功能的结果。请在这方面帮助我。

  gprof -F FunctionName可执行文件gmon.out> log 


解决方案

您几乎重复了关于函数执行时间的另一个问题



当我回答,存在困难(由于硬件!)以可靠地获得一些特定功能的执行时间,特别是如果该功能花费很少时间(例如小于一毫秒)。您原来的问题指向这些方法



我建议使用 clock_gettime(2) CLOCK_REALTIME 或者 CLOCK_THREAD_CPUTIME_ID



gprof(1)(在使用 -pg 编译之后)使用 profil(3)并且正在使用采样技术,基于发送 SIGPROF 信号=http://man7.org/linux/man-pages/man7/signal.7.html =nofollow> signal(7))从周期性间隔(例如每10毫秒)计时器设置为 setitimer(2) TIMER_PROF ;所以周期性地对程序计数器进行采样。请阅读 gprof 上的wikipage,并注意到分析可能会显着地 降低运行



如果您的函数在短时间内执行(小于一毫秒),则概要分析会产生不精确的测量结果(请参阅 heisenbugs )。



换句话说,分析和测量短时运行函数的时间是改变程序的行为(这也会发生在一些其他操作系统上!)。您可能必须放弃精确测量可靠准确的功能 >不干扰它。它甚至可能不会使任何精确感觉,例如。因为 CPU缓存



您可以使用 gprof 而不使用任何 -F 参数,如果需要,后处理文本配置文件输出 GNU awk )提取您的信息



BTW,特定函数的精确时序可能不重要。重要的是整个应用程序的基准测试



您还可以要求编译器优化更多的程序;如果您使用链接时间优化,即编译并将 g ++ -flto -O2 ,小函数的时序概念甚至可能不再存在(因为编译器和链接器可能已经内联了它,而不知道)。

请考虑目前的 superscalar 处理器因此需要使用微架构。 org / wiki / Instruction_pipelinerel =nofollow>指令管道,缓存分支预测器注册重命名推测执行无序执行等等,定时短功能的定义的概念是未定义的。您无法预测或衡量它。


I want to find out the time spent by a particular function in my program. FOr that purpose, I am making use of gprof. I used the following command to get the time for the specific function but still the log file displays the results for all the functions present in the program. Please help me in this regard.

gprof -F FunctionName  Executable gmon.out>log 

解决方案

You are nearly repeating another question about function execution time.

As I answered there, there is a difficulty (due to hardware!) to get reliably the execution time of some particular function, specially if that function takes little time (e.g. less than a millisecond). Your original question pointed to these methods.

I would suggest using clock_gettime(2) with CLOCK_REALTIME or perhaps CLOCK_THREAD_CPUTIME_ID

gprof(1) (after compilation with -pg) works with profil(3) and is using a sampling technique, based upon sending a SIGPROF signal (see signal(7)) at periodic intervals (e.g. each 10 milliseconds) from a timer set with setitimer(2) and TIMER_PROF; so the program counter is sampled periodically. Read the wikipage on gprof and notice that profiling may significantly degrade the running time.

If your function gets executed in a short time (less than a millisecond) the profiling gives an imprecise measurement (read about heisenbugs).

In other words, profiling and measuring the time of a short running function is altering the behavior of the program (and this would happen with some other OS too!). You might have to give up the goal of measuring precisely and reliably and accurately the timing of your function without disturbing it. It might even not make any precise sense, e.g. because of the CPU cache.

You could use gprof without any -F argument and, if needed, post-process the textual profile output (e.g. with GNU awk) to extract the information you want.

BTW, the precise timing of a particular function might not be important. What is important is the benchmarking of the entire application.

You could also ask the compiler to optimize even more your program; if you are using link time optimization, i.e. compiling and linking with g++ -flto -O2, the notion of timing of a small function may even cease to exist (because the compiler and the linker could have inlined it without you knowing that).

Consider also that current superscalar processors have a so complex micro-architecture with instruction pipeline, caches, branch predictor, register renaming, speculative execution, out-of-order execution etc etc that the very notion of timing a short function is undefined. You cannot predict or measure it.

这篇关于Gprof:具体函数时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆