Calculating Time Difference in Milliseconds with time.h

  • Context: C/C++ 
  • Thread starter Thread starter ehrenfest
  • Start date Start date
  • Tags Tags
    Difference Time
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 6K views
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?
 
Physics news on Phys.org
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: