8

I want to set a machine timer resolution to 0.5ms.

Sysinternal utility reports that the min clock resolution is 0.5ms so it can be done.

P.S. I know how to set it to 1ms.

P.P.S. I changed it from C# to more general question (thanks to Hans)

System timer resolution

Boppity Bop
  • 9,613
  • 13
  • 72
  • 151
  • You may look [here](http://www.windowstimestamp/description) for details about timer settings and sub-millisecond timer services. – Arno Aug 01 '12 at 07:44

5 Answers5

12

NtSetTimerResolution

Example code:

#include <windows.h>

extern "C" NTSYSAPI NTSTATUS NTAPI NtSetTimerResolution(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution);

...

ULONG currentRes;
NtSetTimerResolution(5000, TRUE, &currentRes);

Link with ntdll.lib.

Timmmm
  • 88,195
  • 71
  • 364
  • 509
Boppity Bop
  • 9,613
  • 13
  • 72
  • 151
7

You may obtain 0.5 ms resolution by means of the hidden API NtSetTimerResolution(). NtSetTimerResolution is exported by the native Windows NT library NTDLL.DLL. See How to set timer resolution to 0.5ms ? on MSDN. Nevertheless, the true achievable resoltion is determined by the underlying hardware. Modern hardware does support 0.5 ms resolution. Even more details are found in Inside Windows NT High Resolution Timers. The supported resolutions can be obtained by a call to NtQueryTimerResolution().

How to do:

#define STATUS_SUCCESS 0
#define STATUS_TIMER_RESOLUTION_NOT_SET 0xC0000245

// after loading NtSetTimerResolution from ntdll.dll:

// The requested resolution in 100 ns units:
ULONG DesiredResolution = 5000;  
// Note: The supported resolutions can be obtained by a call to NtQueryTimerResolution()

ULONG CurrentResolution = 0;

// 1. Requesting a higher resolution
// Note: This call is similar to timeBeginPeriod.
// However, it to to specify the resolution in 100 ns units.
if (NtSetTimerResolution(DesiredResolution ,TRUE,&CurrentResolution) != STATUS_SUCCESS) {
    // The call has failed
}

printf("CurrentResolution [100 ns units]: %d\n",CurrentResolution);
// this will show 5000 on more modern platforms (0.5ms!)

//       do your stuff here at 0.5 ms timer resolution

// 2. Releasing the requested resolution
// Note: This call is similar to timeEndPeriod 
switch (NtSetTimerResolution(DesiredResolution ,FALSE,&CurrentResolution) {
    case STATUS_SUCCESS:
        printf("The current resolution has returned to %d [100 ns units]\n",CurrentResolution);
        break;
    case STATUS_TIMER_RESOLUTION_NOT_SET:
        printf("The requested resolution was not set\n");   
        // the resolution can only return to a previous value by means of FALSE 
        // when the current resolution was set by this application      
        break;
    default:
        // The call has failed

}

Note: The functionality of NtSetTImerResolution is basically mapped to the functions timeBeginPeriod and timeEndPeriod by using the bool value Set (see Inside Windows NT High Resolution Timers for more details about the scheme and all its implications). However, the multimedia suite limits the granularity to milliseconds and NtSetTimerResolution allows to set sub-millisecond values.

Arno
  • 4,994
  • 3
  • 39
  • 63
  • Thanks. That what I found 4 years ago.. but since the barrage of hostility and misguided answers I answered it myself but as short as possible (cos nobody need this apparently). and i am happily using this in my real-time software since (which is not possible to write on windows and .net according to the majority of 'developers').. PS I looked at your project. seems promising.. just watch for the windows timer 'jitter'.. your project is much more challenging than mine! good luck! – Boppity Bop Apr 05 '14 at 18:47
  • Based on my experiments I think the STATUS_TIMER_RESOLUTION_NOT_SET error code should be 0xC0000245 not 245 – Roland Pihlakas Apr 26 '14 at 22:46
  • @RolandPihlakas That's correct, thanks. I've edited my answer to have this corrected. It is actually [documented at MSDN](http://msdn.microsoft.com/en-us/library/cc704588.aspx) like that. – Arno Apr 27 '14 at 09:28
4

If you use python, I wrote a library named wres, which calls NtSetTimerResolution internally.

pip install wres

import wres

# Set resolution to 0.5 ms (in 100-ns units)
# Automatically restore previous resolution when exit with statement
with wres.set_resolution(5000):
    pass
jks Liu
  • 49
  • 1
  • 3
  • 1
    Perhaps adding a bit more detail to this post would be a lot more helpful to OP and the community in general. – BusyProgrammer Mar 30 '17 at 12:49
  • My practical tests on windows showed between 1ms-1.5ms time.sleep() times when using this method. Replace the 'pass' listed here with your code. When increasing the delay time, it still seems to vary by about .5ms. You'd probably get worse performance in a busy system. – Jacob Waters Aug 17 '22 at 23:29
3

The best you can get out the Win32 API is one millisecond with timeBeginPeriod and timeSetEvent. Maybe your HAL can do better but that's academic, you cannot write device driver code in C#.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I alredy use those. I was hoping to get under that. Do you know if there is a utility which can do this (not necesserely in C#). I just need to change the system clock after all. – Boppity Bop Jun 29 '10 at 20:33
  • I changed from C# to anything else. just need this to be done anyhow. – Boppity Bop Jun 29 '10 at 20:35
1

You'll need a high resolution timer for it.. You can find some information here: http://www.codeproject.com/KB/cs/highperformancetimercshar.aspx

EDIT: More information can be found here: set to tenth of millisecond; http://msdn.microsoft.com/en-us/library/aa964692(VS.80).aspx

riffnl
  • 3,248
  • 1
  • 19
  • 32