8

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.

MinaHany
  • 1,015
  • 4
  • 16
  • 32
  • 6
    This is unanswerable. There exists an entire universe of root causes and you haven't even shown us the code. – Ed S. Jun 08 '12 at 04:57
  • 12
    You should look into [profiling](https://en.wikipedia.org/wiki/Profiling_%28computer_programming%29). This is exactly the process that will solve your problem. (`gprof` is an example of a profiler that is often pre-installed on Unix/Linux systems.) – huon Jun 08 '12 at 04:58
  • 3
    You should really look into profiling your code as indicated by @dbaupp. Some tools - valgrind = http://valgrind.org/ , gprof = http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html... http://oprofile.sourceforge.net/news/ = OProfile. GProf is very good amongst them I often use it. – verisimilitude Jun 08 '12 at 05:03
  • Per your edit I suppose I should have just closed as a duplicate then since you apparently didn't [use the search function](http://stackoverflow.com/search?q=profiling). And again, if you want profiling tools you'll need to tell us what platform you are targeting. – Ed S. Jun 08 '12 at 05:04
  • http://stackoverflow.com/questions/1875167/performance-profiling-on-linux , http://stackoverflow.com/questions/1777556/alternatives-to-gprof –  Jun 08 '12 at 05:06
  • 4
    @EdS. he did not know the keyword "profiling". He described what he was trying to do very politely, and didn't post his entire program and ask us to fix it for him. I think this is a reasonable question. – steveha Jun 08 '12 at 05:26
  • @steveha: Fair enough, I though about that when I did the search. So, how about [how to measure performance in C++](http://stackoverflow.com/search?q=C%2B%2B+measure+performance)? That works without the keyword. – Ed S. Jun 08 '12 at 16:48

2 Answers2

13

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.

huon
  • 94,605
  • 21
  • 231
  • 225
  • Is it likely that the ratios of the times of different functions will change if you wouldn't compile with `-g`? – simon Jun 08 '12 at 06:43
  • @simon, I don't think it will make a huge difference, but I'm not sure. – huon Jun 08 '12 at 08:29
1

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.

Jay
  • 24,173
  • 25
  • 93
  • 141
  • Holy **$$$$7k** for a single user license!?? That makes all my Microsoft tools look cheap... –  Jun 08 '12 at 05:15
  • You know what is also funny, it is 7.7k€ which converts to over $9.6k gotta love being shafted like that. – r_ahlskog Jun 08 '12 at 09:48