12.8.1         PERFORMANCE CONSIDERATIONS

Enclose expressions in braces {} for the best speed and the smallest storage requirements. This allows the Tcl byte code compiler to generate the best code.

As mentioned above, expressions are substituted twice: once by the Tcl parser and once by the expr command. For example, the commands

set a 3

set b {$a + 2}

expr $b*4

return 11, not a multiple of 4. This is because the Tcl parser will first substitute $a + 2 for the variable b, then the expr command will evaluate the expression $a + 2*4.

 

Most expressions do not require a second round of substitutions. Either they are enclosed in braces or, if not, their variable and command substitutions yield numbers or strings that don’t themselves require substitutions.

However, because a few un-braced expressions need two rounds of substitutions, the byte code compiler must emit additional instructions to handle this situation. The most expensive code is required for un-braced expressions that contain command substitutions. These expressions must be implemented by generating new code each time the expression is executed.