To measure execution time of a function in C,how accurate is the POSIX function gettimeofday()
in Ubuntu 12.04 on an Intel i7 ? And why ? If its hard to say,then how to find out ? I can't find a straight answer on this.

- 3,884
- 2
- 24
- 39

- 784
- 3
- 9
- 22
-
execution time of a function – user1511956 Jun 17 '13 at 04:17
-
2This might be helpful : http://stackoverflow.com/questions/12392278/measure-time-in-linux-getrusage-vs-clock-gettime-vs-clock-vs-gettimeofday – VoidPointer Jun 17 '13 at 04:23
-
What kind of run time are we talking about? If you are trying to time things in the millisecond scale, `gettimeofday()` is horribly inappropriate. However, if it your program runs for, e.g. a week, `gettimeofday()` might be sufficient for your needs... – twalberg Jun 17 '13 at 16:11
3 Answers
If you are building a stopwatch, you want a consistent clock that does not adjust to any time servers.
#include <time.h>
...
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
If CLOCK_MONOTONIC_RAW
does not exist, use CLOCK_MONOTONIC
instead. If you want time of day, you still should not use gettimeofday
.
clock_gettime(CLOCK_REALTIME, &ts);
The timer for such clocks is high resolution, but it shifts. The timespec
supports resolution as small as nanoseconds, but your computer will likely only be able to measure somewhere in the microseconds.
gettimeofday
returns a timeval
, which has a resolution in microseconds.

- 344
- 3
- 10

- 8,648
- 5
- 39
- 58
-
1This is an answer to the question the OP should have asked, but not to the one he did ask. – Cairnarvon Jun 17 '13 at 04:59
-
1It returns time in microseconds but the actual accuracy is not microseconds (not 1 microsecond), http://stackoverflow.com/a/7760992/390913 – perreal Jun 17 '13 at 05:11
-
@perreal That is correct. I only meant that the data structure was capable of holding microseconds. – TheBuzzSaw Jun 17 '13 at 05:18
-
@perreal The accuracy of any timing function will always depend on the hardware involved, which TheBuzzSaw clearly stated in his answer – Cdaragorn Jun 17 '13 at 15:27
gettimeofday
should be accurate enough, at least for comparing relative performance. But, in order to smooth out inaccuracies due to context switching and IO related delays you should run the function, say, at least hundreds of times and get the average.
You can tune the number of times you need to execute the function by making sure you get approximately the same result each time you benchmark the function.
Also see this answer to measure the accuracy of gettimeofday in your system.
You can use gettimeofday() at start and end of code to find the difference between starting time and finishing time of program. like
//start = gettimeofday
//your code here
//end = gettimeofday
Execution time will be difference between the 2 But it depends on how much load is one the machine is at that point. So not a reliable method.
You can also use:
time ./a.out
assuming your program is a.out

- 589
- 8
- 23
-
2
-
@thejh Getting execution time from gettimeofday is not a reliable method. and hence inaccurate. – SureshS Jun 17 '13 at 04:33