Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current microsecond time in C?

Tags:

c

unix

time

How can I print current microseconds time in C on Unix platform?

like image 541
Sachin Chourasiya Avatar asked Mar 14 '11 19:03

Sachin Chourasiya


People also ask

How do you timestamp in microseconds?

1900-01-01-00.00. 00.000000. Last 6 digits are micro seconds.

What is USEC in C?

Time#usec() is a Time class method which returns the number of microseconds for time.

How do you express microseconds?

A microsecond is a unit of time in the International System of Units (SI) equal to one millionth (0.000001 or 10−6 or 1⁄1,000,000) of a second. Its symbol is μs, sometimes simplified to us when Unicode is not available.

Is a microsecond fast?

A microsecond is equal to one millionth (10−6 or 1/1,000,000) of a second. One microsecond is to one second as one second is to 11.54 days. To keep our blinking example alive: 350,000 microseconds. Still too slow.


3 Answers

In Linux and BSDs, you can use the gettimeofday() function. This populates a timeval struct which has a field for seconds since the epoch and a field for microseconds. This function is deprecated. The higher resolution clock_gettime() is the favored replacement, however Mac OS X does not implement it. I'm not sure if Windows implements either of these.

like image 160
Matt K Avatar answered Oct 30 '22 07:10

Matt K


There is no portable (multiplatform) way to get that number. On Linux (or other POSIX systems) for example there is the call gettimeofday that provides exactly that precision (accuracy however will depend if that timing is available and implemented on the specific hardware).

like image 20
6502 Avatar answered Oct 30 '22 08:10

6502


The C standard doesn't provide a standard means for that. However the clock() function returns time in CLOCK's. there are CLOCKS_PER_SEC CLOCK's in a second. On my machine and implementation, CLOCKS_PER_SEC is defined as 1000. Both clock and CLOCKS_PER_SEC are defined in <time.h>. Hope this helped

like image 2
Armen Tsirunyan Avatar answered Oct 30 '22 07:10

Armen Tsirunyan