C: Different implementation of clock() in Windows and other OS?

天大地大妈咪最大 提交于 2019-11-28 01:19:25

If we look at the source code for clock() on Mac OS X, we see that it is implemented using getrusage, and reads ru_utime + ru_stime. These two fields measure CPU time used by the process (or by the system, on behalf of the process). This means that if usleep (or fgets) causes the OS to swap in a different program for execution until something happens, then any amount of real time (also called "wall time", as in "wall clock") elapsed does not count against the value that clock() returns on Mac OS X. You could probably dig in and find something similar in Linux.

On Windows, however, clock() returns the amount of wall time elapsed since the start of the process.

In pure C, I am not aware of a function available on OS X, Linux and Windows that will return wall time with a sub-second precision (time.h being fairly limited). You have GetSystemTimeAsFileTime on Windows that will return you time in slices of 100ns, and gettimeofday from BSD that will return time to a microsecond precision.

If second-precision is acceptable to you, you could use time(NULL).

If C++ is an option, you could use one of the clocks from std::chrono to get time to the desired precision.

You're encountering a known bug in Microsoft's C Runtime. Even though the behavior is not conforming to any ISO C standard, it won't be fixed. From the bug report:

However, we have opted to avoid reimplementing clock() in such a way that it might return time values advancing faster than one second per physical second, as this change would silently break programs depending on the previous behavior (and we expect there are many such programs).

On Linux, you should read time(7). It suggests to use the POSIX 2001 clock_gettime which should exist on recent MacOSX (and on Linux). On Linux, running on not too old hardware (e.g. less than 6 years old laptop or desktop), clock_gettime gives good accuracy, typically dozens of microseconds or better. It gives measures with seconds and nanoseconds (in a struct timespec), but I don't expect the nanosecond figure to be very accurate.

Indeed, clock(3) is told to be conforming to...

C89, C99, POSIX.1-2001. POSIX requires that CLOCKS_PER_SEC equals 1000000 independent of the actual resolution.

At last, several framework libraries provide functions (wrapping target specific system functions) to measure time. Look into POCO (in C++) or Glib (from GTK & Gnome in C).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!