A slow proc
--time gives the run time of a script, and --report gives its compute units and its memory. ztcl::disasm shows the bytecode of a proc, so you can see why one form of a command is slower than another. The JIT has the flags that show what the JIT compiled.
The bug: an expr with no braces
examples/debug/slow-proc.tcl adds 100,000 prices:
proc total {prices} {
set sum 0
foreach p $prices {
set sum [expr $sum + $p]
}
return $sum
}
examples/debug/slow-proc-fixed.tcl is the same script with braces around the expression: expr {$sum + $p}. Run each one with --time and --report:
ztcl --time --report examples/debug/slow-proc.tcl
ztcl --time --report examples/debug/slow-proc-fixed.tcl
report compute=700011 mem=15010920 peak=16859768
gc collections=2
299995
examples/debug/slow-proc.tcl: 303.2 ms
report compute=700011 mem=6882008 peak=6882008
gc collections=0
299995
examples/debug/slow-proc-fixed.tcl: 51.4 ms
These times came from a Debug build of ztcl on 2026-11-30, on an Intel Core i7-6700K with Linux. A release build is faster, and the times change from run to run. The two scripts use the same compute units, because one compute unit is about one command (Limits), and the two scripts run the same commands. The first script needs about six times the time and more memory for the same commands.
The bytecode
ztcl::disasm proc NAME gives the bytecode of a proc. This script prints the bytecode of each form up to the end of the body of the loop:
package require ztcl::disasm
proc total {prices} {
set sum 0
foreach p $prices {
set sum [expr $sum + $p]
}
return $sum
}
proc totalFixed {prices} {
set sum 0
foreach p $prices {
set sum [expr {$sum + $p}]
}
return $sum
}
foreach name {total totalFixed} {
set lines [split [ztcl::disasm proc $name] \n]
puts [join [lrange $lines 0 [lsearch -glob $lines {*jump 6 *}]] \n]
}
total: instructions 33, literals 3, slots 3, stack 7, jit after more calls
slot 0 prices (parameter)
slot 1 sum
slot 2 p
# line 2: "set sum 0"
0 guard 23 0 0
1 push 0 0 0 # "0"
2 store_pop 1 0 0 # $sum
# line 3: "foreach p $prices {\n set sum [exp..."
3 guard 29 0 0
4 load 0 0 0 # $prices
5 foreach_init 0 0 0
6 foreach_next 0 15 0
# line 2: "set sum [expr $sum + $p]"
# line 2: "expr $sum + $p"
7 guard 26 0 0
8 push 1 0 0 # "expr"
9 load 1 0 0 # $sum
10 push 2 0 0 # "+"
11 load 2 0 0 # $p
12 invoke 1 4 0 # "expr $sum + $p"
13 store_pop 1 0 0 # $sum
14 jump 6 0 0 # to 6
totalFixed: instructions 32, literals 1, slots 3, stack 4, jit after more calls
slot 0 prices (parameter)
slot 1 sum
slot 2 p
# line 2: "set sum 0"
0 guard 20 0 0
1 push 0 0 0 # "0"
2 store_pop 1 0 0 # $sum
# line 3: "foreach p $prices {\n set sum [exp..."
3 guard 28 0 0
4 load 0 0 0 # $prices
5 foreach_init 0 0 0
6 foreach_next 0 12 0
# line 2: "set sum [expr {$sum + $p}]"
7 guard 25 0 0
# line 2: "expr {$sum + $p}"
8 guard 23 0 0
9 bin_vv x=4 1 2 0
10 store_pop 1 0 0 # $sum
11 jump 6 0 0 # to 6
The body of the loop is the instructions from 7 to the jump:
- In
total, the instructions 8 to 12 push the wordsexpr,$sum,+, and$p, andinvokecalls the commandexprwith them. At each pass,exprjoins the words into a text and parses that text as an expression. - In
totalFixed, the expression compiled with the proc. The one instructionbin_vvadds the two variables.
A line number of a command in a body, such as line 2 for set sum, counts from the start of that body.
The fix
Put braces around each expression of expr, if, while, and for. With braces, the expression compiles one time, and its variables are read when it runs. Without braces, the words are substituted first, so a value with a space or a bracket in it changes the expression.