View unanswered posts | View active topics It is currently Tue Apr 21, 2026 2:47 pm



Reply to topic  [ 5 posts ] 
 Command Timers 
Author Message
Gameop
User avatar

Joined: Tue Nov 19, 2002 3:00 am
Posts: 1050
Location: USA
Unread post 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?

_________________
Dark Dominion TWGS
Telnet://twgs.darkworlds.org:23
ICQ#31380757, -=English 101 pwns me=-
"This one claims to have been playing since 1993 and didn't know upgrading a port would raise his alignment."


Tue Nov 25, 2008 6:33 pm
Profile ICQ
Commander
User avatar

Joined: Tue Oct 07, 2003 2:00 am
Posts: 1134
Location: Augusta, GA
Unread post 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+

_________________
Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.


Tue Nov 25, 2008 7:27 pm
Profile WWW
Veteran Op
User avatar

Joined: Thu Jun 02, 2005 2:00 am
Posts: 5558
Location: USA
Unread post 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.

_________________
May the unholy fires of corbomite ignite deep within the depths of your soul...

1. TWGS server @ twgs.navhaz.com
2. The NavHaz Junction - Tradewars 2002 Scripts, Resources and Downloads
3. Open IRC chat @ irc.freenode.net:6667 #twchan
4. Parrothead wrote: Jesus wouldn't Subspace Crawl.

*** SG memorial donations via paypal to: dpocky68@booinc.com
Image


Tue Nov 25, 2008 8:48 pm
Profile ICQ WWW
Commander
User avatar

Joined: Tue Oct 07, 2003 2:00 am
Posts: 1134
Location: Augusta, GA
Unread post 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+

_________________
Claim to Fame: only guy to ever crack the TW haggle algorithm, and fig/shield/hold price formula, twice.


Wed Nov 26, 2008 12:14 pm
Profile WWW
Veteran Op
User avatar

Joined: Thu Jun 02, 2005 2:00 am
Posts: 5558
Location: USA
Unread post 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.

_________________
May the unholy fires of corbomite ignite deep within the depths of your soul...

1. TWGS server @ twgs.navhaz.com
2. The NavHaz Junction - Tradewars 2002 Scripts, Resources and Downloads
3. Open IRC chat @ irc.freenode.net:6667 #twchan
4. Parrothead wrote: Jesus wouldn't Subspace Crawl.

*** SG memorial donations via paypal to: dpocky68@booinc.com
Image


Wed Nov 26, 2008 12:33 pm
Profile ICQ WWW
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by wSTSoftware.