views:

110

answers:

5

I'm writing a DDE logging applet in visual c++ that logs several hundred events per minute and I need a faster way to keep time than calling GetSystemTime in winapi. Do you have any ideas?

(asking this because in testing under load, all exceptions were caused by a call to getsystemtime)

+1  A: 

Possibly crazy thought: do you definately need an accurate timestamp? Suppose you only got the system time say every 10th call - how bad would that be?

djna
Thanks, I will consider this as workaround and should have thought of it myself.. However I would like to have some very fine-grained time reporting; i was loking for a way to get a tick count without calling the API, or milliseconds, or something similar.
A: 

first of all, find out why your code is throwing exceptions (assuming you have described it correctly: i.e. a real exception has been thrown, and the app descends down into kernel mode - which is really slow btw.)

then you will most likely solve any performance bottleneck.

Chris J.

C Johnson
+1  A: 

As per your comments; calling GetSystemDateFormat and GetSystemTimeFormat each time will be a waste of time. These are not likely to change, so those values could easily be cached for improved performance. I would imagine (without having actually tested it) that these two calls are far more time-consuming than a call to GetSystemTime.

Fredrik Mörk
+3  A: 

There is a mostly undocumented struct named USER_SHARED_DATA at a high usermode readable address that contains the time and other global things, but GetSystemTime just extracts the time from there and calls RtlTimeToTimeFields so there is not much you can gain by using the struct directly.

Anders
A: 

First, The fastest way I could think is using RDTSC instruction. RDTSC is an Intel time-stamp counter instruction with a nanosecond precision. Use it as a combination with CPUID instruction. To use those instructions properly, you can read "Using the RDTSC Instruction for Performance Monitoring", then you can convert the nanoseconds to seconds for your purpose.

Second, consider using QueryPerformanceFrequency() and QueryPerformanceCounter().

Third, not the fastest but much faster than the standard GetSystemTime(), that is, using timeGetTime().

Vantomex