Simple Timing using GetTickCount()

Hello helpful C++ community, i'm new to these forums. Hope I get the format right...i've never tried adding code to a post and so on.

I'm trying to create a simple time based solution where I can run my code (later) at specific time intervals. I thought it would be so simple. from using online searches and MSDN I came up with this code:

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
#include <iostream>
#include <windows.h>
using namespace std;

void wait(DWORD interval)
{
	DWORD startTime = GetTickCount();

	while(GetTickCount() < (startTime + interval))
	{
		//DO NOTHING JUST WAIT
	}
}

int main()
{
	int solutionTime = 5000; // 5 seconds
	int timer;
	int counter = 0;
	DWORD interval = 20;	// 10th of a second

	DWORD start = GetTickCount(); // program starts

	for (timer = 0; timer < solutionTime; timer+=interval)
	{
		wait(interval);
		counter++;
	}

	DWORD end = GetTickCount(); // program ends

	// temp feedback
	cout << "\n" << timer << "\n"<< counter << "\n";
	//cout << "\n" << start << "\t" << end << "\n";
	cout << (end - start); // how long program ran for
	
	char temp[99];
	cin >> temp;

return 0;
}


typical output is as follows:

5000
250
7800


it should be 5000 or very close to but it outputs 7800. I can see that it has stepped through the correct number of times so I imagine there is a huge delay caused by the while loop calling GetTickCount() so many times. I just can't get my head around an alternative.

However I have looked at others code and its pretty much the same as I have... using GetTickCount() in a while loop. 2 days this has been making me crazy : )

Does anyone have any advice?
I get a similar result for that code. I'm no expert, but I think the problem is the resolution of the timer available to the GetTickCount() function. For example, run this code to see what I mean:

1
2
3
4
5
6
7
int main()
{
  while(true)
  {
  	cout << GetTickCount() << endl;
  }
}


The output on my machine looks something like:


29144044
29144044
29144044
29144044
29144044
29144044
29144059
29144059
29144059
29144059
29144059
29144059
29144075
29144075
29144075
29144075
29144075
29144075
29144075
29144091


Notice that, although it's running flat-chat, there are distinct 'jumps' in the timer values (around 16ms on my machine). AFAIK, this is a manifestation of the timer resolution.

Now, consider that you're trying to wait 20ms by looking at these values. You'll see that it's possible that around 32ms (if the conditions are right) can pass before your 20ms condition will return. This explains the high value you were getting.

Now, I changed your 'interval' parameter to 200ms, and it gave a much more reasonable result (5070). Perhaps give that a try. If you do actually need to wait for more precise small amounts of time, perhaps others can give you hints on libraries to use.

Hope that helps :). Happy Easter.
Last edited on
Topic archived. No new replies allowed.