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+