10

How do you measure the execution time in milliseconds or microseconds in Windows C++?

I found many method one calling time(NULL), but it measures time in seconds only and the seconds clock() (clock_t) measure CPU time, not the actual time.

I found the function gettimeofday(Calendar time) mentioned in this paper: dropbox.com/s/k0zv8pck7ydbakz/1_7-PDF_thesis_2.pdf

This function is for Linux (compute time in milli and microseconds) and not Windows.

I found an alternative to it for Windows: dropbox.com/s/ofo99b166l7e2gf/gettimeofday.txt

And this may be relevant: stackoverflow.com/questions/1861294/how-to-calculate-execution-time-of-a-code-snippet-in-c

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3617694
  • 143
  • 1
  • 1
  • 8

3 Answers3

31

You can use the standard C++ <chrono> library:

#include <iostream>
#include <chrono>

// long operation to time
long long fib(long long n) {
  if (n < 2) {
    return n;
  } else {
    return fib(n-1) + fib(n-2);
  }
}

int main() {
  auto start_time = std::chrono::high_resolution_clock::now();

  long long input = 32;
  long long result = fib(input);

  auto end_time = std::chrono::high_resolution_clock::now();
  auto time = end_time - start_time;

  std::cout << "result = " << result << '\n';
  std::cout << "fib(" << input << ") took " <<
    time/std::chrono::milliseconds(1) << "ms to run.\n";
}

One thing to keep in mind is that using <chrono> enables type safe, generic timing code but to get that benefit you have use it a bit differently than you would use dumb, type-unsafe timing libraries that store durations and time points in types like int. Here's an answer that explains some specific usage scenarios and the differences between using untyped libraries and best practices for using chrono: https://stackoverflow.com/a/15839862/365496


The maintainer of Visual Studio's standard library implementation has indicated that the low resolution of high_resolution_clock has been fixed in VS2015 via the use of QueryPerformanceCounter().

starriet
  • 2,565
  • 22
  • 23
bames53
  • 86,085
  • 15
  • 179
  • 244
  • 2
    **Please note!** There is a bug in some versions of the `chrono` header supplied with Visual Studio. Please see [here](http://stackoverflow.com/questions/16299029/resolution-of-stdchronohigh-resolution-clock-doesnt-correspond-to-measureme). `QueryPerformanceCounter` is a way to get high resolution timing under Win32. – Rook May 12 '14 at 19:25
  • 1
    I can confirm that VS 2015 now uses `QueryPerformanceCounter` for `high_resolution_clock`. (Older VS versions just used `GetSystemTime` for all clocks.) – Paul Groke Aug 16 '15 at 20:12
  • return statement in fib() is missing a semicolon – Chief A Dec 28 '18 at 09:12
  • For completeness: Since VS2015, `high_resolution_clock` is an alias for `steady_clock`(it was an alias for `system_clock` until VS2012), and `steady_clock` uses QueryPerformanceCounter and QueryPerformanceFrequency. [Thus](https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps), as of VS2015, `high_resolution_clock` would be the highest resolution. **But,** just use std::chrono::steady_clock or std::chrono::system_clock *directly* instead of std::chrono::high_resolution_clock([ref](https://en.cppreference.com/w/cpp/chrono/high_resolution_clock)). – starriet Oct 08 '22 at 05:23
4

You need to use the QPC/QPF APIs to get compute the execution time. Invoke the code you want to between calls to QueryPerformanceCounter and then use QueryPerformanceFrequency to convert it from cycles to microseconds.

LARGE_INTEGER nStartTime;
LARGE_INTEGER nStopTime;
LARGE_INTEGER nElapsed;
LARGE_INTEGER nFrequency;

::QueryPerformanceFrequency(&nFrequency); 
::QueryPerformanceCounter(&nStartTime);

    SomethingToBeTimed();

::QueryPerformanceCounter(&nStopTime);
nElapsed.QuadPart = (nStopTime.QuadPart - nStartTime.QuadPart) * 1000000;
nElapsed.QuadPart /= nFrequency.QuadPart;

References: http://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx

Darrin Cullop
  • 1,170
  • 8
  • 14
2

You're looking for QueryPerformanceCounter and related functions.

MooseBoys
  • 6,641
  • 1
  • 19
  • 43