Subject: Tcl 8.0 contains a bug in "expr"
From: Georg Fusz <[email-masked]>
To: MinGw32 <[email-masked]>, Cygnus Mailing List <[email-masked]>
Date: Tue, 01 Feb 2000 21:35:32 +0100

I posted a mail to "comp.lang.tcl" about 
the difference between [expr $a + $b] and [expr {$a + $b}]

And received the folowing answer.

The Tcl 8.0 installation I used was from Mummit's www-pages.

Maybe it interesting to some one here.

-------- Original Message --------
Subject: RE: curly braces for speed in expr commands [was: - to +]
Date: Tue, 1 Feb 2000 11:12:14 -0800
From: "Jeffrey Hobbs" <[email-masked]>
To: "Georg Fusz" <[email-masked]>, <[email-masked]>,"Keith Lea"
<[email-masked]>

Doh, but the results you found are due to a bug...  expr used to
overaggressively compile things.  Basically, imagine that all your
exprs had the braces put on the for you...  Noone noticed this for
the first few versions of Tcl8.0 because it was so uncommon to
require that NO braces were used.  However, it was incorrect, and
the behavior was corrected.  If you really wanted, I could think
of a few examples (I think some uses of the ternary operator, as
well as a few where you wanted double-evaluation).  Actually, just
look in 8.2.3's expr.test to find examples that wouldn't work in
8.0p2 due to the overaggressiveness.

Jeffrey Hobbs
Scriptics Corp.
[email-masked] 

> -----Original Message-----
> From: Georg Fusz [mailto:[email-masked]]
> Sent: Tuesday, February 01, 2000 7:26 AM
> To: Jeffrey Hobbs; [email-masked]; Keith Lea
> Subject: curly braces for speed in expr commands [was: - to +]
> 
> 
> 
> In the thread "- to +" "Keith Lea" <[email-masked]> gave the hint that
> commands of the form
> [expr {$a +$b}] are quicker as [expr $a +$b]. 
> 
> 
> 
> I wrote the following script to compare the speed of a loop with 
> expression with
> and without curly braces:
> 
> puts $fh "\nWithout Curlys\n"
> 
> set t1 [clock click]
> 
> set sum 0.0
> set sign -1
> for { set i 1 } { $i < $numb } { incr i 2} {
>     set sign [ expr $sign * -1]
>     set sum [ expr $sum + double($sign) / double( $i ) ]
> }
> 
> # puts "sum = $sum"
> 
> puts $fh "Diff = [expr $PI - 4.0 * $sum]" 
> 
> 
> set t2 [clock click]
> 
> puts $fh "Click Diff = [expr $t2 - $t1]"
> 
> 
> # -----------------------------------------------------------------
> 
> 
> puts $fh "\n\nWith Curlys\n"
> 
> set t1 [clock click]
> 
> 
> set sum 0.0
> set sign -1
> for { set i 1 } { $i < $numb } { incr i 2} {
>     # puts " i = $i"
>     set sign [ expr { $sign * -1 } ]
>     set sum [ expr { $sum + double($sign) / double( $i ) } ]
> }
> 
> # puts "sum = $sum"
> 
> puts $fh "Diff = [expr $PI - 4.0 * $sum]" 
> 
> 
> set t2 [clock click]
> 
> puts $fh "Click Diff = [expr $t2 - $t1]"
> 
> ----------------------------------------------------------------------
> 
> The loop is calculating pi/4 by the following series:
> 
> s = 1 - 1/3 + 1/5 - 1/7 + ...
> 
> -----------------------------------------------------------------------
> 
> Surprisingly using Tcl 8.0 was quicker as Tcl 8.2.
> Using Tcl 8.0 there was nearly no difference between the two loops.
> 
> Using the Tcl 8.2 the second loop is 6 times slower as the first loop.
> 
> With Tcl 8.0 I also tested a added command calling a C-Routine.
> 
> Here are the exact results.
> 
> --------------------------------------------------------------------
> 
> Version    = 8.0
> Patchlevel = 8.0p2
> 
> Number of summands = 500000
> 
> Without Curlys
> 
> Diff = 2.00000010109e-006
> Click Diff = 39066
> 
> 
> With Curlys
> 
> Diff = 2.00000010109e-006
> Click Diff = 37825
> 
> 
> New Command
> 
> Diff = 2.00000019301e-006
> Click Diff = 270
> 
> ++++++++++++++++++++++++++++++++++++++++++++++
> 
> Version    = 8.2
> Patchlevel = 8.2.2
> 
> Number of summands = 500000
> 
> Without Curlys
> 
> Diff = 1.99841799997e-006
> Click Diff = 346298
> 
> 
> With Curlys
> 
> Diff = 2.00000200001e-006
> Click Diff = 51254
> 
> 
> -- 
> Georg Fusz
> 
> home-page: http://cadence.fb12.tu-berlin.de/~fusz/
> 
> Fon: 
> Universitaet: +49 30 314 26 884
> privat:     : +49 30 815 30 32
> Handy:      : +49 173 20 10 696

--
Want to unsubscribe from this list?
Send a message to [email-masked]