Because unbraced expressions are reparsed every time. You may know
that in a piece of code like
if $x<1000 {do something]
that $x is always an integer. But the bytecode compiler doesn't.
It has to be prepared for a $x that contains "$y>10 && $z" --
which would cause the above command to expand into
if {$y>10 && $z<1000} {do something}
after the substitution pass on the command.
--
73 de ke9tv/2, Kevin
So "quoted" expressions should also not be faster.
A special case :
set x 0
while $x<10 {puts $x; incr x}
Can you tell what will be the output of this program ? (Without
checking on wish/tclsh, offcourse)
Right, mostly. What the compiler looks for is actually strings
containing no substitutions - and {} protects the stuff inside
from being substituted. Something like
if 0 {this is never executed}
does nothing just as efficiently as
if {0} {this is never executed, either}
> So "quoted" expressions should also not be faster.
Correct; double-quoted expressions are just as slow as unquoted ones.
> A special case :
>
> set x 0
> while $x<10 {puts $x; incr x}
>
> Can you tell what will be the output of this program ? (Without
> checking on wish/tclsh, offcourse)
It's an endless loop, putting incremented integers. $x<10 is
substituted once to {0<10} in the substitution pass, and that
constant doesn't change inside the loop body.
No. Braces inhibit substitution, quotes do not. Actually, braces aren't
magic, you can use any quoting that sends a literal expression into expr.
For example:
% set x 10
10
% time {expr $x*$x} 100
4.95217 microseconds per iteration
% time {expr {$x*$x}} 100
1.25979 microseconds per iteration
% time {expr \$x*\$x} 100
1.6528200000000002 microseconds per iteration
> What I gather is that,
> expressions inside curly braces are the only ones which are byte-
> compiled because Tcl compiler knows that it need not perform any
> substitution-cum-expression-evaluation on a braced expression. That it
> needs to do only evaluation.
>
> So "quoted" expressions should also not be faster.
>
> A special case :
>
> set x 0
> while $x<10 {puts $x; incr x}
>
> Can you tell what will be the output of this program ? (Without
> checking on wish/tclsh, offcourse)
>
That will be an endless loop, of course. While is passed the string "0<10".
No. Braces inhibit substitution, quotes do not. Actually, braces aren't
magic, you can use any quoting that sends a literal expression into expr.
For example:
% set x 10
10
% time {expr $x*$x} 100
4.95217 microseconds per iteration
% time {expr {$x*$x}} 100
1.25979 microseconds per iteration
% time {expr \$x*\$x} 100
1.6528200000000002 microseconds per iteration
> What I gather is that,
> expressions inside curly braces are the only ones which are byte-
> compiled because Tcl compiler knows that it need not perform any
> substitution-cum-expression-evaluation on a braced expression. That it
> needs to do only evaluation.
>
> So "quoted" expressions should also not be faster.
>
> A special case :
>
> set x 0
> while $x<10 {puts $x; incr x}
>
> Can you tell what will be the output of this program ? (Without
> checking on wish/tclsh, offcourse)
>
That will be an endless loop, of course. While is passed the string "0<10".