High res time - QueryPerformanceCounter

I'm wondering why I can't get QueryPerformanceFrequency/QueryPerformanceCounter to work. I need microsend level timing info but these API's return TRUE/0, indicating that they're not available.  I'm working on a Pentium notebook
mgrinnellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RONSLOWCommented:
Try using the multimedia timers instead - eg. timeGetTime()

0
mgrinnellAuthor Commented:
I appreciate your suggestion, but as I mentioned I need a timer with a resolution of a microsecond or so -- timeGetTime() is only good to the millisecond.  The documentation suggests that the availability of QueryPerformanceCounter() is a hardware issue, but my understanding was that on a Pentium system it just queried timing info from the chip.  I'd like to know if there's anything I can do to make that API work on my machine, or if there's another way to get timer info with the accuracy I need.
0
davmarcCommented:
You're in the right direction - QueryPerfomance*() is the most accurate way to time code.

Did you try your code on a desktop Pentium?
They should work...maybe the problem is involved in the particular organization of notebooks (hmmm...).

Davide Matcato.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

mgrinnellAuthor Commented:
I sent it to a friend on a desktop Pentium and he got the same result; QueryPerformanceFrequency() returns TRUE.  The Performance Monitoring section of the help file talks a lot about registry manipulation, but I think this is just to monitor system functions, not to enable the QueryPerformance* API's.  I'm still looking...
0
davmarcCommented:
mgrinnel,
FALSE = 0 = counter not available
TRUE = non-0 = counter available

If it returns TRUE (which is *not* zero as you mentioned in the original question), that means the counter is supported!

Davide Marcato.
0
mgrinnellAuthor Commented:
Davide,

Sorry I wasn't clear.  QueryPerformanceFrequency() returns TRUE and QueryPerformanceCounter() returns 0.  For QueryPerformanceFrequency(), a return value of TRUE means that the API is not supported on your machine.
0
davmarcCommented:
>For QueryPerformanceFrequency(), a return value of TRUE means
>that the API is not supported on your machine.

This is simply not correct. Both QPFrequency() e QPCounter() return TRUE if the hardware counter *is* supported.
Hence they are very unlikely to return different values...

Try the following code:

#include <windows.h>

void main()
{
      LARGE_INTEGER value;
      BOOL result;
      result = QueryPerformanceFrequency(&value);
      result = QueryPerformanceCounter(&value);
}

Running it under a debugger, what are the values returned by the two APIs? On my computer I see "1" and "1".

Davide Marcato.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RONSLOWCommented:
Why is this a point of contention - check out the documentation it is clearly stated:

==========
QueryPerformanceFrequency

The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists.

BOOL QueryPerformanceFrequency(
LARGE_INTEGER *lpFrequency // address of current frequency);

Parameters
lpFrequency
Points to a variable that the function sets, in counts per second, to the current performance-counter frequency. If the installed hardware does not support a high-resolution performance counter, this parameter can be to zero.

Return Values
If the installed hardware supports a high-resolution performance counter, the return value is nonzero.

If the installed hardware does not support a high-resolution performance counter, the return value is zero.
==========

SO a value of TRUE (non-zero) means "the installed hardware supports a high-resolution performance counter".  A value of FALSE (0) means "the installed hardware does not support a high-resolution performance counter".

It is that simple.

For QueryPerformanceCounter the dos says:

==========
QueryPerformanceCounter

The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter, if one exists.

BOOL QueryPerformanceCounter(
LARGE_INTEGER *lpPerformanceCount // address of current counter value);

Parameters
lpPerformanceCount
Points to a variable that the function sets, in counts, to the current performance-counter value. If the installed hardware does not support a high-resolution performance counter, this parameter can be to zero.

Return Values
If the installed hardware supports a high-resolution performance counter, the return value is nonzero.

If the installed hardware does not support a high-resolution performance counter, the return value is zero.
==========

Here (also) non-zero (TRUE) means it is supported and zero (FALSE) means it is not.


If both calls are TRUE, all is well, if both are FALSE then not supported.  However, if one is TRUE and the other FALSE - then we have a more interesting situation.

Are you passing a valid LARGE_INTEGER to the calls?

BTW: QueryPerofrmanceXXX routines don't guarantee microsecond timing - it depends on your PC.

0
davmarcCommented:
Ronslow, apart from also reporting the documentation, you said exactly the same as me! :-)
TRUE = supported, FALSE = not supported.

Davide Marcato.
0
RONSLOWCommented:
My point is - why was there a question about return values in the  first place?

Also thought I'd lend a bit of officiality by quoting the documentation (just so there could be no doubt about it).

I'm still confused, however, about what return values mgrinnell is actually getting.  Can one call return true and the other false.  
Does GetLastError provide any extra information on the possible reason if this is so?

0
mgrinnellAuthor Commented:
Yikes.  My documentation, which I'll quote here so you don't think I'm insane, says:
---------
The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists.

BOOL QueryPerformanceFrequency(

    LARGE_INTEGER  *lpliPerformanceFreq       // address of current frequency
   );      
Parameters

lpliPerformanceFreq

Points to a variable that the function sets, in counts per second, to the current performance-counter frequency. If the installed hardware does not support a high-resolution performance counter, this parameter can be to zero.

Return Value

If the installed hardware supports a high-resolution performance counter, the return value is FALSE; otherwise, it is TRUE. To get extended error information, call GetLastError.
---------
So here, QPF() returning TRUE is consistent with QPC() returning 0. I think GetLastError was returning 127 - PROC_NOT_FOUND.  I'm working with an older version of our product that compiles with an older version of msvc, though.  I'll check it out with the latest and greatest next week and report back.

Thanks again for your help!
Mark
0
davmarcCommented:
What version of VC++ you are reading the documentation of?
I refer to VC++ 5.0.

Davide Marcato.
0
mgrinnellAuthor Commented:
(Sorry for the delay - I've been in Germany for a few weeks.) You're exactly right, the API works fine on my machine -- my only problem was paying attention to the old, incorrect documentation. It's funny, I was converting this from an OS/2 version that didn't do any error checking - If I hadn't added the check along the way, I would have been fine.  I guess that'll teach me.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.