Current microsecond time in C?

How to print the current microsecond time on a C platform on a Unix platform?

+8
c unix time
source share
4 answers

On Linux and BSD, you can use the gettimeofday() function. This fills the timeval structure, which has a field in seconds from an era and a field for microseconds. This feature is deprecated. A higher resolution clock_gettime () is the preferred replacement, however Mac OS X does not implement it. I am not sure if Windows uses any of them.

+7
source share

There is no portable (multi-platform) way to get this number. For example, on Linux (or other POSIX systems) there is a gettimeofday call that provides exactly that accuracy (accuracy, however, will depend on which is available and implemented on the particular hardware).

+5
source share

Standard C does not provide standard tools for this. However, the clock() function returns time in CLOCK. there is CLOCKS_PER_SEC CLOCK per second. On my machine and implementation, CLOCKS_PER_SEC is defined as 1000. Both clock and CLOCKS_PER_SEC defined in <time.h> . Hope this helps

+2
source share

If you work on Linux, I would look at this site: http://rabbit.eng.miami.edu/info/functions/time.html In particular, the gettimeofday function. Which you can transfer the structure and get the time of day in microseconds.

0
source share

All Articles