Version 0 of is_empty

Updated 2014-05-29 07:20:18 by samoc

is_empty returns true if $string is {}.

It is efficient even if $string is large binary data (see test output below).

It would be nice if there was an optimisation in the interpreter for ... eq {} or string equal to make this unnecessary.

proc is_empty {string} {
    expr {![binary scan $string c c]}
}

proc not_empty {string} {
    expr {![is_empty $string]}
}

Test:

foreach cmd {
    {expr {$data == {}}}
    {expr {$data eq {}}}
    {string bytelength $data}
    {string equal $data {}}
    {is_empty $data}
} {
    set data [file get ~/stuff/file.dat]
    puts "$cmd\n  [time {set r [eval $cmd]}]\n  result = $r\n"
    unset data
    puts ""
}

Output:

expr {$data == {}}
  1099855 microseconds per iteration
  result = 0


expr {$data eq {}}
  1094751 microseconds per iteration
  result = 0


string bytelength $data
  1076724 microseconds per iteration
  result = 378629364


string equal $data {}
  1078098 microseconds per iteration
  result = 0


is_empty $data
  42 microseconds per iteration
  result = 0