3

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 
Xara
  • 8,748
  • 16
  • 52
  • 82
  • I would just use `gprof` (without `-F` arguments) and look inside `log`. As you asked (and as I explained) [elsewhere](http://stackoverflow.com/a/21060594/841108) there might be no accurate enough answer for "get the time for the specific function" – Basile Starynkevitch Jan 12 '14 at 08:13
  • Why don't you just loop it 10^6 times and count how many seconds that takes. If the loop takes N seconds, that means the function takes N microseconds. If you want to count nanoseconds, loop it 10^9 times. This method doesn't slow it down at all and should be plenty accurate. (I can't understand why you're bothering with `gprof`.) – Mike Dunlavey Jan 14 '14 at 02:49
  • @MikeDunlavey I also made use of the time functions present in C++ (http://stackoverflow.com/questions/21060574/function-execution-time) but everytime when I run the program using any of the time functions mentioned in the link, it gives different result. – Xara Jan 14 '14 at 07:28
  • @Zara: But did you run it a large number of times? If you do, the differences will average out to a more reliable number. Don't expect each single run to have the same run time as every other single run. There are too many variables. – Mike Dunlavey Jan 14 '14 at 16:52
  • @MikeDunlavey Yes, I did run my function in a for loop with a limit of 100000. Then I divide the obtained result with 100000. Actually I want to compare how fast one algorithm performs as compared to other, for that purpose I wanted to calculate an accurate time but I think its not possible to get very accurate time results. – Xara Jan 14 '14 at 18:38
  • 1
    @Zara: First, take each program and [*tune the daylights out of it*](http://stackoverflow.com/a/927773/23771), before you compare it to another. Unless you do that, a difference in speed of, say, 2x could be completely meaningless, because any given program, as you write it, typically has lots of water to be squeezed out. – Mike Dunlavey Jan 14 '14 at 18:49

1 Answers1

4

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • As I used clock_gettime earlier, so it used to happen everytime when I run the code I get different time for that function. Do I take several values and then take average? – Xara Jan 12 '14 at 08:44
  • Secondly, in gprof that function displays 0 seconds as one of the functions in my program take 99 % of the time. SO, do I put my function in loop of 10000 or more and then divide the result by that number? – Xara Jan 12 '14 at 08:46
  • You should **give up** your goal of measuring *precisely*, *reliably*, *accurately*, and *without disturbing it*, your function. What you want to do may be *absolutely impossible* and might even make no sense (because of caching effects). – Basile Starynkevitch Jan 12 '14 at 08:48