Boost Timer resolution question

Status
Not open for further replies.
I'm using boost::timer to benchmark some code, but I was wondering about resolution.

http://www.boost.org/doc/libs/1_46_1/libs/timer/

This page states that it uses std::clock() to get the time, which has 1ms resolution, which I would like better than, if possible.

This old page: http://unitedsoft.ch/boost/libs/timers/ states that boost (?) used to be able to define a microsecond timer. Is this still possible with the current standard version of boost? (boost_1_46_1)

I can't seem to find the syntax, if it is possible.

EDIT: This code has to work on both Linux and Windows, otherwise I would just use gettimeofday() to get a better resolution.
 

SkyRanger-1

Smack-Fu Master, in training
60
Boost doesn't implement the actual raw timing functions (or any other raw IO that I know of), it's just a wrapper for calls to std::clock. In the specific case of boost::timer, the wrapper doesn't really do anything useful, so there's no penalty to calling the underlying IO functions yourself. So far as I know, if you actually care about resolution you simply have to call platform-specific functions. If you need portability you can either use #ifdefs on platform-specific time functions or use a portability library like SDL.

The common timing functions I'm familiar with are:

clock
requires: libc
pros: very portable, usually fast
cons: often very low resolution (55 milliseconds is not uncommon)
comments: the standard for portable timing

timeGetTime
requires: WIN32, winmm.lib
pros: fast, millisecond or near-millisecond resolution
cons: generally between 1 millisecond and 16 milliseconds
comments: my preferred solution for casual timing on WIN32

RDTSC
requires: x86-specific, called in slightly different ways on different compilers
pros: very high resolution (often sub-nanosecond), semi-fast (usually < 50 nanoseconds)
cons: resolution can vary, even in mid-run; no standard way to query the units it returns; buggy on some (rare?) computers
comments: my preferred solution for high resolution performance monitoring, but I wouldn't use it for any other purpose

QueryPerformanceCounter
requires: WIN32, Kernel32.lib
pros: high resolution (typically 1 microsecond to 1 nanosecond)
cons: at least 3 different implementations are used on different computers; implementations vary widely in speed, resolution, and reliability
comments: Microsoft recommends this, but I prefer to avoid it - with so many implementations, there are more possible ways it can screw up. Still, it avoids the main flaw of RDTSC while coming close to RDTSCs resolution on most computers. Some implementations are very slow. Some implementations are buggy on some computers.

gettimeofday
requires: POSIX?
pros: relatively portable, at least on the *nix side of things
cons: ???
comments: I haven't used this much, not sure what its performance or typical resolution is like; the max resolution is 1 microsecond

GetTickCount
requires: WIN32
pros: fast
cons: relatively poor resolution (16 milliseconds typically)
comments: I prefer timeGetTime
 
I used boost_posix_time for some stuff a while back. On nix it defaults to gettimeofday, and on windows it uses something else. I do not recall the resolution, but it worked for what I wanted. It seemed fairly straight forward, get a timestamp at the start and end, and they support subtraction/addition so you can find the delta. I did have to manually calculate the correction before timing, (e.g., time the creation of a time stamp and subtract that time off)
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
I find an amalgamation of several of those techniques ends up being needed for "start,operation,stop" invasive profiling (as opposed to letting a profiler take care of it all for you).

The cost of certain high res timer calls can be expensive, sometimes only on initialisation but then you need to be aware of the drift characteristics for the specific device.

A colleague did some analysis based on having some decent absolute time source (think atomic clock signals) that cost a hefty chunk of microseconds to request being mixed in with RDTSC for interpolation with a simple modulus based fallback to the solid time source. I believe this required either a little thread based state or a loop test just in case the thread in question got context switched in the middle of the test. If you know your hardware is past a certain age (different for intel and and AMD) then you can ignore the problems early revisions had.

The result was portable across windows and Linux.

Given that you likely won't care Bout absolute time it should be simpler for you.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
QueryPerformanceCounter() will typically be about 3 MHz on modern computers. In the olden days it used to use rdtsc, but that's always been unusable on multiprocessors, and has been eliminated even from uniprocessors by SpeedStep and similar systems.

Yes but RDTSCP basically fixes that if you're willing to ignore older systems (a very big but I admit)
 
ShuggyCoUk":21suzo82 said:
QueryPerformanceCounter() will typically be about 3 MHz on modern computers. In the olden days it used to use rdtsc, but that's always been unusable on multiprocessors, and has been eliminated even from uniprocessors by SpeedStep and similar systems.

Yes but RDTSCP basically fixes that if you're willing to ignore older systems (a very big but I admit)
I don't think it does. rdtscp just forces serialization, which with rdtsc you have to do manually (e.g. with a cpuid/rdtsc pair--and guess what rdtscp also delivers? The processor ID...). Serialization still doesn't make the clock tick at a constant rate, and doesn't make multiprocessors tick at a constant rate; it just means that the processor can't schedule out-of-order around the rdtscp instruction.

I believe the latest Intel processors support a constant-rate tsc, that always ticks at the maximum clock speed even if SpeedStepped. I'm not sure if it also ticks when HLTed, though.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Sorry, I was imprecise. I was referring precisely to the invariat aspect and using the serialised one as a synonym (when it is not, though I could tell you off hand which ones have it without invariant)

http://software.intel.com/en-us/article ... pi-timing/

It is apparently stable across all C states, though we disable those anyway (plays merry hell with latency)
 
ShuggyCoUk":2m60m00p said:
Sorry, I was imprecise. I was referring precisely to the invariat aspect and using the serialised one as a synonym (when it is not, though I could tell you off hand which ones have it without invariant)
I think every processor with rdtscp also has invariant tsc; I think both are features of Nehalem and newer.

http://software.intel.com/en-us/articles/best-timing-function-for-measuring-ipp-api-timing/

It is apparently stable across all C states, though we disable those anyway (plays merry hell with latency)
The most interesting bit was:
Synchronization of the TSC across multiple threads/cores/packages: As long as software does not write the TSC, the Nehalem TSC will remain synchronized across all threads, cores and packages connected to a single PCH.

This means that rdtsc is once more safe to use in single socket systems, and possibly multiple socket systems too, depending on topology. Dual socket should be fine, at the very least.
 
Status
Not open for further replies.