简体   繁体   English

测量C中函数的时间

[英]measure time in a function in C

I'm debugging an C application and I'd like to know how much time it spends in a particular function. 我正在调试一个C应用程序,我想知道它在特定函数中花费了多少时间。

I could change the source code and add some more code to do the measurement, but it doesn't seem right to me. 我可以更改源代码并添加一些代码来进行测量,但对我来说似乎并不合适。 I'd rather do it with external application, without recompiling every time. 我宁愿用外部应用程序来做,而不是每次都重新编译。

I found out it's possible to set up a break point in GDB so I thought, it must be possible to track the time using similar tool by simple procedure: - set breakpoint - when stopped, measure actual time and run the function - when leaving function, measure time again However, i haven't found a way how to do this in gdb :( 我发现可以在GDB中设置断点,所以我想,必须能够通过简单的程序使用类似工具跟踪时间: - 设置断点 - 停止时,测量实际时间并运行函数 - 离开函数时,再次测量时间然而,我还没有找到一种方法如何在gdb中做到这一点:(

any ideas? 有任何想法吗? Thanks 谢谢

I have a helper function in my ~/.gdbinit: 我的〜/ .gdbinit中有一个辅助函数:

define timeme
set $last=clock()
n
set $timing=clock() - $last
if $timing>$arg0
printf "***long***\n"
end
printf "%d cycles, %f seconds\n", $timing, (float)$timing / 1000000
end

You may need to adjust the 1000000 depending on what your implementation of CLOCKS_PER_SEC is on your platform. 您可能需要调整1000000,具体取决于您的平台上CLOCKS_PER_SEC的实现。

Usage is trivial; 用法很简单; run the helper which will execute the next step and give timing information: 运行帮助程序,它将执行下一步并提供计时信息:

Breakpoint 2, install_new_payload_from_meta (snmp_meta=0x7eee81c0, pkt=0x0, entry=0x7d4f4e58) at /home/sgillibr/savvi-dc-snmp/recipies.c:187
(gdb) timeme 100000
***long***
580000 cycles, 0.580000 seconds
(gdb)

Obviously resolution may not be enough for some needs, although it does prove very useful. 显然,对于某些需求而言,解决方案可能还不够,尽管它确实非常有用。

如果您正在使用GCC,则需要编译选项“-pg”和应用程序gprof

gprof is only reliable -- in my experience, only works at all -- if you statically link -pg compiled versions of every library, including the C library. gprof是唯一可靠的-在我的经验,只有在所有工作-如果你静态链接-pg编译版本的每个库,包括C库。 You can try to do this using gcc's -profile option (which does what -pg does plus tries to sub in -pg libraries) but the problem is, GNU libc really does not like being statically linked, and your distro may not provide -pg compiled versions of every library you need. 您可以尝试使用gcc的-profile选项(它执行-pg确实加上尝试sub-in -pg库)但问题是,GNU libc真的不喜欢静态链接,并且您的发行版可能不提供-pg您需要的每个库的编译版本。

I suggest you try cachegrind , which is a valgrind mode of operation and only needs debug info for everything. 我建议你尝试cachegrind ,这是一种valgrind操作模式,只需要调试信息。 That's much easier to get. 这更容易获得。 The catch is, it has huge overhead costs; 问题是,它有巨大的管理费用; so huge that it might invalidate your testing. 如此巨大,以至于它可能使您的测试无效。 Expect at least a 2x slowdown. 预计至少减速2倍。

You can also try perf -- if you can get your hands on a copy. 您也可以尝试perf -如果你能得到你的手放在一个副本。 It's very clever, but it is of, by, and for kernel hackers who think people like building stuff from scratch. 它非常聪明,但对于那些认为人们喜欢从头开始构建东西的内核黑客而言,这是非常聪明的。 I have had very mixed luck with it. 我的运气很好。 (Do read http://web.eecs.utk.edu/~vweaver1/projects/perf-events/ which is about the underlying API, not the utility, but might still save you from a great deal of wasted time.) (请阅读http://web.eecs.utk.edu/~vweaver1/projects/perf-events/ ,这是关于底层API,而不是实用程序,但可能仍然可以节省大量的浪费时间。)

Put this into your ~/.gdbinit 把它放到你的〜/ .gdbinit中

define timeme
    python import time
    python starttime=time.time()
    next
    python print("Previous takes: " + (str)(time.time()-starttime) + "s")
end
document timeme
    Measure executing time of next function
    Usage: timeme or ti
end

type timeme or ti when you want to measure the time of next function. 当您想要测量下一个功能的时间时,键入timemeti

Profiling is probably what you want. 分析可能就是你想要的。 Take a look at prof or gprof. 看看prof或gprof。

UPDATE: After compilng with "cc -Wall -ggdb -pg -g3 -O2 diskhash.c -o diskhash" ( and running the program), "gprof -p diskhash" gives me: 更新:在使用“cc -Wall -ggdb -pg -g3 -O2 diskhash.c -o diskhash”(并运行程序)编译后,“gprof -p diskhash”给了我:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 32.60      0.41     0.41        1   410.75   646.18  create_hashtab
 31.80      0.81     0.40  5087692     0.00     0.00  hash_func
 27.83      1.16     0.35  2543846     0.00     0.00  find_hash
  2.78      1.20     0.04  2543846     0.00     0.00  chop_a_line
  1.59      1.22     0.02                             main
  0.40      1.22     0.01                             frame_dummy
  0.00      1.22     0.00        4     0.00     0.00  map_da_file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM