Calculating Time Difference in Milliseconds with time.h

  • Topic: C/C++ 
  • Thread starter ehrenfest
  • Start date
  • Tags Tags
    Difference Time
4 replies · 6K views · last reply
ehrenfest
Messages
2,001
Reaction score
1
I have been using the time.h header and the difftime function is good if you want to know the difference between two events measured in seconds but how do you get the difference between two events that occur milliseconds apart?
 
It depends on your operating system.
On windows QueryPerformanceCounter() will tell you the number of ticks since the last call, PerformanceCounterFrequency() tells you the frequency of the ticks ( it's about 10Mhz IIRC). Also take a look at 'multimedia timers'.
 
mgb_phys said:
It depends on your operating system.
On windows QueryPerformanceCounter() will tell you the number of ticks since the last call, PerformanceCounterFrequency() tells you the frequency of the ticks ( it's about 10Mhz IIRC). Also take a look at 'multimedia timers'.

I am using Linux.
 
gettimeofday() contains microseconds since start of day,
I don't know what frequency it is updated at.
But if you google for QueryPerformanceCounter() alternatives under linux you should find something.
 
mgb_phys said:
gettimeofday() contains microseconds since start of day,
I don't know what frequency it is updated at.
But if you google for QueryPerformanceCounter() alternatives under linux you should find something.

Yes, gettimeofday is a good alternative. Function details here: http://www.linuxmanpages.com/man2/gettimeofday.2.php
 
Last edited by a moderator: