Development and Programming
C and C++
An Alternative Function to strcmp() in C++
When we are comparing two strings, if the first characters of each string do not match, there's no need to go further by calling strcmp(); we already know the answer. So we might use an inline function in C++ to encapsulate this logic:
inline int local_strcmp(const char* s, const char* t)
return (*s != *t ? *s - *t : strcmp(s, t));
Another way to implement the same idea is via a C macro:
#define local_strcmp(s, t) ((s) != (t) ? (s) - (t) :
This approach has a couple of disadvantages, however. Macros are hard to get right because of the need to parenthesize arguments so as to avoid subtly wrong semantics. Writing local_strcmp() as a real function is more natural.
And macros are less likely to be understood by development tools such as browsers or debuggers. Inline functions are also a source of problems for such tools, but they at least are part of the C++ language proper, and many C++ compilers have a way of disabling inlining to help address this problem.
How much speedup is this approach good for? In a word stemming program which takes words such as "motoring" and reduces them to their root stem, in this case "motor", for input of about 65000 words, the times in seconds were:
inline local_strcmp() 7.5
#define local_strcmp() 7.5
or a savings of about 23%. Obviously, this figure will vary with the compiler and the application.
This particular speedup is achieved by exploiting a common case -- the case where the first letters of two strings are different. For applications involving English words, this is often a good assumption. For some other types of strings, it may not be.
© 2018 Digcode.com. All rights reserved.