检查空白字符串的最快方法是什么?
[string eq $s ""]
[string compare $s ""]
[expr $s == ""]
[regexp ^$ $s]
答案 0 :(得分:2)
proc check {} {
set s {}
puts "string equal ->[time {string eq $s ""} 100000]"
puts "string compare ->[time {string compare $s ""} 100000]"
puts "regexp ->[time {regexp ^$ $s} 100000]"
puts "expr == ->[time {expr {$s == ""}} 100000]"
puts "expr eq ->[time {expr {$s eq ""}} 100000]"
}
check ; # See the behavior in your PC
答案 1 :(得分:2)
我希望:
string equal $s ""
或
expr {$s eq ""}
会最快;这两种情况会生成几乎相同的字节码,所以我不希望区分它们。