Sign in to follow this  
johnnyBravo

How to get this queryperformancecounter code to output milliseconds?

Recommended Posts

hi heres some code i got from a website, but the problem is it only outputs seconds, and i want milliseconds. How would i get milliseconds?
LARGE_INTEGER freq, count1, count2;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&count1);
QueryPerformanceCounter(&count2);

int overhead = count2.LowPart - count1.LowPart;
QueryPerformanceCounter(&count1);

//
for(int i=0;<i<20000;i++) {
     int a = 3*4*2;
}
//

QueryPerformanceCounter(&count2);
std::cout << "Code took "<< count2.LowPart- count1.LowPart- overhead) / freq.LowPart << " seconds";


Like I don't get how I would get the milliseconds from the line: std::cout << "Code took "<< count2.LowPart- count1.LowPart- overhead) / freq.LowPart << " seconds"; thanks

Share this post


Link to post
Share on other sites
use QueryPerformanceFrequency() to get the number of times per second the counter counts. Then divide the frequency by 1000.
This now tells u how many times per milli second the counter counts.

Then do yer sums.

ace

PS. I could be wrong but don't u use the quadpart of the LARGE_INTEGER.

so time2.quadpart - time1.quadpart.

Share this post


Link to post
Share on other sites
count2.LowPart- count1.LowPart- overhead) / freq.LowPart * 1000

if it gave you seconds, just multiply it by 1000 as i did above since there are 1000 milliseconds in one second.

Share this post


Link to post
Share on other sites
You should use the entire value, not just the lower 32 bits. The QPC frequency on many machines is the CPU frequency. That means that not only can the frequency be too big to fit in 32 bits, but the counter can overflow the lower half in less than a second.

You are doing integer operations which means that (a-b)/f*1000 gives a completely different result from (a-b)*1000/f. (a-b)*1000/f will give you a more accurate result.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this