Funny you should mention Fortran, because the naming and interface design of my function
second() stems from its original use with Fortran code. This was before there were standardized timing functions in Fortran.
Yes, there are differences between measuring wall clock time and CPU time. In extreme cases, the difference can be very large. Just the other day I was looking at a program with about 10 hours of elapsed wall-clock time, but only about 1.5 hours of CPU time. The difference was attributable to heavy I/O activity.
To my knowledge,
gettimeofday() returns wall-clock time, and that is why I stated in my initial post “if you are after elapsed wall-clock time, and microsecond resolution is sufficient, you could use the following code”.
Fair warning: Wall-clock time may be subject to discontinuity or non-monotonicity, for example due to the system operator adjusting the system time, or possibly (not sure, I have not tried it) across the start point or end point of daylight saving time. If you expect situations like that to be an issue, you might want to investigate other timing facilities, for example
clock_gettime(), which has a different set of caveats.
The following question (with answers :-) on Stackoverflow may be helpful: