4.85

Timer Resolution - Windows

Image
Obtaining and Setting Timer Resolution on Windows Operating System - various methods.
Download the Open Timer Resolution Software Application for Windows.

Changing Timer Resolution in Windows

The Timer Resolution in Windows can be changed. This is not a common practice, but if you really need this you can find below several ways of achieving the change of the time resolution on Windows. You can download a dedicated software application from below or you can change it programmatically.


Simple method to Set - For regular computer user

Just download the Open Timer Resolution software application using the link below. This software application is completely Free and Open Source.



Advanced method to Set and/or Measure - For software developers or advanced computer users


The following code sample written in C++ calls the function timeGetDevCaps to determine the minimum and maximum time resolutions supported by the timer services. Before it sets up any timer events, the code below establishes the minimum time resolution by using the timeBeginPeriod function.

#define CUSTOM_TIMER_RESOLUTION 1 // example: the custom target (custom) resolution is 1 millisecond

UINT     customTimerResolution;
TIMECAPS timeCaps;

if (timeGetDevCaps(&timeCaps, sizeof(TIMECAPS)) != TIMERR_NOERROR) {
    // ERROR: stop here ...
}

customTimerResolution = min(max(timeCaps.wPeriodMin, CUSTOM_TIMER_RESOLUTION), timeCaps.wPeriodMax);

timeBeginPeriod(customTimerResolution);


The default Timer Resolution on Windows is 15.6 ms - a timer interrupt 64 times a second

If a windows program increase the timer frequency the power consumption increase too. This can harm the battery life of devices that run on battery such as laptops or tablets running a Windows OS. Vice-versa, when a windows program decrease the timer frequency the power consumption decrease too and can result in a longer battery life for battery powered devices such as laptops or tablets running a Windows OS.
There is a place where you can see a report of the time resolution for particular programs or for the entire system - the powercfg energy report by running a shell command: powercfg /energy. If there are any running processes that are requesting a higher resolution than default, you can see them here (including the timer resolution value of each process) as the energy report will show them with a warning (with yellow color). At the bottom of this report you can also find information about the global current timer resolution of the entire system. What have changed: prior to Windows 10 upd.2004 edition (ex: Windows 10 upd.1909) the global current time resolution of the entire system followed the highest resolution set by an application. Later than Windows 10 upd.2004 the current time resolution of the system is always reported as being 156250. This change of policy from Windows regarding the global current time resolution of the entire system is related with the fact that in the past there were many developers that did not care too much about the implications of increasing the global current time resolution of the entire system instead of chaning just the particular value of one or several processes. Looks like the the KPROCESS and similar structures for the Windows 10 upd.2004 edition have additions specifically for the management of resolution for timer.


Sample code C++ - monitoring the effects of the current global timer interrupt interval as set by timeBeginPeriod

// source: https://github.com/randomascii/blogstuff/blob/main/timer_interval/change_interval.cpp

/*
Copyright 2020 Cygnus Software. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
    http://www.apache.org/licenses/LICENSE-2.0
*/

/*
This program monitors the effects of the current global timer interrupt interval as set by timeBeginPeriod.
For most interesting results run this program while change_interval.exe is running in another window,
and when nothing else that sets the timer interrupt interval is running.
*/

#include <Windows.h>
#include <stdio.h>

#pragma comment(lib, "winmm.lib")

int main(int argc, char *argv[]) {
  for (;;) {
    for (int i = 1; i < 16; ++i) {
      printf("Setting timer interrupt interval to %d.\n", i);
      timeBeginPeriod(i);
      Sleep(4000);
      timeEndPeriod(i);
    }
  }
}



Sample code Go lang - measure the Time Resolution on Windows

// license: BSD, (c) The Go Authors and Contributors
// https://github.com/golang/go/issues/44343
// this may return different results on different Windows systems, depending on the edition

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.NewTicker(5 * time.Millisecond)
    begin := make(chan bool)
    var count int
    go func() {
        <-begin
        for range t.C {
            count++
        }
    }()
    begin <- true
    time.Sleep(time.Second)
    t.Stop()
    fmt.Println(count)
}

The output when running the above code may look like (sample):

> go run .
  avg 1.03ms;  min 63.9µs;  p50 1ms;  max 2.3ms;
  p90 1.29ms;  p99 2.3ms;  p999 2.3ms;  p9999 2.3ms;
     63.9µs [  1] ▌
      500µs [ 47] ███████████████████████████████████████
        1ms [ 48] ████████████████████████████████████████
      1.5ms [  1] ▌
        2ms [  3] ██
      2.5ms [  0]
        3ms [  0]
      3.5ms [  0]
        4ms [  0]
      4.5ms [  0]



Sample code Go lang - return sleep granularity on Windows 10 to 1ms:

// license: BSD, (c) The Go Authors and Contributors
// https://github.com/golang/go/issues/44343
// return sleep granularity on windows 10 to 1ms

package main

import (
    "fmt"
    "syscall"
    "time"
)

var (
    winmmDLL            = syscall.NewLazyDLL("winmm.dll")
    procTimeBeginPeriod = winmmDLL.NewProc("timeBeginPeriod")
)

func main() {
    procTimeBeginPeriod.Call(uintptr(1))

    for i := 0; i < 10; i++ {
        time.Sleep(time.Nanosecond)
        fmt.Println(time.Now())
    }
}


Conclusions - Changing te Resolution of the Timer in Windows

This is sometimes needed by many game players that want more FPS ... By increasing the timer resolution they consequently improve the FPS for the games they are playing. But perhaps there are many other situations when an advanced user or a software developer may want to change the time resolution. It can be done using a software application that is dedicated for this purpose like the Open Timer Resolution or can be achieved by developing some code using a programming language (see the code samples above).