1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
source [file dirname [info script]]/testing.tcl
needs constraint manual
set iterations 10000
set version [info patchlevel]
proc bench {name cmd} {
if {[catch {
set t [time $cmd 2]
set ms [format %.0f [expr {[lindex $t 0] / 1000}]]
}]} {
set ms ?
}
puts "$::version: $name ${ms}ms"
}
proc set_dict_sugar {} {
for {set i 0} {$i < $::iterations} {incr i} {
set a(b) $i
}
}
# Note that this case does not benefit from the dict sugar
# speedup since a($b) needs to be interpolated and reparsed every time
proc set_var_dict_sugar {} {
set b b
for {set i 0} {$i < $::iterations} {incr i} {
set a($b) $i
}
}
proc set_var_dict {} {
set b b
for {set i 0} {$i < $::iterations} {incr i} {
dict set a $b $i
}
}
proc read_file {file} {
set f [open $file]
while {[gets $f buf] >= 0} {
}
close $f
}
proc read_file_split {file} {
set f [open $file]
while {[gets $f buf] >= 0} {
split $buf \t
}
close $f
}
proc read_file_split_assign_foreach {file} {
set f [open $file]
while {[gets $f buf] >= 0} {
foreach {info(chan) info(datetime) info(duration) info(title) subtitle_genre info(desc) info(rating) dummy} [split $buf \t] {break}
}
close $f
}
proc read_file_split_assign_foreach_dict {file} {
set f [open $file]
while {[gets $f buf] >= 0} {
foreach {chan datetime duration title subtitle_genre desc rating dummy} [split $buf \t] {break}
dict set info chan $chan
dict set info duration $duration
dict set info title $title
dict set info subtitle_genre $subtitle_genre
dict set info desc $desc
dict set info rating $rating
}
close $f
}
proc read_file_split_assign_foreach_dictsugar {file} {
set f [open $file]
while {[gets $f buf] >= 0} {
foreach {chan datetime duration title subtitle_genre desc rating dummy} [split $buf \t] {break}
set info(chan) $chan
set info(duration) $duration
set info(title) $title
set info(subtitle_genre) $subtitle_genre
set info(desc) $desc
set info(rating) $rating
}
close $f
}
proc read_file_split_assign_foreach_simple {file} {
set f [open $file]
while {[gets $f buf] >= 0} {
foreach {chan datetime duration title subtitle_genre desc rating dummy} [split $buf \t] {break}
}
close $f
}
proc read_file_split_assign_lindex {file} {
set f [open $file]
while {[gets $f buf] >= 0} {
set split [split $buf \t]
set info(chan) [lindex $split 0]
set info(datetime) [lindex $split 1]
set info(duration) [lindex $split 2]
set info(title) [lindex $split 3]
set info(subtitle_genre) [lindex $split 4]
set info(desc) [lindex $split 5]
set info(rating) [lindex $split 6]
}
close $f
}
# Create a really big file
set f [open test.in w]
for {set i 0} {$i < $::iterations} {incr i} {
puts $f "a\tb\tc\te\tf\tg\th\ti\tj\tk"
}
close $f
bench "set dictsugar" {set_dict_sugar}
bench "set var dictsugar" {set_var_dict_sugar}
bench "set var dict" {set_var_dict}
# Read once before testing perf
read_file test.in
bench "read file" {read_file test.in}
bench "read file split" {read_file_split test.in}
bench "foreach: simple" {read_file_split_assign_foreach_simple test.in}
bench "foreach: direct dictsugar" {read_file_split_assign_foreach test.in}
bench "foreach: dict cmd" {read_file_split_assign_foreach_dict test.in}
bench "foreach: assign to dictsugar" {read_file_split_assign_foreach_dictsugar test.in}
bench "foreach: assign to dictsugar via lindex" {read_file_split_assign_lindex test.in}
file delete test.in
# testreport
|