Version 13 of Concatenating lists

Updated 2008-06-11 15:10:13 by LV

From a recent query on The comp.lang.tcl newsgroup on combining 2 list into a single list:

Here is the test code used:

 proc SET         {l1 l2} { set l1 "$l1 $l2" }
 proc APPEND      {l1 l2} { append l1 " $l2" }
 proc CONCAT      {l1 l2} { set l1 [concat $l1 $l2] }
 proc EVAL/LAPPEND {l1 l2} { eval [list lappend l1] $l2 }
 proc FOREACH/LAPPEND {l1 l2} {foreach i $l2 {lappend l1 $i} ; set l1}
 set cases "SET APPEND CONCAT EVAL/LAPPEND FOREACH/LAPPEND"

 proc makeList {len item} {
     for {set i 0} {$i< $len} {incr i} {
         lappend res $item
     }
     return $res
 }

 proc testem {len} {
     global cases
     set res {}
     foreach p $cases {
         catch {unset l1}
         catch {unset l2}
         set l1 [makeList $len a]
         set l2 [makeList $len b]
         puts -nonewline stderr "$p..." ; flush stderr
         lappend res $p,$len [lindex [time {llength [$p $l1 $l2]} 1000] 0]
     }
     return $res
 }


 foreach l {10 100 1000 10000} {
     puts -nonewline stderr "testing size $l..." ; flush stderr
     array set data [testem $l]
     puts stderr done ; flush stderr
 }

 puts "Tcl Version - [info patchlevel]\n"

 set fmtstr "| %15s | %6d | %6d | %6d | %6d |"
 set divid  "|-----------------|--------|--------|--------|--------|"
 set ends  "|-----------------------------------------------------|"

 puts $ends
 puts [format $fmtstr Method 10 100 1000 10000]
 puts $divid

 foreach p $cases {
     puts [format $fmtstr $p \
                 $data($p,10) $data($p,100) \
                 $data($p,1000) $data($p,10000)]
 }

 puts $ends


And here are the results on an oLder alPha box running Tru64 UNIX

 Tcl Version - 7.4p3
 |-----------------------------------------------------|
 |          Method |     10 |    100 |   1000 |  10000 |
 |-----------------|--------|--------|--------|--------|
 |             SET |     14 |     45 |   1298 |  12978 |
 |          APPEND |     13 |     43 |   1474 |  12787 |
 |          CONCAT |     17 |     57 |   1639 |  15712 |
 |    EVAL/LAPPEND |     29 |    104 |   3494 |  34986 |
 | FOREACH/LAPPEND |     42 |    796 |  10501 | 105971 |
 |-----------------------------------------------------|

 Tcl Version - 7.5  
 |-----------------------------------------------------|
 |          Method |     10 |    100 |   1000 |  10000 |
 |-----------------|--------|--------|--------|--------|
 |             SET |     15 |     45 |   1334 |  13277 |
 |          APPEND |     14 |     45 |   1396 |  12904 |
 |          CONCAT |     18 |     54 |   1632 |  16561 |
 |    EVAL/LAPPEND |     26 |    108 |   3635 |  36794 |
 | FOREACH/LAPPEND |     44 |    832 |  11428 | 114468 |
 |-----------------------------------------------------|

 Tcl Version - 7.6
 |-----------------------------------------------------|
 |          Method |     10 |    100 |   1000 |  10000 |
 |-----------------|--------|--------|--------|--------|
 |             SET |     21 |     64 |   1592 |  17324 |
 |          APPEND |     21 |     62 |   1801 |  15949 |
 |          CONCAT |     28 |     72 |   2237 |  19821 |
 |    EVAL/LAPPEND |     43 |    719 |   6153 |  66896 |
 | FOREACH/LAPPEND |     80 |   2261 |  22370 | 232325 |
 |-----------------------------------------------------| 

 Tcl Version - 8.0
 |-----------------------------------------------------|
 |          Method |     10 |    100 |   1000 |  10000 |
 |-----------------|--------|--------|--------|--------|
 |             SET |     20 |    146 |   6727 |  61365 |
 |          APPEND |     27 |    215 |   7527 |  72501 |
 |          CONCAT |     21 |    661 |   6224 |  63780 |
 |    EVAL/LAPPEND |     34 |    344 |   3140 |  24726 |
 | FOREACH/LAPPEND |     37 |   1018 |  10688 | 107653 |
 |-----------------------------------------------------|

 Tcl Version - 8.2.0
 |-----------------------------------------------------|
 |          Method |     10 |    100 |   1000 |  10000 |
 |-----------------|--------|--------|--------|--------|
 |             SET |     20 |    145 |   6611 |  67115 |
 |          APPEND |     28 |    176 |   7485 |  78008 |
 |          CONCAT |     21 |    598 |   6069 |  67277 |
 |    EVAL/LAPPEND |     31 |    172 |   2805 |  26525 |
 | FOREACH/LAPPEND |     36 |    925 |  10676 | 108879 |
 |-----------------------------------------------------| 

 Tcl Version - 8.4a4
Method10100100010000
SET 21150655569080
APPEND28391817282023
CONCAT9143385830
EVAL/LAPPEND15287955956
FOREACH/LAPPEND19530425945044

How do I interpret these results?

The entries in the tables are the times (in microseconds) it takes to concatenate two lists of length equal to the column heading, using the method in the row heading. The smaller the number, the faster the method.