Archived

This topic is now archived and is closed to further replies.

Muzlack

better time method

Recommended Posts

Is there a more accurate time method than time()? Because I think that I have a loop that is run through so fast that it gets the same time and it gives the same random number for that reason. But I can''t be sure of that. I do have a srand((unsigned)time(NULL); at the beginning of my game, but I get the enemies at the same state at the same time...

Share this post


Link to post
Share on other sites
Don''t seed srand more than once - it''ll produce consistent but spurious results - if you display the results on the screen you''ll see an obvious pattern, which means the data isn''t very random anymore.

...
A better timer on Windows is QueryPerformanceCounter - but it''s expensive, takes like 5us on fast computer, and requires you to divide the results by QueryPerformanceFrequency - which means the precision varies between machines.

There''s also the x586 op code, rdtsc, which returns a 64bit CPU tick count. If you know (or accurately measure) the clock MHz of the computer, in thoery you can get sub-nanosecond timing on a fast machine - however in practice nanosecond accuracy is difficult to obtain. Precision and (32bit) roll-over are heavily dependent upon the clock of the computer. You need to call a serializing instuction prior to rdtsc, cpuid is the most popular choice.

Share this post


Link to post
Share on other sites