CPU clock frequency and thus QueryPerformanceCounter wrong?

I am using QueryPerformanceCounter to time some code. I was shocked when the code starting reporting times that were clearly wrong. To convert the results of QPC into "real" time you need to divide by the frequency returned from QueryPerformanceFrequency, so the elapsed time is:

Time = (QPC.end - QPC.start)/QPF

After a reboot, the QPF frequency changed from 2.7 GHz to 4.1 GHz. I do not think that the actual hardware frequency changed as the wall clock time of the running program did not change although the time reported using QPC did change (it dropped by 2.7/4.1).

MyComputer->Properties shows:

Intel(R) Pentium(R) 4 CPU 2.80 GHz; 4.11 GHz; 1.99 GB of RAM; Physical Address Extension

Other than this, the system seems to be working fine.

I will try a reboot to see if the problem clears, but I am concerned that these critical performance counters could become invalid without warning.


While I appreciate the answers and especially the links, I do not have one of the affected chipsets nor to I have a CPU clock that varies itself. From what I have read, QPC and QPF are based on a timer in the PCI bus and not affected by changes in the CPU clock. The strange thing in my situation is that the FREQUENCY reported by QPF changed to an incorrect value and this changed frequency was also reported in MyComputer -> Properties which I certainly did not write.

A reboot fixed my problem (QPF now reports the correct frequency) but I assume that if you are planning on using QPC/QPF you should validate it against another timer before trusting it.

-------------Problems Reply------------

Apparently there is a known issue with QPC on some chipsets, so you may want to make sure you do not have those chipset. Additionally some dual core AMDs may also cause a problem. See the second post by sebbbi, where he states:

QueryPerformanceCounter() and QueryPerformanceFrequency() offer a bit better resolution, but have different issues. For example in Windows XP, all AMD Athlon X2 dual core CPUs return the PC of either of the cores "randomly" (the PC sometimes jumps a bit backwards), unless you specially install AMD dual core driver package to fix the issue. We haven't noticed any other dual+ core CPUs having similar issues (p4 dual, p4 ht, core2 dual, core2 quad, phenom quad).

From this answer.

You can try to use the Stopwatch class from .NET, it could help with your problem since it abstracts from all this low-lever stuff.

Use the IsHighResolution property to see whether the timer is based on a high-resolution performance counter.

Note: On a multiprocessor computer, it does not matter which processor the thread runs on. However, because of bugs in the BIOS or the Hardware Abstraction Layer (HAL), you can get different timing results on different processors. To specify processor affinity for a thread, use the ProcessThread..::.ProcessorAffinity method.

You should always expect the core frequency to change on any CPU that supports technology such as SpeedStep or Cool'n'Quiet. Wall time is not affected, it uses a RTC. You should probably stop using the performance counters, unless you can tolerate a few (5-50) millisecond's worth of occasional phase adjustments, and are willing to perform some math in order to perform the said phase adjustment by continuously or periodically re-normalizing your performance counter values based on the reported performance counter frequency and on RTC low-resolution time (you can do this on-demand, or asynchronously from a high-resolution timer, depending on your application's ultimate needs.)

Just a shot in the dark.

On my home PC I used to have "AI NOS" or something like that enabled in the BIOS. I suspect this screwed up the QueryPerformanceCounter/QueryPerformanceFrequency APIs because although the system clock ran at the normal rate, and normal apps ran perfectly, all full screen 3D games ran about 10-15% too fast, causing, for example, adjacent lines of dialog in a game to trip on each other.

I'm afraid you can't say "I shouldn't have this problem" when you're using QueryPerformance* - while the documentation states that the value returned by QueryPerformanceFrequency is constant, practical experimentation shows that it really isn't.

However you also don't want to be calling QPF every time you call QPC either. In practice we found that periodically (in our case once a second) calling QPF to get a fresh value kept the timers synchronised well enough for reliable profiling.

As has been pointed out as well, you need to keep all of your QPC calls on a single processor for consistent results. While this might not matter for profiling purposes (because you can just use ProcessorAffinity to lock the thread onto a single CPU), you don't want to do this for timing which is running as part of a proper multi-threaded application (because then you run the risk of locking a hard working thread to a CPU which is busy).

Especially don't arbitrarily lock to CPU 0, because you can guarantee that some other badly coded application has done that too, and then both applications will fight over CPU time on CPU 0 while CPU 1 (or 2 or 3) sit idle. Randomly choose from the set of available CPUs and you have at least a fighting chance that you're not locked to an overloaded CPU.

Category:timer Views:6 Time:2009-03-13
Tags: timer

