program efficiency timer problem

I just want a simple function that I can call twice per run of my program that will return the time in milliseconds so I can make my programs more efficient. When I run the program I end up getting some small decimal (.002 ms to .0017 ms ish). Similar programs on ProjectEuler.net have gotten around 15 to 14 milliseconds. I know that I’m a skilled programmer (sarcasm) and all… The stuff between lines 13 and 27 can be ignored; I only included that section for reference. Any explanation for such a small output would be greatly appreciated, as having a handy little function like this at my disposal would really help make my coding more efficient.


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
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <windows.h>
using namespace std;

float timer();

#define LIMIT 100

int main()
{
	timer();

///////////////////////////////
	int sumSq = 0;
	int sqSum = 0;
	
	for(int i = 1;i < LIMIT + 1;i++)
	{
		sumSq += i * i;
		//sqSum += i;
	}
	
	sqSum = LIMIT / 2;
	sqSum *= LIMIT + 1;

	sqSum *= sqSum;
///////////////////////////////

	cout<<timer()<<" ms"<<endl;

	cin.get();
	return 0;
}
float timer()
{
	static LARGE_INTEGER iBeg, iEnd, iFreq;
	float fCount, fTime = 0;
	
	if(iFreq.QuadPart == 0)// first call
	{
		QueryPerformanceFrequency(&iFreq);
		QueryPerformanceCounter(&iBeg);
	}
	else // second call
	{
		QueryPerformanceCounter(&iEnd);
		iBeg.QuadPart = iEnd.QuadPart - iBeg.QuadPart;
		
		fCount = static_cast<float>(iBeg.QuadPart);
		fTime = static_cast<float>(iFreq.QuadPart);
		
		fTime = fCount / fTime;
		fTime = fTime * 1000;// gives me ms
	}
	return fTime;
}
Last edited on
What you are trying to do is really something of an illusion -- the best way to time sections of code is to execute it a zillion times.

But, there are the following:
http://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759

Good luck!
There's a bug in your formula for calculating sigma(i=[1;n])(i).

n/2*(n+1) works only if n is a float. If n is an integer, the integer division messes up the result for n%2!=0:
3/2==1, so 3/2*(3+1)==4, but 1+2+3==6
(n*n+n)/2 works regardless of the type of n.
What you are trying to do is really something of an illusion -- the best way to time sections of code is to execute it a zillion times.


Did you mean execute a section of code a x number of times, time it, and then divide the time by x? Any ways I found just what I was looking for at:

http://www.codeguru.com/cpp/cpp/date_time/general/article.php/c10333/
> Did you mean...

Yes.

> I found just what I was looking for...

You need to be aware of the issues that come with using the Microsoft Performance Timer API.
http://www.gamedev.net/community/forums/topic.asp?topic_id=423176 is a good read (plus it has some good code posted by Kest down at the bottom).

You can also google around "QueryPerformanceTimer" for more information. I am not saying that you shouldn't use it -- just make sure you use it carefully.

Good luck!
Last edited on
Arg, I use a laptop... I havent heard of most of those functions, so I have some learning to do tonight. Thanks for the heads up on QueryPerfromanceFrequency() and QueryPerformanceCounter().
Topic archived. No new replies allowed.