The above example involving summing the first 1000 integers required 630 milliseconds on a DecStation 3100 using wish. Using TC, it required only 57 milliseconds for speedup of 11 times. Some more examples follow which illustrate the relative strengths and weaknesses of TC. Note that in no case is TC slower than the original Tcl interpreter.
Test 1: Simple variable access and the incr command. This is espeically fast under TC, because only the first iteration needs to parse the value of counter. The time command is used to perform iteration because a for loop would interact with the timing data. Note that the times are per iteration in this case.
set counter 0 puts stdout [ time {incr counter} 50000 ]
Performance- μsec per iter. | ||
Uncompiled Tcl: | 219 | μsec |
Compiled Tcl: | 18 | μsec |
Speedup: | 12.17x |
Test 2: Empty loop. The limiting factor of loops are the boolean expressions, which are less efficient to evaluate than simple variable accesses, as compared to uncompiled Tcl.
for {set counter 0} { $counter<10000} { incr counter} { }
Performance- msec total | ||
Uncompiled Tcl: | 3,670 | msec |
Compiled Tcl: | 425 | msec |
Speedup: | 8.64x |
Test 3: Nested loops. This shows a more realistic example of the relative speedups of loops.
for {set count1 0} { $count1<1000} {incr count1} { for {set count2 0} { $count2<1000} {incr count2} { } }
Performance- msec total | ||
Uncompiled Tcl: | 14,090 | msec |
Compiled Tcl: | 1,649 | msec |
Speedup: | 8.54x |
Test 4: Pessimistic case- we can do nothing but break the inner command into arguments and pass to the normal evaluation mechanism, because the command is not known at compile time.
set oper incr set count 0 puts stdout [time { $oper count 2} 10000 ]
Performance- μsec per iter. | ||
Uncompiled Tcl: | 244 | μsec |
Compiled Tcl: | 188 | μsec |
Speedup: | 1.30x |