Posted to tcl by emiliano at Wed Mar 25 17:01:14 GMT 2020view raw
- # striding a list; comparison of times
- # using lmap
- proc lstride1 {l n} {
- for {set i 0} {$i < $n} {incr i} {
- lappend vars var$i
- }
- lmap $vars $l {
- lmap _ $vars {
- set $_
- }
- }
- }
- # using lrange
- proc lstride2 {l n} {
- set res {}
- set len [llength $l]
- set from 0
- set to [expr {$n - 1}]
- while {$from < $len} {
- lappend res [lrange $l $from $to]
- incr from $n
- incr to $n
- }
- return $res
- }
- % llenght $data
- 567784
- # confirm that both work as intended
- % llength [lstride1 $data 14]
- 40556
- % llength [lstride2 $data 14]
- 40556
- % time {lstride $data 14} 5
- 3195229.4 microseconds per iteration
- % time {lstride2 $data 14} 5
- 128583.0 microseconds per iteration
- # Difference is ~ 25X