Sujet : Re: good to know: tcl static "regexp" is faster than tcl "string" operation
De : aotto1968 (at) *nospam* t-online.de (aotto1968)
Groupes : comp.lang.tclDate : 31. Dec 2024, 19:24:30
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vl1csu$2bnb8$1@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
On 31.12.24 14:46, Rich wrote:
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
→ thanks.