Posted to tcl by philbr-t at Tue Mar 19 20:49:53 GMT 2024view raw
- package require fileutil
- # Benchmark showing the impact on lsearch in Tcl 8.6.11
- #
- # $ fossil info 26e57ca1486e497b
- # hash: 26e57ca1486e497b32e75a074c0ee81b62beefb8 2020-05-05 16:00:06 UTC
- # parent: 5d38a6b3c64572f808163ae7656333fea1e478a7 2020-05-05 15:56:14 UTC
- # child: b275de7c6434b50adf9f27ff24d01bd972efa624 2020-05-06 08:15:49 UTC
- # merged-into: b07935f03d33d1bdfb89a4b13f1fee914d78080d 2020-05-05 16:20:24 UTC
- # tags: core-8-6-branch
- # comment: More usage of TclUtfToUCS4(), so we can use the whole Unicode range better in TCL_UTF_MAX>3 builds. (user: jan.nijtmans)
- #
- # $ /usr/local/tcl8.6.11/bin/tclsh8.6 lbench.tcl
- # stringlist has 5000 items.
- # lsearch returned -1 took 2297 microseconds.
- # lsearch -exact returned -1 took 23 microseconds.
- # $ /usr/local/tcl8.6.10/bin/tclsh8.6 lbench.tcl
- # stringlist has 5000 items.
- # lsearch returned -1 took 512 microseconds.
- # lsearch -exact returned -1 took 24 microseconds.
- #
- #
- set alphabet [ list X M R 0 1 2 3 4 5 6 7 8 9 / ]
- # based on suchenwi's random string generator from the Tcl Wiki:
- proc lpick L {lindex $L [expr {int(rand()*[llength $L])}]}
- proc randomlyPicked { length {chars { X 0 1 2 3 4 5 6 7 8 9 / }} } {
- for {set i 0} {$i<$length} {incr i} {append res [lpick $chars]}
- return $res
- }
- set stringlist [ list ]
- # Generate some repetitive random strings
- # Start all strings with a common prefix
- set string_prefix "X14/X33/X5/X1848/X1/X3/X1/X1/X1/X1/X8/X49/X34"
- set numstrings 5000
- for { set n 0 } { $n < $numstrings } { incr n } {
- set random_length [ expr { 5 + int(rand()*25) } ]
- set new_string "$string_prefix/[ randomlyPicked $random_length ]"
- lappend stringlist $new_string
- }
- # Search for a unique string that also has the same common prefix
- set search_for "$string_prefix/M0"
- puts "stringlist has [ llength $stringlist ] items."
- set timer [ clock microseconds ]
- set finder [ lsearch $stringlist $search_for ]
- puts "lsearch returned $finder took [ expr { [ clock microseconds ] - $timer } ] microseconds."
- set timer [ clock microseconds ]
- set finder [ lsearch -exact $stringlist $search_for ]
- puts "lsearch -exact returned $finder took [ expr { [ clock microseconds ] - $timer } ] microseconds."