how can i use QFC and QPC

i am a beginer learner of c++
i am using borland c++ ver 5
and my opreating system is windows xp

i want to use the two API functions

QueryPerformanceCounters()
QueryPerformanceFrequency()

plz someone teaches me step by step how to add these two functions to my code

sine i want to get an accurate process time for example for the following process:

for (int i=0 ;i<1000;i++)
x+=x;


Last edited on
plz i need help here):
Try google with the function name and "MSDN" as search string. Usually the first hit is the Microsoft Developer Network Documentation. That's an awesome documentation site about practically all Windows-API functions in any language supported by Microsoft. It usually also has some examples of usage.

If you still get stuck, make a small program showing your problem and try again in the Windows Programming Forum.

Ciao, Imi.
The idea is that you call QueryPerformanceFrequency() once to get the frequency, then you call QueryPerformanceCounters() at each checkpoint. The difference divided by the frequency gives you time difference in seconds. You can get something like 100 nano-sec accuracy on a standard modern Intel box.

The number type is large, LARGE_INTEGER in fact, with is a 64 integral value. When you do the divide, remember to do a floating point divide.
thank you imi and kbw for your response

i did read about these two functions in msdn
and i understood their jobs

but when i add them to my code alot of errors occours

does these functions are standered in c++ , since an error msg undefined function and

about LARGE_INTGER is not defined too.

so my problem with these two function in the way of decleration and there is no type number called LARGE_INTEGER or LONGLONG

i am sorry for my too questions.
They are Windows platform specific functions and not generally available C++ functions.

You must include windows.h to get them.
thanks alot for your help
i did the following and the problem was that i did not include windows.h


LARGE_INTEGER t1,t2,freq;

QueryPerformanceCounter(&t1);
for( int i = 0 ; i < size ; i++)
for(int j = 0 ; j < size ; j++)
for(int k = 0 ;k < size ; k++)
C[i][k] += A[i][j]* B[j][k];
QueryPerformanceCounter(&t2);
QueryPerformanceFrequency( &freq );
cout<<(t2.QuadPart-t1.QuadPart)/freq.QuadPart;

the code works

but i am wondering what is defferent between

getting process time by using function clock() and QueryPerformanceCounters()

since i read that clock is not accurate to get the process time(wall clock ) while QPC gives accurate time of process
for me , both of them gave approxi same results
When you do the divide, remember to do a floating point divide.


You want:
cout << (double)(t2.QuadPart - t1.QuadPart) / (double)freq.QuadPart;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <windows.h>
#include <iostream>
#include <iomanip>
#include <set>
#include <time.h>
#include <stdlib.h>

int main()
{
	LARGE_INTEGER freq;
	QueryPerformanceFrequency(&freq);

	LARGE_INTEGER start;
	QueryPerformanceCounter(&start);

	// Do some time consuming work--Monte Carlo's PI to 8 places
	srand(time(0));
	double pi = 0.0;
	unsigned long in = 0, out = 0;
	unsigned long count = 0;
	while (!(3.14159264 < pi && pi < 3.14159266))
	{
		double x = (double)rand() / (double)RAND_MAX;
		double y = (double)rand() / (double)RAND_MAX;
		double h = x*x + y*y;

		if (h <= 1.0)
			++in;
		else
			++out;

		pi = (4.0 * in) / ++count;
	}
	std::cout << "pi found to be: "
		<< std::setprecision(8) << std::fixed << pi << std::endl;

	LARGE_INTEGER stop;
	QueryPerformanceCounter(&stop);

	double delay = (double)(stop.QuadPart - start.QuadPart) / (double)freq.QuadPart;
	std::cout << "Time taken: "
		<< int(delay * 100000 + 0.5) << " micro seconds" << std::endl;

	return 0;
}

thaks kbw for telling me that,

i used these two functions to see the results of time process

sine i used clock function , but the results were not too much different.

using clock function gave me about 19000 ms which i have been told is very high and the

mistake was in using clock function which is not sutiable here to calculate the process time

so i tried to use QPC but the result was not too much different, about 16000 ms
The clock functions, based on GetSystemTime, sample time in a different way (at a lower rate), and so have a lower resolution.

If a box has special timer hardware, it'll hook into the QueryPerformance functions. So if you always use those, you'll pickup fancy hardware by default.
thanks alot kbw for your help although i did not understand but hopfuly i get understand with time and get more experince in programming

i appreciate your help (:
Topic archived. No new replies allowed.