Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how does bracing help ?

4 views
Skip to first unread message

call_me_anything

unread,
Feb 10, 2007, 9:56:55 AM2/10/07
to
Why does bracing of expressions help them execute faster ?

Kevin Kenny

unread,
Feb 10, 2007, 10:09:07 AM2/10/07
to
call_me_anything wrote:
> Why does bracing of expressions help them execute faster ?
>

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

call_me_anything

unread,
Feb 10, 2007, 10:52:47 AM2/10/07
to
Then will it be still faster if we use "quotes" instead of {curly
braces} ?
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)

Kevin Kenny

unread,
Feb 10, 2007, 11:00:40 AM2/10/07
to
call_me_anything wrote:
> Then will it be still faster if we use "quotes" instead of {curly
> braces} ?
> 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.

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.

Bryan Oakley

unread,
Feb 10, 2007, 11:10:28 AM2/10/07
to
call_me_anything wrote:
> Then will it be still faster if we use "quotes" instead of {curly
> braces} ?

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".

Bryan Oakley

unread,
Feb 10, 2007, 11:10:28 AM2/10/07
to
call_me_anything wrote:
> Then will it be still faster if we use "quotes" instead of {curly
> braces} ?

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".

0 new messages