Sujet : Re: good to know: tcl static "regexp" is faster than tcl "string" operation
De : rich (at) *nospam* example.invalid (Rich)
Groupes : comp.lang.tclDate : 31. Dec 2024, 14:46:44
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vl0sk4$28nvd$1@dont-email.me>
References : 1
User-Agent : tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
aotto1968 <
aotto1968@t-online.de> wrote:
#!/usr/bin/env tclsh
proc test-1 { val } {
if {[regexp ^:: $val]} {
return true
} else {
return false
}
}
proc test-2 { val } {
if {[string range $val 0 1] eq "::"} {
return true
} else {
return false
}
}
set num 100000
puts 1=[time {test-1 ::otto} $num]
puts 2=[time {test-1 otto} $num]
puts 3=[time {test-2 ::otto} $num]
puts 4=[time {test-2 otto} $num]
./sbin/time-check.tcl
1=1.26311 microseconds per iteration
2=1.09152 microseconds per iteration
3=1.44028 microseconds per iteration
4=1.43917 microseconds per iteration
But neither is quite as fast as string match (athough regex is close):
$ cat time-check.tcl
#!/usr/bin/env tclsh
proc test-1 { val } {
if {[regexp ^:: $val]} {
return true
} else {
return false
}
}
proc test-2 { val } {
if {[string range $val 0 1] eq "::"} {
return true
} else {
return false
}
}
proc test-3 { val } {
return [string match ::* $val]
}
set num 100000
puts 1=[time {test-1 ::otto} $num]
puts 2=[time {test-1 otto} $num]
puts 3=[time {test-2 ::otto} $num]
puts 4=[time {test-2 otto} $num]
puts 5=[time {test-3 ::otto} $num]
puts 6=[time {test-3 otto} $num]
$ ./time-check.tcl
1=0.45252 microseconds per iteration
2=0.42354 microseconds per iteration
3=0.58949 microseconds per iteration
4=0.58363 microseconds per iteration
5=0.4351 microseconds per iteration
6=0.41378 microseconds per iteration