BDE 4.14.0 Production release
|
Typedefs | |
typedef bsls::TimeUtil | bsls_TimeUtil |
This alias is defined for backward compatibility. | |
Provide a platform-neutral functional interface to system clocks.
This component provides a set of platform-neutral pure procedures to access real-time system clock functionality. High-resolution time functions intended for interval-timing return a time interval in nanoseconds (1 nsec = 1E-9 sec) as a 64-bit integer.
bsls::TimeUtil
high-resolution functions return time values as absolute nanoseconds from an arbitrary reference that will in many cases remain fixed within a single process (and among running processes on a single machine). Absolute monotonic behavior is platform-dependent, however, as are accuracy and useful precision, despite the nominal nanosecond precision implied by the return value. The user is advised to determine the actual performance on each platform of interest.
On certain windows platform configurations, bsls::TimeUtil::getTimer
and bsls::TimeUtil::getRawTimer
can produce unreliable results. On some machines, these high-resolution functions have been observed to run at inconsistent speeds, with worst cases as slow as half the speed of actual wall time. This is known behavior of the underlying high-performance timer function QueryPerformanceCounter
, upon which the Windows implementation of bsls::TimeUtil
relies.
Reference: https://msdn.microsoft.com/library/windows/desktop/dn553408
The behavior of the timer on windows platforms depends on the interaction of operating system, BIOS, and processor, and certain combinations of the three (particularly older ones) are vulnerable to timer inaccuracy. For example, frequently the QueryPerformanceCounter
function that TimeUtil
uses on Windows will utilize the CPU's timestamp counter (TSC), and CPUs with speed scaling mechanisms such as SpeedStep (frequently used for power management) will generally see the clock speed vary with the CPU frequency. However, newer processors often provide an Invariant TSC
that solves this problem. Also versions of Windows starting with Vista may internally handle the inconsistency by automatically using a lower resolution, but accurate, counter on processors that do not provide an Invariant TSC
.
In addition, on multi-core machines, each call to QueryPerformanceCounter
may read the TSC from a different CPU. The TSCs of the CPUs may be out of sync, resulting in slightly inconsistent or even non-monotonic behavior.
Reference: http://support.microsoft.com/kb/895980
If a Windows machine appears to have a slow and/or inconsistent high-resolution timer, it can be reconfigured to avoid using the TSC. On Windows XP and earlier versions, add the parameter /usepmtimer
to the operating system's boot configuration in boot.ini
. On Windows Vista and later, run the following command as an administrator:
Note that unless the machine has a High Performance Event Timer (HPET) and it has been enabled in the BIOS, these steps might reduce the resolution of the bsls::TimeUtil
high-resolution functions from the nanosecond range to the microsecond range (or worse).
Providing that the underlying timer is capable of supporting the QueryPerformanceCounter
interface, getTimer
and convertRawTime
will perform their calculations to nanosecond precision based on the values reported by QueryPerformanceCounter
. Because of overflow concerns, these routines do not simply divide the result of QueryPerformanceCounter
by the result of QueryPerformanceFrequency
. In the course of calculating the final nanosecond-precision time, there are two places where some precision might be lost. Therefore, the times reported by getTimer
and convertRawTime
may be as much as two nanoseconds less than the actual time expressed by the QueryPerformanceCounter
interface. Note that the times will still be monotonically non-decreasing.
The following snippets of code illustrate how to use bsls::TimeUtil
functions to implement a very simple timer. Only the most primitive functionality is illustrated. See the bsls::Stopwatch
component for a better example of a timer interface.
The my_Timer
object may be used to time some section of code at runtime as follows:
typedef bsls::TimeUtil bsls_TimeUtil |