Valhalla Legends Forums Archive | C/C++ Programming | GetTickCount and finding the time between to events

AuthorMessageTime
warz
This can relate to bot uptimes, or times between issued commands, or whatever, but my question is.. in order to find out how much time has elapsed since (for example) an application has been executed, you'd do something like:

[code]
dword startedat = gettickcount();
...
dword howLongSinceNow = gettickcount();
dword timeElapsed = howLongSinceNow - startedat;
[/code]

would this be correct? i was doing this, and was receiving strange return values...
April 11, 2006, 2:36 AM
JoeTheOdd
Yes, but you might want to use long.
April 11, 2006, 2:39 AM
K
The code you have is correct.

[quote author="J"]
Yes, but you might want to use long.
[/quote]

so instead of using a DWORD, which is an unsigned 32 bit integer, which is what GetTickCount() returns, he should use a long, which is a signed 32 bit integer which may not be large enough to hold the return value from GetTickCount()?
April 11, 2006, 2:48 AM
Myndfyr
[quote author=K link=topic=14745.msg150373#msg150373 date=1144723738]
so instead of using a DWORD, which is an unsigned 32 bit integer, which is what GetTickCount() returns, he should use a long, which is a signed 32 bit integer which may not be large enough to hold the return value from GetTickCount()?
[/quote]

Shh, Joe is trying to make up for incompetence by typing with only one letter visible.  ;)

[quote author=warz link=topic=14745.msg150370#msg150370 date=1144722991]
would this be correct? i was doing this, and was receiving strange return values...
[/quote]
Remember that a system tick is not necessarily correspondent to a second.
April 11, 2006, 3:24 AM
UserLoser
You may recieve funny values if your system has been up for >47 days.  See QueryPerformanceCounter and QueryPerformanceFrequency
April 11, 2006, 3:31 AM
JoeTheOdd
[quote author=K link=topic=14745.msg150373#msg150373 date=1144723738]
The code you have is correct.

[quote author="J"]
Yes, but you might want to use long.
[/quote]

so instead of using a DWORD, which is an unsigned 32 bit integer, which is what GetTickCount() returns, he should use a long, which is a signed 32 bit integer which may not be large enough to hold the return value from GetTickCount()?
[/quote]

Excuse my stupidity. =p
April 12, 2006, 12:53 AM
Spht
[quote author=warz link=topic=14745.msg150370#msg150370 date=1144722991]
This can relate to bot uptimes, or times between issued commands, or whatever, but my question is.. in order to find out how much time has elapsed since (for example) an application has been executed, you'd do something like:

[code]
dword startedat = gettickcount();
...
dword howLongSinceNow = gettickcount();
dword timeElapsed = howLongSinceNow - startedat;
[/code]

would this be correct? i was doing this, and was receiving strange return values...
[/quote]

Gettickcount returns system up time in milliseconds as an unsigned 32 bit integer, so can therefore only go up to 0xffffffff (4294967295 milliseconds, which as UL mentioned is about 47 days), after that it begins at 1 again.  So if between the two time samples the tick resets and you try to find "timeElapsed," you will get a negative number.

That should clear things up.

As UL mentioned, you may want to look into QueryPerformanceCounter and QueryPerformanceFrequency, from which uptime can be computed to 64 bit accuracy.
April 12, 2006, 1:34 AM
warz
[quote author=Spht link=topic=14745.msg150470#msg150470 date=1144805697]
[quote author=warz link=topic=14745.msg150370#msg150370 date=1144722991]
This can relate to bot uptimes, or times between issued commands, or whatever, but my question is.. in order to find out how much time has elapsed since (for example) an application has been executed, you'd do something like:

[code]
dword startedat = gettickcount();
...
dword howLongSinceNow = gettickcount();
dword timeElapsed = howLongSinceNow - startedat;
[/code]

would this be correct? i was doing this, and was receiving strange return values...
[/quote]

Gettickcount returns system up time in milliseconds as an unsigned 32 bit integer, so can therefore only go up to 0xffffffff (4294967295 milliseconds, which as UL mentioned is about 47 days), after that it begins at 1 again.  So if between the two time samples the tick resets and you try to find "timeElapsed," you will get a negative number.

That should clear things up.

As UL mentioned, you may want to look into QueryPerformanceCounter and QueryPerformanceFrequency, from which uptime can be computed to 64 bit accuracy.
[/quote]

I now use QueryPerformanceCounter, and QueryPerformanceFrequency
April 12, 2006, 7:41 AM
Adron
[quote author=Spht link=topic=14745.msg150470#msg150470 date=1144805697]
Gettickcount returns system up time in milliseconds as an unsigned 32 bit integer, so can therefore only go up to 0xffffffff (4294967295 milliseconds, which as UL mentioned is about 47 days), after that it begins at 1 again.  So if between the two time samples the tick resets and you try to find "timeElapsed," you will get a negative number.
[/quote]

Actually you will not get a negative number. You will get the correct number for any timeElapsed interval shorter than 47 days. Whether the timer has wrapped during the interval or not does not matter.
April 12, 2006, 3:09 PM

Search