Best timer (clock) functions to use.

Started by
8 comments, last by bgoldbeck 12 years, 6 months ago
Hello,
I am currently using SDL's timer functions and they work great for me so far. However, the timer eventually will go out of range after (42 days) I believe it was. That does seem like a long time I know, but I was wondering if there are better alternatives like 'ctime' or something. Although on paper it looks to me ctime will share the same 42 days flaw, unless someone knows a workaround.
[size="2"]More sprinkles!
Advertisement

Hello,
I am currently using SDL's timer functions and they work great for me so far. However, the timer eventually will go out of range after (42 days) I believe it was. That does seem like a long time I know, but I was wondering if there are better alternatives like 'ctime' or something. Although on paper it looks to me ctime will share the same 42 days flaw, unless someone knows a workaround.


Do you expect your game to be running on someones machine for more than 42 days? Does SDL's timer work the way you want / need it to?
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Use a platform-dependent clock. In POSIX systems, I would use this:
#include <time.h>

double get_time_in_seconds() {
struct timespec t;

clock_gettime(CLOCK_REALTIME, &t);

return t.tv_sec + 1e-9 * t.tv_nsec;
}
Perhaps i'm being a little eccentric, but I like to not be limited. Which is why I chose SDL and OpenGL in the first place. Otherwise, I would have picked a single platform to work with. For me, as a programmer, I am inquisitive about learning another way that isn't limited to 42 days.
[size="2"]More sprinkles!

Use a platform-dependent clock. In POSIX systems, I would use this:
#include <time.h>

double get_time_in_seconds() {
struct timespec t;

clock_gettime(CLOCK_REALTIME, &t);

return t.tv_sec + 1e-9 * t.tv_nsec;
}



Ooh, I like that. How is that platform dependent though? Would time.h not work the same on multiple OS?
[size="2"]More sprinkles!

Perhaps i'm being a little eccentric, but I like to not be limited. Which is why I chose SDL and OpenGL in the first place. Otherwise, I would have picked a single platform to work with. For me, as a programmer, I am inquisitive about learning another way that isn't limited to 42 days.


It's actually 49 days. :)

If you use SDL to abstract away the differences between platforms, you have to live with the decisions that the SDL writers made, including the fact that the timer function returns a 32-bit number of milliseconds.

I normally keep one module for things that are platform specific. For that I provide a single header file and several implementations. When I've done this, I've often ended up with an implementation for Windows, one for POSIX systems and a fallback implementation for any other system, although I don't know what those systems would be. This might be the only function you need to write in that module.

Ooh, I like that. How is that platform dependent though? Would time.h not work the same on multiple OS?


clock_gettime is not part of C or C++. It is part of the POSIX specification, which is followed by pretty much every OS except for Windows. You need some other way of doing it for Windows.

It's actually 49 days. :)

I normally keep one module for things that are platform specific. For that I provide a single header file and several implementations. When I've done this, I've often ended up with an implementation for Windows, one for POSIX systems and a fallback implementation for any other system, although I don't know what those systems would be. This might be the only function you need to write in that module.


Very good idea alvaro : )
So then it would only be windows machines that crash after "49 Days".
[size="2"]More sprinkles!

[quote name='alvaro' timestamp='1316723757' post='4864842']
It's actually 49 days. :)

I normally keep one module for things that are platform specific. For that I provide a single header file and several implementations. When I've done this, I've often ended up with an implementation for Windows, one for POSIX systems and a fallback implementation for any other system, although I don't know what those systems would be. This might be the only function you need to write in that module.


Very good idea alvaro : )
So then it would only be windows machines that crash after "49 Days".
[/quote]

Thanks. You can write an implementation for Windows around QueryPerformanceCounter. It shouldn't be too hard.
This is actually good news for me, because I was thinking of eventually implementing a client/server and the server would be UNIX anyway. Still 49 days is probably a really long time for a server to run and a client would probably be extremely absurd to run for that long.
[size="2"]More sprinkles!

This topic is closed to new replies.

Advertisement