Posted to tcl by emiliano at Wed Mar 25 17:01:14 GMT 2020view raw

  1. # striding a list; comparison of times
  2.  
  3. # using lmap
  4. proc lstride1 {l n} {
  5. for {set i 0} {$i < $n} {incr i} {
  6. lappend vars var$i
  7. }
  8. lmap $vars $l {
  9. lmap _ $vars {
  10. set $_
  11. }
  12. }
  13. }
  14.  
  15. # using lrange
  16. proc lstride2 {l n} {
  17. set res {}
  18. set len [llength $l]
  19. set from 0
  20. set to [expr {$n - 1}]
  21. while {$from < $len} {
  22. lappend res [lrange $l $from $to]
  23. incr from $n
  24. incr to $n
  25. }
  26. return $res
  27. }
  28.  
  29. % llenght $data
  30. 567784
  31. # confirm that both work as intended
  32. % llength [lstride1 $data 14]
  33. 40556
  34. % llength [lstride2 $data 14]
  35. 40556
  36.  
  37. % time {lstride $data 14} 5
  38. 3195229.4 microseconds per iteration
  39. % time {lstride2 $data 14} 5
  40. 128583.0 microseconds per iteration
  41.  
  42. # Difference is ~ 25X
  43.