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
SUMMARY

The discussion focuses on calculating time differences in milliseconds using the C standard library's time.h header. For Windows, the QueryPerformanceCounter() function is recommended for high-resolution timing, while PerformanceCounterFrequency() provides the tick frequency, approximately 10 MHz. For Linux, gettimeofday() is suggested as a suitable alternative, which returns time in microseconds since the start of the day. Users are encouraged to search for alternatives to QueryPerformanceCounter() for Linux systems.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with the time.h header file
  • Knowledge of Windows and Linux operating systems
  • Basic concepts of high-resolution timers
NEXT STEPS
  • Research the usage of QueryPerformanceCounter() and PerformanceCounterFrequency() in Windows
  • Explore the gettimeofday() function in Linux for time measurement
  • Investigate multimedia timers for high-resolution timing on Windows
  • Look for Linux alternatives to QueryPerformanceCounter() for precise timing
USEFUL FOR

Software developers, particularly those working with performance-critical applications in C, and anyone needing precise time measurements across different operating systems.

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
2K
Replies
81
Views
7K
  • · Replies 7 ·
Replies
7
Views
131K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
1
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
3K