Link to home
Start Free TrialLog in
Avatar of bachra04
bachra04Flag for Canada

asked on

Display time in milliseconds in C++/C linux /unix

Hi guys,

I need help on how to display time in milliseconds  in c/c++ under linux ?
Avatar of sarabande
sarabande
Flag of Luxembourg image

you would use the gettimeofday function:

struct timeval  tv = { 0 };
gettimeofday(&tv, NULL);

double mill = (tv.tv_usec) / 1000 ; 

Open in new window


Sara
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bachra04

ASKER

The problem I have seen is how to printf the value, I'm seeing weird values
267.0000
948.0000

then
15.0000


etc ...
I'm expecting that time of day will be increasing
For both system calls? The man page to gettimeofday (http://linux.die.net/man/2/gettimeofday) states:

POSIX.1-2008 marks gettimeofday() as obsolete, recommending the use of clock_gettime(2) instead. [...] The time returned by gettimeofday() is affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the system time). If you need a monotonically increasing clock, see clock_gettime(2).

(even though I wouldn't expect such a weird behaviour)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial