Ulysses
Ulysses

Reputation: 6003

Tcl: What's the fastest way to check a blank string

What's the fastest way to check a blank string?

Upvotes: 1

Views: 1931

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137567

I'd expect that either:

string equal $s ""

or

expr {$s eq ""}

would be fastest; the two cases will generate virtually identical bytecode so I wouldn't expect to distinguish them.

Upvotes: 2

Dinesh
Dinesh

Reputation: 16428

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

Upvotes: 2

Related Questions