The simplest method of making a script run faster is to buy a faster processor. Unfortunately, this isn't always an option. You may need to optimize your script to run faster. This is difficult if you can't measure the time it takes to run the portion of the script that you are trying to optimize.
The time command is the solution to this problem. time will measure the length of time that it takes to execute a script. You can then modify the script, rerun time and see how much you improved it.
Note: accurate timings of scripts require that you run them often enough for random variations in the computer's performance to average out. Therefore you should choose the repeat count with some care.
proc timetst1 {lst} { set x [lsearch $lst "5000"] return $x } proc timetst2 {array} { upvar $array a return $a(5000); } # Make a long list and a large array. for {set i 0} {$i < 5001} {incr i} { set array($i) $i lappend list $i } puts "Time for list search: [ time {timetst1 $list} 100]" puts "Time for array index: [ time {timetst2 array} 100]"
Time for list search: 93.83 microseconds per iteration Time for array index: 1.52 microseconds per iteration