Wrapper for Query Performance Counter

Hi,

How do I write a Wrapper for the QueryPerformanceCounter API and QueryPerformanceFrequency

so that it could be used on different OS.

A snippet for the same written in C Language would be helpful or the steps to create

QueryPerformanceCounter/QueryPerformanceFrequency wrapper.

Kindly send me ASAP...

thanks in Advance, Hari

Reply to
Hari
Loading thread data ...

Why do you need a wrapper? Those API functions are very simple. The code will be really small. The QueryPerformanceFrequency gives you the frequency of the performance counter on the system and I believe it takes a pointer to a LARGE_INTEGER structure.

The QueryPerformanceCounter takes one parameter, a pointer to a LARGE_INTEGER structure. You use that to time your loops and the like. Of course, continually polling the Performance Counter for anything greater than or equal to ms resolution is not a good practice. This will use up 100% CPU needlessly and will be enough for most x86 CPU's to heat up quite rapidly.

-Isaac

Reply to
Isaac Bosompem

Thanks for the reply.

I have declared the following

LARGE_INTEGER GetHighResolution(PLARGE_INTEGER frequency) { #if defined(__i386__) && defined(__GNUC__) if (IsProcessorFeaturePresent( PF_RDTSC_INSTRUCTION_AVAILABLE )) { frequency->QuadPart = 2793000; /* FSB /4(Divider) i.e 533/4 = ~133 , System clock =133, Multiplier = 2800/133 =21 * Processor Speed = 133 * 21 = 2793; */ } #endif frequency->u.LowPart = 10000000; frequency->u.HighPart = 0; }

LARGE_INTEGER ReadHighClock(PLARGE_INTEGER counter) { #if defined(__i386__) && defined(__GNUC__) if (IsProcessorFeaturePresent( PF_RDTSC_INSTRUCTION_AVAILABLE )) { __asm__ __volatile__ ( "rdtsc" : "=a" (counter->u.LowPart), "=d" (counter->u.HighPart) );

} #endif }

and I have used it in my Code by using

ReadHighClock(&start_ticks);

anyfunction( ) / *a function whose timing needs to be calculated */

ReadHighClock(&end_ticks);

cputime.QuadPart = end_ticks.QuadPart- start_ticks.QuadPart;

p1 = (float)cputime.QuadPart;

p2 =(float)ticksPerSecond.QuadPart;

p3 = p1/p2;

printf ("\t Creat Process timing : %.9f micro sec \n ",p3);

Does it seem to be meaningful. If not could you please tell me how to do it.

Hari

Isaac Bosompem wrote:

Reply to
Hari

This function looks decent but it is using the RDTSC and not the performance counter. Also you seem to be putting a fixed frequency into the structure. Do not do this. Detect the processor frequency. To get a rough sampling of the processor clock frequency, sample the TSC using the RDTSC instruction, save it, do a Sleep(1000) API call and then sample it again and simply calculate the difference. You can do it multiple times to increase the accuracy of course.

This looks alright too, but again using the time stamp counter.

The way you are using the TSC is meaningful. It's just not a good idea to use the TSC or performance counter to time periodic events in the ms resolution.

-Isaac

Reply to
Isaac Bosompem

Kindly send me the source code for the same.

Isaac Bosompem wrote:

Reply to
Hari

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.