Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DLL Talk / Timing using the system clock

Author
Message
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 5th Aug 2005 04:01 Edited at: 5th Aug 2005 04:02
I've written a DLL that uses the RDTSC command to read the clock count of the processor since power on.

This could be especially useful in benchmarking small amounts of code since it records the exact number of clock cycles passed between two points.

There are four commands in the DLL:

startTSCtimer - records the clock count to act as a starting figure for the timer

stopTSCtimer - records the clock count to act as an ending figure for the timer

getTSCtime() - returns the difference between the two recorded times

getClockCount() - returns the actual clock count since power on

I've included some source code to show some examples of timing.

Attachments

Login to view attachments
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Aug 2005 12:29
Sounds useful. Does it return a real time, or a time relative to the speed of your CPU? That is, a 3Ghz machine would return more clock cycles than a 2 Ghz machine in 1 second.

Would this be useful as a program loop counter, to get the real time it takes to do one cycle of your code? Or is it too intensive in itself?

You might guess, I'm a speed junkie, I need to know exactly how my program is performing!

Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 6th Aug 2005 01:38
It is relative to the speed of your computer, the clocks in a 3Ghz would increase faster than in a 2Ghz machine, but there is a way to normalize it if you measure the amount of clock cycles in a given time and use it to adjust the numbers (I've shown an example of this in the source code with the variable OFFSET#)

With regard to speed, the instruction itself is said to take 6-7 clock cycles to read the counter from the processor, and there is some overhead in calling a dll to do it, I ran a test using this code:



and on my machine it records 76 clock cycles between the two reads of the clock, which is still very quick (done on a 1.5Ghz CPU is about 0.00005 milliseconds = 50 nanoseconds)
Cryptoman
20
Years of Service
User Offline
Joined: 24th Nov 2003
Location: Utah Mountains
Posted: 9th Aug 2005 06:10
@sparky, you can call the QueryPerformanceFrequency funtion which you divide by. (Gives you the speed of the ticker)

Use that as a divisor and all results will be real time.


IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 9th Aug 2005 13:12
One warning. I find that the performance timer is slower than the DBPro timer function (on a P3), so you only want to use this where you need absolutely accurate timing.

@Sparky,
Download the source code for my Matrix1Utility plugin. In the System.cpp file you see how to use the QueryPerformanceFrequency function.

*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins and source code http://www.matrix1.demon.co.uk
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 9th Aug 2005 22:24
I hadn't heard of the QueryPerformance commands before, I found your code IanM thanks, I'll keep it in mind for future reference.

However, the command I use the grab the time is the RDTSC command built into the processor and takes about 100 clock cycles when called from DBPro.

The Timer() command takes about 500, give or take a couple hundread, clock cycles, and the queryPerformanceCounter seems to take over 2000.

Benchmark without performanceCounter:



I agree that timer() should still be used for real-time timing, since it isn't slow and is going to be far more accurate to real time over greater timing spans - more and more processors come with speed step, which throws the clock to real-time calculations way off.

RDTSC is more useful for relative timing, like comparing sections of code for speed.
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 10th Aug 2005 15:34
My mistake. When you said RDTSC, I assumed that you were using the performance counter functions to access the timings. If you are accessing them directly, then of course, they will be faster.

*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins and source code http://www.matrix1.demon.co.uk

Login to post a reply

Server time is: 2024-05-06 03:38:54
Your offset time is: 2024-05-06 03:38:54