When QueryPerformanceCounter call takes long time
Tags: winapi, optimization, hardware, windows
Published: 2017-12-28 23:50:16

---
title: "When QueryPerformanceCounter call takes long time"
published: true
tags: winapi,optimization,hardware,windows
canonical_url: http://asawicki.info/news_1667_when_queryperformancecounter_call_takes_long_time.html
---

[**QueryPerformanceCounter**](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx) function is all about measuring time and profiling performance, so I wasn't able to formulate right Google query to find a solution to the problem I had - call to `QueryPerformanceCounter` function itself taking too much time. Below I describe what I eventually found out.

It all started from hardware failure. My motherboard stopped working, so I needed to buy a new one (ASRock X370 Killer SLI). I know that normally changing motherboard requires reinstalling Windows, but I tried not to do it. The system didn't want to boot, so I booted the PC using pendrive with Windows installer and launched the repair function. It helped - after that Windows was able to start and everything seemed to work... until I launched the program that I develop on that machine. It was running painfully slow.

I tried different things to find out what is happening. Input/output to hard drive or network was not an issue. GPU performance was also OK. It seemed that the app is just doing its calculations slowly, like the CPU was very slow. I double-checked actual CPU and RAM frequency, but it was OK. Finally I launched sampling profiler (the one embedded in Visual Studio - command: Analyze \> Performance Profiler). This way I found that most of the time is spent in function `QueryPerformanceCounter`.

This WinAPI function is the recommended way to obtain a timestamp in Windows. It's very precise, monotonic, safe to use on multiple cores and threads, it has stable frequency independent of CPU power management or Turbo Boost... It's just great, but in order to meet all these requirements, Windows may use different methods to implement it, as described in article [Acquiring high-resolution time stamps](https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx). Some of them are fast (just reading TSC register), others are slow (require system call - transition to kernel mode).

I wrote a simple C++ program that tests how long it takes to execute `QueryPerformanceCounter` function. You can see the code here: [**QueryPerformanceCounterTest.cpp**](https://github.com/sawickiap/MISC/blob/master/QueryPerformanceCounterTest.cpp) and download 64-bit binary here: [**QueryPerformanceCounterTest.zip**](http://asawicki.info/Download/Code/QueryPerformanceCounterTest.zip). Running this test on two different machines gave following results:

CPU: Intel Core i7-6700K, Motherboard: GIGABYTE Z170-HD3-CF:

> \> QueryPerformanceCounterTest.exe 1000000000  
> Executing QueryPerformanceCounter x 1000000000...  
> According to GetTickCount64 it took 0:00:11.312 (11.312 ns per call)  
> According to QueryPerformanceCounter it took 0:00:11.314 (11.314 ns per call)

CPU: AMD Ryzen 7 1700X, Motherboard: ASRock X370 Killer SLI (changed from different model without system reinstall):

> \> QueryPerformanceCounterTest.exe 10000000  
> Executing QueryPerformanceCounter x 10000000...  
> According to GetTickCount64 it took 0:00:24.906 (2490.6 ns per call)  
> According to QueryPerformanceCounter it took 0:00:24.911 (2491.1 ns per call)

As you can see, the function takes 11 nanoseconds on first platform and 2.49 microseconds (220 times more!) on the second one. This was the cause of slowness of my program. The program calls this function many times.

I tried to fix it and somehow convince Windows to use the fast implementation. I uninstalled and reinstalled motherboard drivers - the latest ones downloaded from manufacturer website. I upgraded and downgraded BIOS to different versions. I booted the system from Windows installation media and "repaired" it again. I restored default settings in UEFI/BIOS and tried to change "ACPI HPET Table" option there to Disabled/Enabled/Auto. Nothing worked. Finally I restored Windows to factory settings (Settings \> Update & Security \> Recovery \> Reset this PC). This solved my problem, but unfortunately it's like reinstalling Windows from scratch - now I need to install and configure all the apps again. After that the function takes 22 ns on this machine.

My conclusions from this "adventure" are twofold:

1. It is valid for function `QueryPerformanceCounter` to execute slowly on some platforms, like for 2.5 microseconds. If you call it just once per rendering frame then it doesn't matter, but you shouldn't profile every small portion of your code with it, calling it millions of times.
2. Windows 10 still requires reinstallation when changing motherboard. Otherwise, even if it seems to work, you may experience strange issues like this one.

Update 2017-12-11: A colleague told me that enabling/disabling HPET using "bcdedit" system command could possibly help for that issue.