1

I wanted to know if there are even faster ways of comparing strings in C than using strcmp(), especially when I have to compare a string with multiple pre-defined strings in a switch statement fashion. In my application, the string to be compared can sometimes go as big as 1000 chars, so was just thinking if strcmp() is sufficient enough or if there exists better and efficient way which I am not familiar with. I am actually working on a low power embedded IoT project where more CPU cycles cost power.

20
  • 6
    Is this a measured bottleneck in your code? Don't fall into the trap of premature optimizations. Commented Oct 1, 2018 at 7:44
  • 2
    What makes you think that even this macro of yours is "faster"? What makes you think any claims for "faster" are worth the fact that you're now facing all the potential troubles that come with multiple macro parameter expansion, and people scratching their heads about your non-standard solution to comparing strings? Commented Oct 1, 2018 at 7:46
  • 1
    That macro is not "nothing but strcmp". Before that, A and B are evaluated for non-null test, and skips invoking strcmp and returns 0 if either are null. A bit misleading, considering if both are null they're certainly "equal", but still report 0. Regardless, if string comparison is really the bottleneck, perhaps you'd be better off analyzing how to not do it as much rather than trying to optimize this thing. Commented Oct 1, 2018 at 7:47
  • 2
    If the goal is to emulate a switch statement where the cases are hardcoded strings, then a hash map, or binary tree, or a trie are all possible options. Commented Oct 1, 2018 at 8:04
  • 1
    I still think you don't understand the issue here. Whether any given implementation of some functionality is "faster" than another depends heavily on the input data and usage profile. You should rely on strcmp being well-optimized for the general case. If your case is specific, you need to specify the details of your case. Your macro example does not really help, because it is not an "optimization" at all, but a change of the strcmp semantics -- an empty string now always compares equal to anything else. user3386109 has pointed to potential candidates for specific case optimizations. Commented Oct 1, 2018 at 8:19

1 Answer 1

6

It doesn't sound as if the problem has as much to do with strcmp itself, as how you use it.

The fastest way to compare strings against a table of pre-defined strings, is to ensure that the strings are sorted alphabetically, then use binary search. Where strcmp acts as the comparison function. C standard bsearch may or may not be feasible on an embedded system. Otherwise, it is fairly simple to implement yourself.

That is, unless the number of strings are vast. Then at some point, some manner of hash table will perform better than searching. To give an exact answer of what performs best, one needs all the details of the data.

With fixed-length strings you can improve performance ever so slightly by using memcmp instead - that way you don't have to check against null termination. But that's really a micro-optimization.

Sign up to request clarification or add additional context in comments.

2 Comments

A hash table would probably be even faster.
@LeeDanielCrocker Generally, yes, unless this is a limited amount of strings. A hash table has almost random access time, but calling the hash function gives a bit of overhead. Typically it contains division/modulus etc which might chew up a fair amount of CPU cycles. So it isn't obvious where exactly the hash table starts to out-perform binary search, only that it eventually will, at table size "x" and beyond.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.