Both "string map" and the split-join method can also count many many replacments where done.
Code: Select all
set len [string length $orig]
set ph "%20"
set new [join [split $orig] $ph]
set count [expr ([string length $new] - $len) / [string length $ph]]
Not as simple or elegant, more CPU intensive.
As for the battle of time, this will vary on text length and system. The battle between Windows and Linux has allways been odd.
Windows has shown on occasions to beat the pants of linux in some commands, but lagged in others. Linux shows consistant replies where windows hasn't.
Note, there was one test missed out.
Code: Select all
set text [string repeat "ssss " 30]
time {set newtext [join [split $text " "] %20]} 1000
time {set newtext [string map {" " %20} $text]} 1000
time {regsub -all " " $text "%20" newtext} 1000
time {set newtext [join [split $text] %20]} 1000
The results in order (cycles reduced)
Code: Select all
92 microseconds per iteration
108 microseconds per iteration
405 microseconds per iteration (ouch)
110 microseconds per iteration
note how using the default marker for "split" rather than " " causes time to increase.
Here are the tests again, with the "string repeat" set to 10
Code: Select all
46 microseconds per iteration
47 microseconds per iteration
151 microseconds per iteration
53 microseconds per iteration
Tcl 8.3.4 - Linux 2.4.21
System level optimisations are not important to a coder, generic speed is. Unless they are gonna use a test suite first to determine the best command for the job.