www.ClassicTW.com
https://mail.black-squirrel.com/

Command Timers
https://mail.black-squirrel.com/viewtopic.php?f=15&t=21216
Page 1 of 1

Author:  Kaus [ Tue Nov 25, 2008 6:33 pm ]
Post subject:  Command Timers

What is the best way to time a command or series of commands? In the past I think I remember asking or reading something to the extent of using TWX's time commands but does anyone have any actual examples?

Author:  ElderProphet [ Tue Nov 25, 2008 7:27 pm ]
Post subject:  Re: Command Timers

It depends on how fine you need the timer to be. I get a start time and end time for lots of long routines, or I use the very precise getTimer for benchmarking events that are very short.

This should be fine for events that last a few seconds or several hours.
Code:
getTime $start
gosub :startZTM
echo "*Started: " $start ", Ended: " TIME


For really tight precision, as in sub-milliseconds, you need getTimer. The getTimer command retrieves the "tick count" from your CPU. A CPU increments the tick count once per clock cycle... so a 1 GHz processor increments the tick count 1 billion times per second. You can get a starting and ending tick count, and that will let you get the ratio of how fast one event is compared to another, or you can do the time conversion using your CPU hertz to convert it to seconds.

Code:
getTimer $time1
echo "*Dude, my PC is fast"
getTimer $time2
echo "*Your mom is fast."
getTimer $time3
# my CPU is a quad-core 2.4 GHz, which is still only 2.4 GHz
setVar $hz 2400000000
setVar $event1Time ($time2 - $time1)
setVar $event2Time ($time3 - $time2)
setPrecision 6
echo "*Event 1 took " (($time2 - $time1) / $hz) " seconds."
echo "*Event 2 took " (($time3 - $time2) / $hz) " seconds."
setPrecision 2
if ($event1Time < $event2Time)
        echo "*Event 2 took " ($event2Time / $event1Time) " times longer than Event 1."
else
        echo "*Event 1 took " ($event1Time / $event2Time) " times longer than Event 2."
end
setPrecision 0

As if that weren't enough flexibility, the getTime routine can also be sent a format string, which I'm happy to elaborate on if anyone needs that info.

+EP+

Author:  Singularity [ Tue Nov 25, 2008 8:48 pm ]
Post subject:  Re: Command Timers

Keep in mind that if you use ticks you're going to have several timing problems that could skew the accuracy of the results. In order for ticks to be reliable you have to compute the ticks per second, rather than assuming a fixed value, inside of the script.

Author:  ElderProphet [ Wed Nov 26, 2008 12:14 pm ]
Post subject:  Re: Command Timers

Let me elaborate on that a bit. Ticks are a constant value, one per CPU Hz. But, when you are dealing with something that precise, the slightest thing can skew results from one run to the next. I once tried to time a pre-arrayed nearfig lookup, and the results varied significantly between runs. I learned that you need to assign values to all of your variables before timing a routine, including the variable which will hold the getTimer result, or the times may not accurately reflect whatever you were timing.

My advice is to initialize the variables, and time multiple iterations of a routine to determine it's real duration.
Code:
# Intialize all variables
getTimer $start
getTimer $stop
gosub :routine
# Obviously, the routine has to do something and return to be testable like this
setVar $totalTicks 0
setVar $i 1
while ($i <= 1000)
          getTimer $start
          gosub :routine
          getTimer $stop
          add $totalTicks ($stop - $start)
          add $i 1
end
# adjust the $hz variable as necessary for your CPU
setVar $hz 2400000000
# 18 is the limit for significant digits
setPrecision 18
echo "*The routine takes " (( $totalTicks / 1000) / $hz) " seconds.*"
+EP+

Author:  Singularity [ Wed Nov 26, 2008 12:33 pm ]
Post subject:  Re: Command Timers

Quote:
Let me elaborate on that a bit. Ticks are a constant value, one per CPU Hz


Hehe, ever used something like core temp or CPU-z to watch the processor fluctuate over time? They're anything but constant... ;)

Right now my 3ghz CPU is running at 3013.89mhz. The FSB is slightly over 200 (200.93) w/ a 15x multi. Anything that draws a load on the PSU can change that value a little (like if you've got something active in the DVD tray or if you've got something plugged into the USB), altho most reach a fairly constant equilibrium of around ~1mhz. Of course if you overclock all bets are off.

Add in a throttle like AMD's "cool and quiet" and it gets messy in a hurry.

Which is why you really need to have a load on the system and compute the ticks in real-time. It won't be 100% accurate, but it'll be close enough. Reminds me of the old timer routines we'd use for coding stuff back before windows.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/