# How to get this queryperformancecounter code to output milliseconds?

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

## 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 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.

##### 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 on other sites
i guess it must be quadpart, i got the code from a vb source,

##### 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.

1. 1
Rutin
19
2. 2
3. 3
JoeJ
15
4. 4
5. 5

• 21
• 19
• 11
• 13
• 17
• ### Forum Statistics

• Total Topics
631697
• Total Posts
3001764
×