TclX loop slow in the default case



Hello

I wanted to see how much faster the Tclx loop command was compared to
the ordinary for loop.

When [loop] is used without an explicit increment, it defaults to 1.
I noticed that this default case runs very slowly.

Here is my script:
ed@aleph> cat loop.tcl

# timing "for" and the Tclx "loop"
package require Tclx

proc loop_default {lim} {
loop i 0 $lim {}
}

proc loop_explicit {lim} {
loop i 0 1 $lim {}
}

proc tcl_for {lim} {
for {set i 0} {$i<$lim} {incr i} {}
}

proc run {lim} {
puts "time (us) taken for $lim iterations"
puts "[lindex [time {loop_default $lim}] 0]\t\
<-- TclX 'loop' with default increment"
puts "[lindex [time {loop_explicit $lim}] 0]\t\
<-- TclX 'loop' with explicit increment"
puts "[lindex [time {tcl_for $lim}] 0]\t\ <-- tcl 'for'"
}

if {[info script] eq $argv0 } {
puts "tclsh: [info patchlevel]"
puts "Tclx: [package require Tclx]"
set lim [lindex $argv 0]
run $lim
}
# end ----------------------------------------------------

I tested the script with different values for the iterations/limit 9
times per limit and took the median value. I tested with tcl 8.4 and
8.5.

ed@aleph>
ed@aleph> tclsh8.5 loop.tcl 10 > out.txt
ed@aleph> tclsh8.5 run-tests.tcl 100 >> out.txt
ed@aleph> tclsh8.5 run-tests.tcl 1000 >> out.txt
ed@aleph> tclsh8.5 run-tests.tcl 10000 >> out.txt
ed@aleph> cat out.txt
tclsh: 8.5.3
Tclx: 8.4
time (us) taken for 10 iterations
66 <-- TclX 'loop' with default increment
34 <-- TclX 'loop' with explicit increment
47 <-- tcl 'for'
8.4.18 8.5.3 %chng proc
23 21 -0.02 tcl_for
62 172 1.10 loop_default
7 9 0.02 loop_explicit
8.4.18 8.5.3 %chng proc
200 174 -0.26 tcl_for
561 1605 10.44 loop_default
7 9 0.02 loop_explicit
8.4.18 8.5.3 %chng proc
1961 1700 -2.61 tcl_for
5525 16539 110.14 loop_default
7 10 0.03 loop_explicit
ed@aleph>

Is this a bug for the default increment case?

Regards
Evilson
.



Relevant Pages

  • Re: critique my program!
    ... One person already recommended against the definition for ZER, ... The difference is that LIM, presumably an abbreviation for limit, can be seen as a parameter. ... void printar; ... There are many ways to write a loop for a fixed number of iterations, as you do with the while loop. ...
    (comp.lang.c)
  • Re: more CommEvents than expected
    ... He resets proc to 0 at the beginning of the loop and only sets it to 1 when ... times even if WaitCommEvent is not called in the for loop. ... 1> clear dwEvtMask every time when you get into the new loop, ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Deleting in a cursor loop
    ... cursor for loop and an iterative commit within the loop every 300,000 ... This is the proc: ... CURSOR delrowcur IS ... IF (RCOUNT>= 300000) THEN ...
    (comp.databases.oracle.server)
  • Re: tcl lists
    ... i need to be able to set ALL the elements of the list in the same loop. ... other lists with different numbers or arguments which can be passed to ... the runVpn proc. ... proc SetOpt { ...
    (comp.lang.tcl)
  • Re: Deleting in a cursor loop
    ... cursor for loop and an iterative commit within the loop every 300,000 ... CURSOR delrowcur IS ... into this table while also purging from it with this proc, ...
    (comp.databases.oracle.server)