5

I'm looking for a way to measure microsecs in C++/Windows.

I read about the "clock" function, but it returns only milliseconds...
Is there a way to do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Idov
  • 5,006
  • 17
  • 69
  • 106

6 Answers6

7

Use QueryPerformanceCounter and QueryPerformanceFrequency for finest grain timing on Windows.

MSDN article on code timing with these APIs here (sample code is in VB - sorry).

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • I remember there being some issue about using those on multi-processor systems. Does remember what it is and if it was fixed? – André Caron Dec 14 '10 at 21:01
  • @André - correct, YMMV if you have an SMP system that suffers from this. Docs says "On a multiprocessor computer, it should not matter which processor is called. However, you can get different results on different processors due to bugs in the basic input/output system (BIOS) or the hardware abstraction layer (HAL)." – Steve Townsend Dec 14 '10 at 21:05
  • I don't think the documentation has been updated since multi-core processors came out. Today the BIOS & HAL are independent of reading the TSC and TSC differences between cores. You can use the APIC timer for checking TSC drift on older systems. – Steve-o Dec 31 '10 at 05:59
4

There are two high-precision (100 ns resolution) clocks available in Windows:

QueryPerformanceCounter is independant of, and isn't synchronized to, any external time reference. It is useful for measuring absolute timespans.

GetSystemTimePreciseAsFileTime is synchronized. If your PC is in the process of speeding up, or slowing down, your clock to bring it gradually into sync with a time server, GetSystemTimePreciseAsFileTime will appropriately be slower or faster than absolute timespans.

The guidance is:

  • if you need UTC synchronized timestamps, for use across multiple systems for example: use GetSystemTimePreciseAsFileTime
  • if you only need absolute timespans: use QueryPerformanceCounter

Bonus Reading

All kernel-level tracing infrastructure in Windows use QueryPerformanceCounter for measuring absolute timespans.

GetSystemTimeAsFileTime would be useful for something like logging.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
1

I guess there's nothing wrong with the QuerPerformance* answer already given: the question was for a Windows-specific solution, and this is it. For a cross-platform C++ solution, I guess boost::chrono makes most sense. The Windows implementation uses the QuerPerformance* methods, and you immediately have a Linux and Mac solution too.

ecotax
  • 1,933
  • 17
  • 22
1

http://www.boost.org/doc/libs/1_45_0/doc/html/date_time/posix_time.html

altough

Get the UTC time using a sub second resolution clock. On Unix systems this is implemented using GetTimeOfDay. On most Win32 platforms it is implemented using ftime. Win32 systems often do not achieve microsecond resolution via this API. If higher resolution is critical to your application test your platform to see the achieved resolution.

Anycorn
  • 50,217
  • 42
  • 167
  • 261
0

More recent implementations can provide microsecond resolution timestamps on windows with high accuracy. The joint use of system filetime and performance counter allows such accuracies see this thread or also this one

One of the recent implementations can be found at the Windows Timestamp Project

Community
  • 1
  • 1
Arno
  • 4,994
  • 3
  • 39
  • 63
0

(since no-one has mentioned a pure c++ approach yet),

as of c++11:

#include <chrono>
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch())

gets you the number of microseconds since 1970-01-01, and a port of php's microtime(true) api would be

#include <chrono>
double microtime(){
    return (double(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count()) / double(1000000));
}

gets you the number of seconds since 1970-01-01 with microsecond precision

hanshenrik
  • 19,904
  • 4
  • 43
  • 89