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.
After you've run the example, play with the size of the loop counters in
timetst1
and timetst2
. If you make the inner loop counter 5 or less, it may take longer to execute timetst2
than it takes for timetst1
. This is because it takes time to calculate and assign the variable k
, and if the inner loop is too small, then the gain in not doing the multiply inside the loop is lost in the time it takes to do the outside the loop calculation.time
script
?count?
- Returns the number of milliseconds it took to execute
script
. Ifcount
is specified, it will run the scriptcount
times, and average the result. The time is elapsed time, not CPU time.
Example
proc timetst1 {lst} {set x [lsearch $lst "5000"]return $x }ray} { upvar $arrayproc timetst2 {a r a return $a(5000); }ge array. for {set i 0} {$i < 5001} {# Make a long list and a la rincr i} { set array($i) $i lappend list $i }" puts "Time for array index: [ time {timetst2 array} 10puts "Time for list search: [ time {timetst1 $list} 10]]"
No comments:
Post a Comment