Re: My own control structures are slow



Ulrich Schöbel wrote:
Hi Christian,

 set sum 20

sets a _global_ variable, whereas your second example
sets a local variable.

Verify it with info globals.

Best regards

OK, so I modified the suggestion to:

proc MAP5 command {
  set MAP_up [list upvar 1]
  set locvars [uplevel 1 {info locals}]
  foreach locvar $locvars {
    lappend MAP_up $locvar $locvar
  }

  if {[llength $locvars]==0} {set MAP_up ""}

  set MAP_glob [list global]
  set globvars [uplevel 1 {info globals}]
  foreach globvar $globvars {
    lappend MAP_glob $globvar $globvar
  }

  if {[llength $globvars]==0} {set MAP_glob ""}


proc mapbody {} "$MAP_up $MAP_glob for {set y 0} {\$y<512} {incr y} { for {set x 0} {\$x<512} {incr x} {$command} }" uplevel 1 mapbody }

which seems to work now, BUT: I import every global variable, not just the one visible in the current context. It IS an awkward thing, I don't see how I can exactly duplicate the outer context including upvars and imported globals without using uplevel, which destroys the possibility of bytecode compilation. So I doubt it is possible to implement new control structures that are as efficient as the builtin ones.

I have the same thingy in C++ as a macro, defined as

#define MAP for (int y=0; y<512; y++) \
                  for (int x=0; x<512; x++)

which is easy and efficient, since after preprocessing the compiler sees nothing else than two nested for loops. What we would need here is something like uplevel that bytecompiles the code before execution. Not to mention that I want to replace other similiar controls structures that iterate over circles, other defined areas,...

Regards,
	Christian
.