开发者

Cheapest way to generate timestamps under windows

What would be the fastest way to generate a timestamp for tons of events on windows?

A resolution of milliseconds is fine.

The obvious options are GetLocalTime(), GetTickCount() or even QueryPerformanceCounter().

Which of those (or some other c开发者_开发问答andidates) requires the least amount of clock cycles?


QueryPerformanceCounter() and GetTickCount() will get you the amount of time elapsed since the machine was started, and won't directly yield a timestamp. You could, in theory, store the time the machine was started and use those as an offset to get the current time, however I'm not sure how accurate that would be.

If millisecond accuracy isn't required, you could also look at the time() function and its variants. It will give you the time elapsed since the epoch in seconds. This should be quite fast and can be stored in its raw format until you need to display or manipulate it.

If performance is your biggest concern then you should profile each of the alternatives you come up with (most likely using QueryPerformanceCounter) and let that decide for you. I also think that the performance of a timestamp function is probably of little importance in most serious applications, so maybe give focus to the more complex parts of your application first.


The fastest possible way is to use x86 instruction RDTSC. It has several drawbacks, so please read the documentation. If you don't like inline assembly, VS has an intrinsic for it: __rdtsc()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