Unfortunately, clock () has only one second resolution on Linux (although it returns time in units of microseconds).
Many people use gettimeofday () for benchmarking, but this means that elapsed time - not the time used by this process / thread - is not ideal. Obviously, if your system is more or less idle, and your tests are quite long, you can average the results. Usually less of a problem, but it's worth knowing that the time returned by gettimeofday () is non-monotonous - it can jump a bit, for example. when your system first connects to an NTP time server.
The best thing for benchmarking is clock_gettime () with whatever option is most suitable for your task.
- CLOCK_THREAD_CPUTIME_ID - time clock of CPU time.
- CLOCK_PROCESS_CPUTIME_ID - a high-performance timer for each process from the CPU.
- CLOCK_MONOTONIC - represents a monotonous time from some unspecified starting point.
- CLOCK_REALTIME - System-wide real-time clock.
NOTE , but not all options are supported on all Linux platforms except clock_gettime (CLOCK_REALTIME), which is equivalent to gettimeofday ().
Useful link: Profiling code using clock_gettime
source share