My own control structures are slow



Hi all,

in Tcl one can add one's own control structures to the language, but I see that it is very hard to get it right. I want to add some for loops to an interactive program that uses tcl with tkcon as a command line interpreter. An often used pattern is to iterate over an image with 512x512 pixels, and because it is tedious to type in all those for loops I decided to add a function MAP. My first try was:

proc MAP command {
  upvar x x y y
    for {set x 0} {$x<=511} {incr x} {
      for {set y 0} {$y<=511} {incr y} {
         uplevel $command
       }
     }
}

then you can do something like

MAP { set sum [expr {$sum+$x+$y}]}

this works, but is very slow (2.3 seconds). Actually it seems to be very hard to do this right.

My second try was to move the for loops into the upper level:

proc MAP2 command {
  uplevel 1 [list for {set x 0} {$x<=511} {incr x} [
      list for {set y 0} {$y<=511} {incr y} $command
      ]]
}

This is way faster, it takes only 0.4 seconds

My third try was to put the body into a procedure to force bytecode compilation:

proc MAP3 command {
uplevel 1 [list proc mapbody {} [list for {set y 0} {$y<512} {incr y} [list for {set x 0} {$x<512} {incr x} $command]]]
uplevel 1 mapbody
}


This kind of works, but now the upper level variables are not accessible, so

MAP3 { set sum [expr {$sum+$x+$y}]}


failes with 'can't read "sum": no such variable'

but if I use this procedure eg. with

MAP3 { set sum [expr {$x+$y}]}

it is much, much faster than the other variants (73 milliseconds!) due to bytecode compilation. Trying to fix the problem with the variable context by inserting "uplevel" like:

MAP3 { uplevel 1 {set sum [expr {$sum+$x+$y}]}}

makes it as slow as the first version (2.3 s)

the fault seems to be that uplevel hinders bytecode compilation. How to do these control structure thingy right?

	Christian
.



Relevant Pages

  • ANN: AOLserver 4.5.0 released!
    ... On behalf of the AOLserver Team, I have the honor of announcing the ... AOLserver 4.5.0 is a major upgrade including several new Tcl commands, ... AOLserver is America Online's Open-Source web server. ... the "ns_zlib" command for compressing and uncompressing ...
    (comp.lang.tcl)
  • Tcl-URL! - weekly Tcl news and links (Mar 5)
    ... GRIDPLUS2 is a Tile based version of GRIDPLUS. ... *) New "gpselect" command to select a tablelist row or tree node. ... Some people take their license issues very personal indeed;-) (and ... Word documents via Tcl and tcom - ...
    (comp.lang.tcl)
  • Re: Core commands as ensembles...
    ... command that does much the same as [namespace code], ... TCL as their scripting/console engine. ... home-brew bits and pieces, in which I will use TCL in any way that I ...
    (comp.lang.tcl)
  • Re: Can Ruby read emails?
    ... command, and Ruby will read it and exacute that command. ... I actually wrote a system using Tcl some years back which used ... e-mail to "move" a software agent from one host to another. ...
    (comp.lang.ruby)
  • Re: ANNOUNCE: tcgd - modern, nearly feature-complete interface to gd-2 graphics drawing
    ... This style of Tcl_Obj management is something that some Tcl core developers ... Tcl_Obj command might work better, ... Hmm, Itcl incremements an integer, "unique", for each object generated ... until it finds an command name that doesn't exist, though, wraparound ...
    (comp.lang.tcl)