• Welcome to Valhalla Legends Archive.
 

GetTickCount and finding the time between to events

Started by warz, April 10, 2006, 09:36 PM

Previous topic - Next topic

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:


dword startedat = gettickcount();
...
dword howLongSinceNow = gettickcount();
dword timeElapsed = howLongSinceNow - startedat;


would this be correct? i was doing this, and was receiving strange return values...

Joe[x86]

Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

K

The code you have is correct.

Quote from: J
Yes, but you might want to use long.

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()?

MyndFyre

Quote from: K on April 10, 2006, 09:48 PM
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()?

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

Quote from: warz on April 10, 2006, 09:36 PM
would this be correct? i was doing this, and was receiving strange return values...
Remember that a system tick is not necessarily correspondent to a second.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

UserLoser

You may recieve funny values if your system has been up for >47 days.  See QueryPerformanceCounter and QueryPerformanceFrequency

Joe[x86]

Quote from: K on April 10, 2006, 09:48 PM
The code you have is correct.

Quote from: J
Yes, but you might want to use long.

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()?

Excuse my stupidity. =p
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Spht

Quote from: warz on April 10, 2006, 09:36 PM
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:


dword startedat = gettickcount();
...
dword howLongSinceNow = gettickcount();
dword timeElapsed = howLongSinceNow - startedat;


would this be correct? i was doing this, and was receiving strange return values...

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.

warz

Quote from: Spht on April 11, 2006, 08:34 PM
Quote from: warz on April 10, 2006, 09:36 PM
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:


dword startedat = gettickcount();
...
dword howLongSinceNow = gettickcount();
dword timeElapsed = howLongSinceNow - startedat;


would this be correct? i was doing this, and was receiving strange return values...

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.

I now use QueryPerformanceCounter, and QueryPerformanceFrequency

Adron

Quote from: Spht on April 11, 2006, 08:34 PM
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.

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.