Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 448
MinaHany Avatar asked Jun 08 '12 04:06

MinaHany


2 Answers

Since you are on linux, you probably have the gprof profiler installed already. The most basic use of gprof is by compiling with the -pg option (the -g option is also needed to get informative output). e.g.

> 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. 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 (e.g. open was called 7208 times and 0.02s were spent executing it.). 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.

like image 105
huon Avatar answered Sep 22 '22 07:09

huon


As suggested by dbaupp above, gprof is an excellent tool for linux. In addition to that, if you have access to IBM Rational Quantify, you can try that also. It is a commercial tool, but provides good graphical view of functions taking more time and the call flow etc.

like image 21
Jay Avatar answered Sep 26 '22 07:09

Jay