Calculating Time Difference in Milliseconds with time.h

  • Context: C/C++ 
  • Thread starter Thread starter ehrenfest
  • Start date Start date
  • Tags Tags
    Difference Time
Click For Summary

Discussion Overview

The discussion revolves around calculating the time difference in milliseconds between two events using the C programming language's time.h header. Participants explore various methods and functions available on different operating systems, particularly focusing on alternatives to the difftime function for more precise measurements.

Discussion Character

  • Technical explanation, Debate/contested

Main Points Raised

  • One participant notes that the difftime function measures time differences in seconds and seeks a method for milliseconds.
  • Another participant suggests using QueryPerformanceCounter() on Windows, mentioning its ability to provide ticks since the last call and the frequency of those ticks.
  • A participant using Linux mentions gettimeofday() as a function that provides microseconds since the start of the day but expresses uncertainty about its update frequency.
  • Another participant reiterates the use of gettimeofday() and suggests looking for alternatives to QueryPerformanceCounter() for Linux users, providing a link to further details.

Areas of Agreement / Disagreement

Participants present multiple competing views regarding the appropriate functions to use on different operating systems, indicating that there is no consensus on a single solution for measuring time differences in milliseconds.

Contextual Notes

Participants express uncertainty about the frequency of updates for the gettimeofday() function and the specifics of alternatives to QueryPerformanceCounter() on Linux, highlighting potential limitations in their suggestions.

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?
 
Technology 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:

Similar threads

Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
86
Views
4K
Replies
81
Views
8K
  • · Replies 7 ·
Replies
7
Views
131K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
1
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K