-5

I'm aware that the standard strcmp() function is the fastest way to test if a string is "lexicographically less than, equal to, or greater than" the other. Is strcmp() also the fastest way to see if 2 strings exactly match? If not, then what is?

sj95126
  • 6,520
  • 2
  • 15
  • 34
user15159590
  • 13
  • 1
  • 2
  • 6
    `strcmp()` is the only way. If it returns `0`, they match. – Barmar Sep 28 '22 at 23:28
  • 7
    There's negligible performance impact from the fact that it returns two different values when the strings don't match, depending on whether it's less than or greater than. – Barmar Sep 28 '22 at 23:30
  • Related: https://stackoverflow.com/questions/13095513/what-is-the-difference-between-memcmp-strcmp-and-strncmp-in-c – Bob__ Sep 29 '22 at 00:04
  • 4
    The fast way is to store known length strings (a la C++ `std::string`, but you gotta reimplement it yourself). It allows a short-circuit that `strcmp` can't do (compare lengths first, and immediately return false without checking the data at all if they don't match), and then use a known length `memcmp` operation if the lengths match (which can *probably* be a little faster by avoiding unpredictable `NUL` character checks in favor of a known length loop). But yeah, for C-style `NUL`-terminated strings without pre-computed length, `strcmp` will be within a cycle or so of the optimal. – ShadowRanger Sep 29 '22 at 00:07
  • 2
    Show us an example where the difference would matter. `strcmp`'s implementation is almost certainly not slowing down your program. Perhaps you need a different data structure, in which case a motivating example would also help. – alter_igel Sep 29 '22 at 00:18
  • What is the difference? – user16217248 Sep 29 '22 at 00:35
  • 1
    @ShadowRanger Checking for null termination might lead to an extra branch, whereas comparing two chunks of raw memory is about as straight-forward as it goes. So there are good reasons to believe that `memcmp` performs _significantly_ faster, particularly on systems utilizing data and instruction cache. – Lundin Sep 29 '22 at 06:49
  • If the lengths of the strings are already known, `memcpy()` is an ostensibly faster option. – user16217248 Oct 28 '22 at 20:27

2 Answers2

2

I'm aware that the standard strcmp() function is the fastest way to test if a string is "lexicographically less than, equal to, or greater than" the other. Is strcmp() also the fastest way to see if 2 strings exactly match? If not, then what is?

It sounds like the premise of the question is that there might be something faster than strcmp() because strcmp() figures out if the strings are less than or greater than, and you're thinking that maybe there's some other function that only determines if the strings match or not, and that that function would be faster because it somehow is doing less work.

No, there is no such function.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
1

What's the fastest way to test if 2 strings match?

strcmp() provides a speedy function that is certainly faster that any user code for a isolated general purpose string compare. strcmp() spends the lion share of its time testing for the equality of strings and only needs to compare for order as a final step - once inequality is detected. Thus even an optimized library function that only tests for equality is not expected to be significantly faster. @Barmar

Given 2 pointers to strings a, b, the fastest match test is a == b which only compares pointers. If a == b, the pointed to strings match. Yet if a != b, then the pointed to strings may still match or not.

Yet what is the higher goal for this need for the fastest way?

If we have a collection of strings to test repeatedly against a various a, we could store the collection of b stings in a hash table and very quickly compare them via their hashes.

I have used equal = a[0] == b[0] && strcmp(a,b) == 0 as a pre-test that in a given application, significantly sped up compares, yet that was time efficient given the select condition of the anticipated strings to compare and is not a general faster solution.

There are numerous way to speed up a set of string compares. We simply need to take advantage of the larger picture.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256