web analytics
An Alternative Function to strcmp() in C++ Options
Posted: Friday, February 5, 2016 7:59:45 AM

Rank:Advanced Member
Groups: Member
Joined: 12/14/2015
Posts: 182
Points: 894

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)[0] != (t)[0] ? (s)[0] - (t)[0] : 
                strcmp((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:

        strcmp()                9.7

        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.

Posted: Friday, February 5, 2016 7:59:45 AM
Users browsing this topic

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

© 2019 Digcode.com. All rights reserved.