how to calculate time difference in milliseconds in C/C++
Hi
I want to calculate the time difference between two events in C++ in milliseconds. I am using difftime which returns me the time in seconds. but I want to calculate the time in milliseconds. Please help me. Because problem is that time difference between two events is mostly is zero seconds. But I want more precision in milliseconds. Thanks in Advanced, Waqas Daar |
You are using time() to get the time of day at the beginning of the interval and at its end, correct?
Use gettimeofday() for this instead. That puts the time of day in something of type struct timeval, which you will find defined by doing this at the command line: Code:
man gettimeofday |
For an OS agnostic approach you could use the Boost libraries, but there is nothing defined in the C++ standard to help you.
|
Code:
#include <sys/time.h> |
Um, jiml8? Your code seems incomplete. (I've tucked a copy of it at the end of this post.) As it stands, it's not clear what sort of int value is supposed to be returned by timeval_subtract(), and in any event your sample calling code at the beginning doesn't use that value at all. But the function also doesn't even use its parameter result in any way, which is puzzling.
I'd like to see jiml8's full code. In the meantime, here's another way to do it. I suspect the guts of the code (in red, ewwww) are simpler than jiml8's will end up being, which is always better if it's at least as fast and just as accurate. But, um, time will tell. Here's my code, complete with sample calling code. Code:
#include <sys/time.h> Code:
difference is 3002435 microseconds Here's jiml8's code so far: Quote:
|
Oops!
I lifted the code out of a working project of mine, and stopped copying at the wrong right brace. Code:
int timeval_subtract (result, x, y) |
battle of the banjos
Comparison of the two functions:
|
I needed the input data only for this computation so didn't worry about changing it. If that is an issue, copying the data would only take a few clock cycles.
|
The difference between wje_lq's code and jiml8's code (lets just call it jims code for the time being) is that jims is bullet proof where as wje_lq's is not. There are systems where tv_sec is unsigned and this is where the code would fail.
Quote:
|
Quote:
I'm guessing that you got the tv_sec information (which is useful, by the way, thank you) from the link that you provided: Quote:
|
Quote:
Least-wise, that's my guess. |
Quote:
|
Quote:
|
Quote:
Quote:
Code:
difference->tv_sec =end_time->tv_sec -start_time->tv_sec ; |
Quote:
Quote:
To see that, let's revisit his code after redefining struct timeval to have an unsigned member tv_sec, shall we? And treat it as unsigned throughout. Run this bash script: Code:
#!/bin/bash Code:
4294967294d sec, 0 usec |
Well I suppose the question is:
How do you suppose we represent a negative number in an unsigned int? In reflection both codes return a value which indicates if the value is positive or negative, which I failed to notice in your code sample. I stand corrected. |
Hi, waqasdaar -
Now that you've got these cool routines (and they *are* cool!) ... please be sure to take the results with a pound of salt (or better, to quote Andrew Tanenbaum, with a "metric ton of salt"). Results are seldom accurate to-the-millisecond, much less to the microsecond. At the very least, you'll definitely want to run multiple tests, and average your results... |
Quote:
|
I don't understand why the other functions posted are so complicated. It can be done in a single long line.
Code:
#include <stdio.h> |
Quote:
The code kinda changed incrementally. Or, as one of my past cow-orkers was known to say, "excrementally". |
All times are GMT -5. The time now is 09:12 PM. |