Calculating Time Difference in Milliseconds with time.h

In summary, The difftime function in the time.h header is useful for finding the difference between two events in seconds. However, for differences measured in milliseconds, it depends on your operating system. For Windows, the QueryPerformanceCounter() function can be used to get the number of ticks since the last call, and PerformanceCounterFrequency() can determine the frequency of the ticks. Linux users can use the gettimeofday() function, which returns the number of microseconds since the start of the day. Alternatives for QueryPerformanceCounter() under Linux can also be found through a simple Google search.
  • #1
ehrenfest
2,020
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
  • #2
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'.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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:

FAQ: Calculating Time Difference in Milliseconds with time.h

How do I calculate the time difference in milliseconds using the time.h library?

To calculate the time difference in milliseconds, you will need to use the clock() function from the time.h library. This function returns the number of clock ticks since the program started. You can convert this number to milliseconds by dividing it by the value of CLOCKS_PER_SEC, which represents the number of clock ticks per second. This will give you the time difference in milliseconds.

Can I calculate the time difference in milliseconds between two specific times?

Yes, you can calculate the time difference in milliseconds between two specific times by using the difftime() function from the time.h library. This function takes two time values as parameters and returns the difference between them in seconds. You can then convert this value to milliseconds using the method mentioned in the previous answer.

How accurate is the time difference calculation in milliseconds using time.h?

The accuracy of the time difference calculation in milliseconds using time.h depends on the underlying hardware and operating system. Generally, it is accurate up to the millisecond level, but this may vary. It is best to consult the documentation for your specific hardware and operating system to determine the accuracy.

Can I calculate the time difference in milliseconds for a process or function in my program?

Yes, you can calculate the time difference in milliseconds for a specific process or function in your program by using the clock() function at the start and end of the process or function. You can then calculate the difference between these two values to get the time difference in milliseconds.

Is there a maximum time difference that can be calculated in milliseconds using time.h?

The maximum time difference that can be calculated in milliseconds using time.h is dependent on the data type used to store the time value. For example, if you are using the clock_t data type, the maximum value that can be stored is different for different systems. It is best to consult the documentation for your specific data type to determine the maximum time difference that can be calculated.

Back
Top