Does anyone know how to calculate time difference in C++ in milliseconds?
I used difftime
but it doesn't have enough precision for what I'm trying to measure.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
if you are using win32 FILETIME is the most accurate that you can get: Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
So if you want to calculate the difference between two times in milliseconds you do the following:
You can get micro and nanosecond precision out of Boost.Date_Time.
I think you will have to use something platform-specific. Hopefully that won't matter? eg. On Windows, look at
QueryPerformanceCounter()
which will give you something much better than milliseconds.I know this is an old question, but there's an updated answer for C++0x. There is a new header called
<chrono>
which contains modern time utilities. Example use:More information can be found here:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm
There is also now a boost implementation of
<chrono>
.The clock function gives you a millisecond timer, but it's not the greatest. Its real resolution is going to depend on your system. You can try
and see how your results are.
You can use
gettimeofday
to get the number of microseconds since epoch. The seconds segment of the value returned by gettimeofday() is the same as that returned by time() and can be cast to a time_t and used in difftime. A millisecond is 1000 microseconds.After you use difftime, calculate the difference in the microseconds field yourself.