Jump to content
  • Advertisement
Sign in to follow this  
Mercenarey

Alternative to windows high-performance timer (QueryPerformanceCounter())?

This topic is 3608 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to be independent of Windows, which Im not at the moment. My performance-timer class still depends on the QueryPerformanceCounter() function of windows, which is a problem, because it is at so low a level, that it links into too much stuff. Even when I factory my way out of the problems, I find it too much luggage to carry around the entire windows library, just to be able to do high performance timing. Do you guys know of an alternative? Can std::clock() do it at a resolution comparable to QueryPerformanceCounter()?

Share this post


Link to post
Share on other sites
Advertisement
There is probably no way around it.
Make it generic/plugable.

There is probably something like this on the intarwebs, using some #ifdefs.

Share this post


Link to post
Share on other sites
I usually keep my projects very close to platform independence, except for one module whose header is called system.h . Then I write different .cpp files to implement those functions on specific platforms. Then I use a slightly different list of files in the projects for different platforms.

system.h:
#ifndef SYSTEM_H_INCLUDED
#define SYSTEM_H_INCLUDED

double time_in_seconds();

#endif





system_posix.cpp:
#include "system.h"
#include <sys/time.h>

double time_in_seconds() {
struct timeval tp;
gettimeofday(&tp,0);
return tp.tv_sec+.000001*tp.tv_usec;
}





system_standard.cpp:
#include "system.h"
#include <ctime>

double time_in_seconds() {
return std::clock()/(double)CLOCKS_PER_SEC;
}




... etc.

Share this post


Link to post
Share on other sites
Alvaro:
Yes, I know that way of doing it. That was what my comment about "factory my way out of the problems" was about. I use a BaseTimer class, and then ask the factory to give me a timer. Only the factory knows of the platform specifics (not all computers can run the PerformanceTimer - old ones have problems, so I need that flexibility in any case, so I can switch over to a StandardTimer in case the computer doesn't support Performance).

Still, it would suit me alot better to just use some standard stuff to use in all cases :(
And it should be possible. If MS can do it, why not Std?

Anyway, I just wondered if anyone knew if someone had done it, it doesn't look that way :(

Share this post


Link to post
Share on other sites
Quote:
Original post by Mercenarey
Still, it would suit me alot better to just use some standard stuff to use in all cases :(
And it should be possible. If MS can do it, why not Std?

Well, you don't get to redefine the language, and as it is, standard C++ does not provide a better timer than clock().

Quote:
Anyway, I just wondered if anyone knew if someone had done it, it doesn't look that way :(

Well, I told you how to get a pretty accurate timer on POSIX systems, which these days covers just about everything that is not Windows.

Share this post


Link to post
Share on other sites
Im only running on windows for now. I will make a note of your Posix-implementation.


Btw. it's not really about rewriting the language. It is just a matter of implementing a function that does the same as the Microsoft one. And that one accesses some special hardware (crystal on the motherboard or something).

How come that Microsoft can access this but noone else?

Share this post


Link to post
Share on other sites
Actually, reading the Linux man pages I just found something that might be closer to what you are doing on Windows:
#include <time.h>

double time_in_seconds() {
struct timespec tp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
return tp.tv_sec + .000000001 * tp.tv_nsec;
}




Compile with `-lrt'.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mercenarey

Still, it would suit me alot better to just use some standard stuff to use in all cases :(
And it should be possible. If MS can do it, why not Std?


Because hardware doesn't provide such guarantees. All timing methods have some sort of issues, some worse than others. The problem here isn't with software, but hardware. Reliable and accurate timing just isn't that important on PCs, and for applications that do require it, there's specialized hardware.

Quote:
Anyway, I just wondered if anyone knew if someone had done it, it doesn't look that way :(


Jan Wassenberg has.

As it appears from his articles, QueryPerformanceCounter isn't reliable, and one needs to write custom driver to provide proper timing under Windows as well.

Share this post


Link to post
Share on other sites
Some salt for gettimeofday(...). Although it's claimed to have very high precisions I've seen a few linux boxes in which the granularity was way higher.
I don't use linux much anymore so the fact I already found a machine with this issue is quite indicative.
As with clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);, this doesn't really look like a real time timer.

Quote:
Original post by Mercenarey
(not all computers can run the PerformanceTimer - old ones have problems, so I need that flexibility in any case, so I can switch over to a StandardTimer in case the computer doesn't support Performance).
I have been using the perf timer for years now and I had only a few glitches with it over the years. I just make sure the affinity mask is set correctly and it seems to work ok even on early multicores (although I admit I haven't tested much - only a few X2 and two Intel Quads).
Maybe it jerks on high system load as the article says. I'm not on a AAA budget so I am simply taking the risk.
Quote:

And it should be possible. If MS can do it, why not Std?
I suppose because MS it's basically x86. Alot of processors have RDTSC-like instructions at CPU ot system level but I suppose somebody may be using STD on cheap cell phones. You know, portability can be bad.

In my opinion, the performance counter is really unbeatable on most cases.

Share this post


Link to post
Share on other sites
Bah, I had no idea this would be so complicated. Incredible that no standard exists for a thing like timing, a part so fundamental and integral to computing. A standard that should exist in both hardware and software IMO.

Since my only problem really is living with having to #include windows, it is not really worth the effort atm.

Thanks for input guys. I never fail to get amazed at the level of expertise on this board.
(Too bad I can't seem to affect your ratings, because my own rating is so low)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!