Best method for string comparison in tcl?

Viewed 112

I have two strings let suppose $a and $b. I want to compare whether they are equal or not. Which method should I use and why?

I know of 2 methods: $a eq $b and string equal $a $b

which of these is faster or if we have any other faster way of string comparison in tcl?

1 Answers

Use whichever is most obvious to you as a human. The two commands actually result in the exact same byte codes:

% tcl::unsupported::disassemble script {expr {$a eq $b}}
ByteCode 0x0x556045ea6fd0, refCt 1, epoch 17, interp 0x0x556045e1a390 (epoch 17)
  Source "expr {$a eq $b}"
  Cmds 1, src 15, inst 8, litObjs 2, aux 0, stkDepth 2, code/src 0.00
  Commands 1:
      1: pc 0-6, src 0-14
  Command 1: "expr {$a eq $b}"
    (0) push1 0         # "a"
    (2) loadStk 
    (3) push1 1         # "b"
    (5) loadStk 
    (6) streq 
    (7) done 

% tcl::unsupported::disassemble script {string equal $a $b}
ByteCode 0x0x556045ea76d0, refCt 1, epoch 17, interp 0x0x556045e1a390 (epoch 17)
  Source "string equal $a $b"
  Cmds 1, src 18, inst 8, litObjs 2, aux 0, stkDepth 2, code/src 0.00
  Commands 1:
      1: pc 0-6, src 0-17
  Command 1: "string equal $a $b"
    (0) push1 0         # "a"
    (2) loadStk 
    (3) push1 1         # "b"
    (5) loadStk 
    (6) streq 
    (7) done 

Considering the very straight-forward byte codes, it is hard to imagine that there can be any faster method.

Related