What is the best, most accurate timer in C++?
相关问题
- 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
Under windows it would be
QueryPerformanceCounter
, though seeing as you didn't specify any conditions it possible to have an external ultra high resolution timer that has a c++ interface for the driverIn C++11 you can portably get to the highest resolution timer with:
Example output:
"chrono_io" is an extension to ease I/O issues with these new types and is freely available here.
There is also an implementation of
<chrono>
available in boost (might still be on tip-of-trunk, not sure it has been released).The C++ standard doesn't say a whole lot about time. There are a few features inherited from C via the
<ctime>
header.The function
clock
is the only way to get sub-second precision, but precision may be as low as one second (it is defined by the macroCLOCKS_PER_SEC
). Also, it does not measure real time at all, but processor time.The function
time
measures real time, but (usually) only to the nearest second.To measure real time with subsecond precision, you need a nonstandard library.
The answer to this is platform-specific. The operating system is responsible for keeping track of timing and consequently, the C++ language itself provides no language constructs or built-in functions for doing this.
However, here are some resources for platform-dependent timers:
SetTimer
: http://msdn.microsoft.com/en-us/library/ms644906(v=vs.85).aspxsetitimer
: http://linux.die.net/man/2/setitimerA cross-platform solution might be boost::asio::deadline_timer.