Related post

  • Finding out the CPU clock frequency (per core, per processor) 2011-12-02

    Programs like CPUz are very good at giving in depth information about the system (bus speed, memory timings, etc.) However, is there a programmatic way of calculating the per core (and per processor, in multi processor systems with multiple cores per

  • What is the most reliable way to determine the CPU clock frequency of Android phones? 2011-02-09

    I found some references and ended up with the following code: String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" }; cmd = new ProcessBuilder(args); Process process = cmd.start(); InputStream in = process.getI

  • How do I get CPU Clock Speed in C++ (Linux)? 2010-03-10

    How can I get the CPU clock speed in C++? I am running Ubuntu 9.10 if that makes any difference. --------------Solutions------------- Read the pseudo-file /proc/cpuinfo. See this link for an explanation of the fields it contains. This information is

  • CPU Clock Speed & Clock Period Relation 2011-07-04

    In this slide, things looks a little off to me. Clock cycle time or clock period, is already time required per clock cycle. Question is, does the word Clock Rate makes sense? It also says, Hardware designer must often trade off clock rate against cyc

  • Why, when my netbook is on power save mode (CPU clocks dow 2012-02-21

    Why, when my netbook is on power save mode (CPU clocks down to 0.97GHz from 1.67GHz), its CPU usage won't go past 59% when doing something intensive? Is Windows 8.1 limiting the CPU usage besides underclocking it? Is it measuring the CPU usage with r

  • conversion if a processor is with a clock frequency of 2GHz, 2011-04-02

    if a processor is with a clock frequency of 2GHz, what is the clock cycle time? for exapmle . if the clock cycle tiem is 2.5 micro sec , then frequency is 1/(2.5x10-6)= 400,000 Hertz or 400 KHz --------------Solutions------------- Wolfram Alpha answe

  • Bus and DDR clock frequency on Android phones 2012-02-07

    Is there any way to check the bus and DDR memory clock frequency on any of the latest Android devices (ASUS Transformer Prime based on Tegra3, or Samsung Note based on Exynos 4212). Is there any way to set the bus frequency to a particular clock?

  • I have an AMD XP Athlon 2600 + / 333 MHz processor. What do I set the CPU External frequency MHz at 100/133/166/200? 2013-05-16

    Original title: I have an AMD XP Athlon 2600 + / 333 MHz processor What do I set the CPU External frequency MHz at 100/133/166/200 I'm running PC3200 DDR 400 MHz memory 1.5 GB 3 512 MB memory sticks --------------Solutions------------- Hi, Your quest

  • Rundll32.exe overusing the cpu clock when running Hoyle Puzzle Games 2005 2013-05-20

    Original title:rundll32.exe overusing the cpu clock My parents are having an issue with their PC; while running one of their applications (Hoyle Puzzle Games 2005), their CPU will speed up and get very noisy, more than is necessary. Having recently b

  • CoreAudio AudioTimeStamp.mHostTime clock frequency? 2009-03-23

    I'm running into a bit of a problem with AudioTimeStamps on the iPhone. When I'm running my application in the simulator the AudioTimeStamp.mHostTime appears to be in nanoseconds (1,000,000,000th of a second) whereas when running on my device (iPod T

  • How to caluate cpu cycles as that of QueryPerformanceCounter 2011-05-04

    I have the following code in win32 to calculate cpu cycles using QueryPerformanceCounter() LARGE_INTEGER ltime; <br> UINT32 cycles; <br> QueryPerformanceCounter(&ltime);<br> cycles = (UINT32) ((ltime.QuadPart >> 8) & 0

  • Detect current CPU Clock Speed Programmatically on OS X? 2012-03-30

    I just bought a nifty MBA 13" Core i7. I'm told the CPU speed varies automatically, and pretty wildly, too. I'd really like to be able to monitor this with a simple app. Are there any Cocoa or C calls to find the current clock speed, without actually

  • Measuring CPU clocks consumed by a process 2010-04-06

    I have written a program in C. Its a program created as result of a research. I want to compute exact CPU cycles which program consumes. Exact number of cycles. Any idea how can I find that? --------------Solutions------------- The valgrind tool cach

  • Mutli-core Processors: Does each "core" run at the full clock speed or some fraction of the full clock frequency? 2010-04-12

    Let's say you have a single (1) Intel/AMD x86-64 bit 2 GHz 8 core processor. Does each of the 8 cores all run at the full 2 GHz or, does each core run at some fraction of the full 2 GHz clock (e.g. 250 MHz)? --------------Solutions------------- Under

  • How to change clock frequency in Android? 2010-11-21

    I'm a new to software development on Android. I want to make an application like SetCPU that can manipulate CPU frequency in Android. But I couldn't find some related APIs or materials. I want to know following two things sincerely. Are there APIs to

  • My cpu clock !!!! 2012-09-22

    Hi i recently have problem with my PC freeze i my topic BSOD whale i playing games "win23k.sys\ntoskrnl.exe still not solved when boot i press F9 for infarmation system and i got this ! is this normal ? processor tybe : intel (R) core (TM) i5 process

  • CPU clock speed (Ghz) and Memory ram dropping 2014-07-27

    Hi, I have windows 7 profesional 64bit, My cpu is set to 4.2ghz( using bios to check my core speed and the software cpu-z), but it only show in my computer's property 2.79ghz. Before it was 3.06ghz and it drops continuously. My ram also i have 6gb ra

  • What is the clock source for the count returned by QueryPerfomanceCounter 2009-11-13

    I was under the impression that QueryPerformanceCounter was actually accessing the counter that feeds the HPET (High Performance Event Timer)---the difference of course being that HPET is a timer which send an interrupt when the counter value matches

  • Are there any modern Windows PCs that don't have at least one DirectShow capture device in them? 2011-11-20

    I am considering doing something that might seem strange to get a reliable time clock. I have to push buffers to a WiFi connected audio output device that does not do buffering or audio timestamp arbitration on its own. If I don't send the buffers at

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.091 (s). 11 q(s)