Posted to tcl by dbohdan at Mon Jun 12 10:19:51 GMT 2017view raw
- namespace eval ::poly {
- namespace export *
- proc index {value args} {
- ::poly::[lindex $value 0]::index $value {*}$args
- }
- proc length value {
- ::poly::[lindex $value 0]::length $value
- }
- }
- namespace eval ::poly::LIST {
- proc index {value args} {
- return [lindex [lindex $value 1] {*}$args]
- }
- proc length value {
- return [llength [lindex $value 1]]
- }
- }
- namespace eval ::poly::DICT {
- proc index {value args} {
- return [dict get [lindex $value 1] {*}$args]
- }
- proc length {value args} {
- return [dict size [lindex $value 1]]
- }
- }
- namespace eval ::benchmark {
- namespace import ::poly::*
- }
- set testValues {}
- for {set i 0} {$i < 10000} {incr i} {
- set tag [expr {$i % 2 == 0 ? "LIST" : "DICT"}]
- lappend testValues [list $tag [list foo$i bar$i baz$i qux$i hi yo]]
- }
- proc ::benchmark::bench1 values {
- set result {}
- foreach value $values {
- set tag [lindex $value 0]
- set content [lindex $value 1]
- if {$tag == {LIST}} {
- lappend result [llength $content]
- } else {
- lappend result [dict size $content]
- }
- }
- return $result
- }
- proc ::benchmark::bench2 values {
- set result {}
- foreach value $values {
- lappend result [length $value]
- }
- return $result
- }
- puts [time {::benchmark::bench1 $testValues} 100]
- puts [time {::benchmark::bench2 $testValues} 100]