简体   繁体   English

找出减慢C程序速度的工具?

[英]Tools to find out what is slowing down a C program?

I have a program made up of several .h and .c files and a lot of functions. 我有一个由几个.h和.c文件和许多函数组成的程序。 And there are functions which call other functions and so on. 还有一些功能可以调用其他功能等。 Now, this is actually an assignment so I know how much time the program needs to reach the end. 现在,这实际上是一项任务,所以我知道该计划需要多长时间才能达到目的。

The problem is, my program takes too much time compared to the times I am given. 问题是,与我给出的时间相比,我的程序花费了太多时间。 Is it possible to find out which function is taking too much time or which part of the code is holding the program down? 是否有可能找出哪个函数花费了太多时间或者代码的哪个部分使程序停止运行?


I did not give the code here because it is too long. 我没有在这里给出代码,因为它太长了。 I know that no one can answer why "my program" is slow but I am talking in general! 我知道没有人能回答为什么“我的节目”很慢但我说话一般! Is there a tool that measures how much time each function takes or something similar? 是否有工具可以衡量每个功能需要多长时间或类似的功能? I am using gcc and I'm on Linux. 我正在使用gcc,我在Linux上。

Since you are on linux, you probably have the gprof profiler installed already. 由于您使用的是linux,因此您可能已经安装了gprof探查器。 The most basic use of gprof is by compiling with the -pg option (the -g option is also needed to get informative output). gprof最基本的用法是使用-pg选项进行编译(获取信息输出也需要-g选项)。 eg 例如

> gcc -g -pg -o my_executable my_file.c

Now, you can just run your program normally. 现在,您可以正常运行程序。 Then you run 然后你跑

> gprof my_executable > profile.txt

which will output the profiling information into profile.txt . 它将分析信息输出到profile.txt This data looks a little like 这些数据看起来有点像

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 33.34      0.02     0.02     7208     0.00     0.00  open
 16.67      0.03     0.01      244     0.04     0.12  offtime
 16.67      0.04     0.01        8     1.25     1.25  memccpy
 16.67      0.05     0.01        7     1.43     1.43  write
 16.67      0.06     0.01                             mcount
  0.00      0.06     0.00      236     0.00     0.00  tzset
  0.00      0.06     0.00      192     0.00     0.00  tolower
  0.00      0.06     0.00       47     0.00     0.00  strlen
  0.00      0.06     0.00       45     0.00     0.00  strchr
  0.00      0.06     0.00        1     0.00    50.00  main
  0.00      0.06     0.00        1     0.00     0.00  memcpy
  0.00      0.06     0.00        1     0.00    10.11  print
  0.00      0.06     0.00        1     0.00     0.00  profil
  0.00      0.06     0.00        1     0.00    50.00  report

[...]

and you can read off some data about each function (eg open was called 7208 times and 0.02s were spent executing it.). 并且你可以读出关于每个函数的一些数据(例如, open被称为7208次,并且花费0.02s执行它。)。 That example data was borrowed from this guide , which you should read as it gives much more explanation and describes to how manipulate the profiling to get things like line-by-line profiling. 这个示例数据来自本指南 ,您应该阅读它,因为它提供了更多的解释,并描述了如何操纵分析以获得逐行分析等内容。

As suggested by dbaupp above, gprof is an excellent tool for linux. 正如上面的dbaupp所建议的,gprof是一个很好的linux工具。 In addition to that, if you have access to IBM Rational Quantify , you can try that also. 除此之外,如果您可以访问IBM Rational Quantify ,您也可以尝试使用它。 It is a commercial tool, but provides good graphical view of functions taking more time and the call flow etc. 它是一种商业工具,但提供了更多时间和呼叫流程等功能的良好图形视图。

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

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