Re: TclX loop slow in the default case



On Aug 28, 11:03 am, "Donal K. Fellows" <donal.k.fell...@xxxxxxxxx>
wrote:
Alexandre Ferrieux wrote:
Why a wrong analysis ? How would you define [loop] in pure Tcl so that
it has the same performance as a [for] ?

In 8.5 and before, you can't because the body script will not be
compiled to have efficient access to local variables. (This might
change in 8.6 AIUI.) But apart from that you can get close:

  proc loop {var from to body} {
     upvar 1 $var v
     for {set v $from} {$v <= $to} {incr v} {
        uplevel 1 $body
     }
  }

Yes, that's probably the sort of thing you'd write naïvely. That will
use maximally efficient variable accesses (except in the body at the
moment because we don't currently reconnect the compilation of the
body to the local variable table of the calling context) and will
compile the body. By contrast, the TclX loop command is actually less
efficient than that as it currently uses code that isn't very
efficient to assign the loop variables. (The API it uses is optimized
for ease of use with literal variable names, not for execution speed.)

Donal.

Funny you mention it, I had indeed written the naive implementation as
an exercise :-) I had gotten the following results for counting: 0 --
10,000-1

ed@aleph> tclsh8.5 run-tests.tcl 10000
8.4.18 8.5.3 ratio proc
1960 1699 0.87 tcl_for
5481 16175 2.95 loop_default
9020 11777 1.31 loop_eval <-- naive implemenation
.



Relevant Pages

  • Re: Ada.Real_Time behavior with GNAT
    ... the following code using GNAT GPL 2007 on Windows XP SP2 I observe ... procedure Timing is ... 10_000_000 loop ... The compilation command is pretty straightforward: ...
    (comp.lang.ada)
  • Ada.Real_Time behavior with GNAT
    ... the following code using GNAT GPL 2007 on Windows XP SP2 I observe ... procedure Timing is ... 10_000_000 loop ... The compilation command is pretty straightforward: ...
    (comp.lang.ada)
  • Re: Toward a Forth thats easier to learn
    ... return stack before you enter a DO loop and use R@ inside the loop. ... That's why we have -LOOP in Gforth. ... (compilation token) ...
    (comp.lang.forth)
  • Re: (loop initially ... getting non-ANSI CL warning
    ... I am still in a bit of shock that my first loop attempt works, ... upon compilation: ... One thing refugees from the land of genius compilers like C/C++/Java have to learn is to Actually Read and Consider Lisp compiler errors/warnings. ... I actually remember telling some newby to some other language "never look at its error messages, ...
    (comp.lang.lisp)
  • Re: self-reference
    ... the third of the 2 possibility is to use a compilation trick of the HP48G ... > I'm trying to write a program which includes a subroutine that calls ... > 'S' on the stack rather than satisfyingly go into an infinite loop). ... > Aren't local variables visible at lower levels? ...
    (comp.sys.hp48)