Click here to Skip to main content
15,886,919 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionQueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
Robert Inventor14-Mar-13 15:50
Robert Inventor14-Mar-13 15:50 
AnswerRe: QueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
Robert Inventor14-Mar-13 18:07
Robert Inventor14-Mar-13 18:07 
GeneralRe: QueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
dusty_dex15-Mar-13 0:24
dusty_dex15-Mar-13 0:24 
GeneralRe: QueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
Robert Inventor18-Mar-13 14:44
Robert Inventor18-Mar-13 14:44 
GeneralRe: QueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
dusty_dex18-Mar-13 22:08
dusty_dex18-Mar-13 22:08 
AnswerRe: QueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
Robert Inventor19-Mar-13 6:19
Robert Inventor19-Mar-13 6:19 
GeneralRe: QueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
dusty_dex19-Mar-13 8:34
dusty_dex19-Mar-13 8:34 
GeneralRe: QueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
Robert Inventor20-Mar-13 10:10
Robert Inventor20-Mar-13 10:10 
Yes I've tried the multimedia timers. The problem is that on Windows they aren't quite good enough for musicians, frankly. They don't let you quite achieve the 1 ms precision that musicians require. There is a Microsoft article about that here too:

Guidelines For Providing Multimedia Timer Support[^]

So, anyway, it actually turned out it is only partly solved. I had the HPET enabled as well - the HPET described in that article which I think is a normal feature of modern chips.

To enable it you open up an admin level command prompt and type:
bcdedit /set useplatformclock true

then you need to reboot.

This requires Vista or later.

You know it is enabled if QueryPerformanceFrequency gives you a frequency in the range of 14+ MHz. You might possibly need to enable it in the BIOS as well.

To disable you do the same and type

bcdedit /deletevalue useplatformclock

and reboot.

It's disabled by default. Which seems surprising since it improves multimedia performance - or should - but from the forums it seems some users have issues including reduced performance of some games and mouse pointer "ghosting" and some report freezes with it enabled in the forums. So they might have decided it is more trouble than it is worth to have it enabled by default.

So - with HPET enabled and using the interrupt timer I get this perfect timing.

Actually turns out the interrupt timer changes only once every ms on my computer (roughly) and stays steady in between those calls. So I suppose on some other computers might change less often.

So though very fast look up, you wouldn't probably use it for code performance testing. I'm planning to use it most of the time, but to use the QueryPerformanceCounter(..) for the last couple of ms of the loop just to time fractional ms increments, if required (or for longer if it turns out to have larger increments than 1 ms).

Also bug fix in the previous code, seems you have to do this every time you check it:

C++
 // http://www.dcl.hpi.uni-potsdam.de/research/WRK/2007/08/getting-os-information-the-kuser_shared_data-structure/
 //  However, to ensure applications (Win32) to read a consistent time value, the order in which the members of KSYSTEM_TIME are updated has a very strict manner. 
// The ISR first updates High2Time, then LowPart, and finally High1Time. 
 // A consuming application must read the structure the same strict but 
 // inverse order, that is, it first reads High1Time, then LowPart, 
 // and finally High2Time. If High1Time and High2Time are equal, 
 // the High1Time:LowPart is a consistent value. 
 // Otherwise the application was interrupted by the clock interrupt and 
 // needs to read the structure again.

 for(;;)
 {
  ts.SysTime.High1Time = USER_SHARED_DATA->InterruptTime.High1Time;
  ts.SysTime.LowPart   = USER_SHARED_DATA->InterruptTime.LowPart;
  if(ts.SysTime.High1Time == USER_SHARED_DATA->InterruptTime.High2Time);
   break;
  ntests++;
 } 


On the backwards timers, there is one thing to be careful about - this caught me out - if you have a multi-threaded app with different threads calling the time simultaneously it might seem to run backwards just because you are using the previously recorded time of one thread and comparing it with the current time of the current time. I fixed that by using thread local variables to check the previously recorded time.

It turns out that that was the reason why I thought I had the time going backwards - not multiple cores or anything just a bug in the code for testing if the time was monotonic in a multi-threaded app.

Sorry about that!

Anyway - still have these timing issues for users who run my program on a computer with the OS set to not use the HPET - as it is by default. So - what I'd really like to know is if there is any way to access the HPET in the case where Windows isn't using it for timing itself? Is there some assembly language way for instance to get at its register even though Windows itself isn't using it?

Don't really think there is or surely someone would have posted a way to do it by now, and everyone would be doing it, but you never know Smile | :) .
GeneralRe: QueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
Robert Inventor20-Mar-13 11:12
Robert Inventor20-Mar-13 11:12 
GeneralRe: QueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
dusty_dex20-Mar-13 11:18
dusty_dex20-Mar-13 11:18 
GeneralRe: QueryPerformanceCounter inaccurate timing even with use of SetThreadAffinityMask Pin
Robert Inventor20-Mar-13 12:15
Robert Inventor20-Mar-13 12:15 
QuestionHelp in Win32 programming with c++ Pin
naseer86114-Mar-13 8:44
naseer86114-Mar-13 8:44 
AnswerRe: Help in Win32 programming with c++ Pin
NotPolitcallyCorrect14-Mar-13 8:50
NotPolitcallyCorrect14-Mar-13 8:50 
GeneralRe: Help in Win32 programming with c++ Pin
naseer86118-Mar-13 9:41
naseer86118-Mar-13 9:41 
AnswerRe: Help in Win32 programming with c++ Pin
Maximilien14-Mar-13 8:54
Maximilien14-Mar-13 8:54 
AnswerRe: Help in Win32 programming with c++ Pin
Captain Price16-Mar-13 2:55
professionalCaptain Price16-Mar-13 2:55 
QuestionNeed a free image library, png, Visual Studio 6.0 Pin
Andrlage14-Mar-13 5:52
Andrlage14-Mar-13 5:52 
AnswerRe: Need a free image library, png, Visual Studio 6.0 Pin
Richard MacCutchan14-Mar-13 6:22
mveRichard MacCutchan14-Mar-13 6:22 
AnswerRe: Need a free image library, png, Visual Studio 6.0 Pin
_Flaviu14-Mar-13 21:19
_Flaviu14-Mar-13 21:19 
QuestionMSCOWRKS.DLL ? Pin
ForNow13-Mar-13 3:10
ForNow13-Mar-13 3:10 
AnswerRe: MSCOWRKS.DLL ? Pin
Richard MacCutchan13-Mar-13 3:53
mveRichard MacCutchan13-Mar-13 3:53 
GeneralRe: MSCOWRKS.DLL ? Pin
ForNow13-Mar-13 4:12
ForNow13-Mar-13 4:12 
AnswerRe: MSCOWRKS.DLL ? Pin
Albert Holguin13-Mar-13 5:33
professionalAlbert Holguin13-Mar-13 5:33 
QuestionHow to fix Static contol width at run time? Pin
Le@rner12-Mar-13 20:52
Le@rner12-Mar-13 20:52 
AnswerRe: How to fix Static contol width at run time? Pin
Richard MacCutchan12-Mar-13 22:22
mveRichard MacCutchan12-Mar-13 22:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.