Version 3 of Concatenating lists

Updated 2002-04-25 15:14:43

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 odler aplha box running Tru64 UNIX

 <coming soon>

 Tcl 8.4a4
 |-------------------------------------------| 
 | Method          | 10 | 100 | 1000 | 10000 | 
 |-----------------|----|-----|------|-------| 
 | SET             | 21 | 148 | 6777 | 69467 | 
 | APPEND          | 29 | 475 | 7993 | 81514 | 
 | CONCAT          |  9 |  15 |  312 |  5968 | 
 | EVAL/LAPPEND    | 15 |  23 |  393 |  6065 | 
 | FOREACH/LAPPEND | 18 | 369 | 4524 | 44235 | 
 |-------------------------------------------|