Are there any guidelines available that can be followed before calling standard string operation related functions in C?
For example, how much optimization will comparing the first character of two strings (and checking if they are equal) before calling strcmp provide?
What types of overhead related to string related functions in C can be expected, and what mechanisms will help avoid them?
Thanks!
The string functions are there for you to use them. If you need to compare two strings, call
strcmp. Don’t worry about tiny performance difference, which are mostly imagined anyway. Get your code working first.First, to answer any question about performance, if you ask “How much optimization will…” the answer is “Profile!” Nobody can predict how fast something is going to run. The implementation of the C stdlib has been continuously improved for years, any optimization tricks you try to come up with might hurt it.
For example, I think GCC will use vectorization when comparing strings, so you’re actually comparing some 4-8 elements at a time. Were you expecting that? Doing your single character compare might actually slow it down.
That said, a typical implementation just checks character for character, so you’d simply be moving one comparison out of the loop, for no net gain. (But as stated, maybe for a net loss!)
So the guideline is:
And optimize the rational way: with evidence and testing, not with guessing.