Skip to main content
gamedev.net
🔒 Locked

Which timer to use?

Started by Andre the Giant 3 replies 400+ views
Original Post
Andre the Giant
Andre the Giant
Im trying to fix my game loop so that the game speed will be constant even if I run the game on a faster (or slower) computer. I know this involves time somehow, and Im still deciding which approach I like best. Anyway, my problem is finding a suitable timer function. Its my understanding that there are many many different ways to obtain a reading of time in a program, and I was wondering which way is the best/ what other people use. I am currently using this code: clock_t currentTime; currentTime = clock(); to get a reading of time. This was the only function that I found returned a reading in milliseconds, which is what I want. (Seconds are too slow). There are two major problems with this function though: 1) Apparently the ''resolution'' is not 1 millisecond like I assumed. I think I read somewhere that it was closer to 50. Am I correct in assuming that this means I won''t be able to tell the difference between 1 millisecond of time and 49 milliseconds? Because that would be a bad thing. 2) This function only returns the time since the program started. Although this is useful for a lot of things, such as finding the relative time between two events, I would also like a function that returns the current system time, in milliseconds. If I recall my Java days correctly, there was a handy little funciton called getTimeInMillis() or something like that, which would be perfect for me (in c++) Also, it would be great if i could accomplish all this using standard c/c++, and not have to include a 3rd party library. But if I have to, I have to. Thanks to anyone who replies. Its not my fault I''''m the biggest and the strongest; I don''''t even exercise.
Its not my fault I''m the biggest and the strongest; I don''t even exercise.
spock
spock
If you need better resolution than that offered by clock(), you pretty much have to use something platform-specific.

AFAIK, most people use timeGetTime (good resolution, fast) or QueryPerformanceCounter (usually best resolution but slower and not guaranteed to be available) on Windows. Most *nix platforms have gettimeofday .

[edited by - spock on June 13, 2002 5:44:58 PM]
IndirectX
IndirectX
quote:
Original post by Andre the Giant
1) Apparently the 'resolution' is not 1 millisecond like I assumed. I think I read somewhere that it was closer to 50. Am I correct in assuming that this means I won't be able to tell the difference between 1 millisecond of time and 49 milliseconds? Because that would be a bad thing.


That's exactly right, which is why you need a better timer function. Check out timeGetTime (along with timeBeginPeriod and timeEndPeriod for NT) that will give you 1-ms resolution, and QueryPerformanceCounter for even higher resolution timing. All of these are Win32-specific.
quote:

2) This function only returns the time since the program started. Although this is useful for a lot of things, such as finding the relative time between two events, I would also like a function that returns the current system time, in milliseconds.


Check out time() and localtime() from the CRT. The Win32 way is GetSystemTime, or GetSystemTimeAsFileTime.


[edited by - IndirectX on June 13, 2002 5:46:24 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
eldee
eldee
when i wrote my timer class, i just made it
QueryPerformanceFrequency, and if it returned 0
then the system obviously didnt have a
high performance counter. If that test failed,
all my timing functions would use timeGetTime
instead (winmm.lib)

-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

DarkEmpire
DarkEmpire
here is the faster instruction to have the number of cycles:

rdtsc

Ex:

void CRdtscTimer::Start()
{
uint32 dwLow, dwHigh;

__asm
{
cpuid
rdtsc
mov dwLow, eax
mov dwHigh, edx
}

m_ui64NbCycles = (((uint64) dwHigh) << 32) | ((uint64) dwLow);
}

uint64 CRdtscTimer::Stop()
{
uint32 dwLow, dwHigh;

__asm
{
cpuid
rdtsc
mov dwLow, eax
mov dwHigh, edx
}
return (m_ui64NbCycles -= (((uint64) dwHigh) << 32) | ((uint64) dwLow));
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.