tags:

views:

298

answers:

5

I want another condition --still maintaining a fast execution time but safer-- where i return false if either or both strings is empty:

int speicial_strcmp(char *str1, char* str2 )
{

    if(*str1==*str2 =='\0')
         return 0;

     return strcmp(str1,str2);

}
+4  A: 

Even if you implement the early-out tests you suggest correctly, you are very unlikely to make things any faster by doing this sort of thing - strcmp will already be doing this or nearly this.

moonshadow
+3  A: 
if( *str1 == 0 || *str2 == 0 )
   return 0;
stijn
Are you sure you meant to `return 0;` here? The questioner wants to return 'false' (I take, that means 'not equal') in case either or both of the strings are empty. If you want to keep the strcmp semantics, you should probably only `return -1;` or something else than 0 (which means 'the strings are equal` as far as strcmp is concerned).
Frerich Raabe
Except that the originator's code returned 0 in the special case? So I don't think we can know what he meant by 'false'
Douglas Leeder
@Frerich: what Douglas said.. ;P
stijn
+12  A: 

No, that's not a good way to do it, because it doesn't work.

if(*str1==*str2 =='\0')

will get evaluated as:

bool tmp1 = *str1==*str2;
bool tmp2 = tmp1 == '\0';
if (tmp2)

In other words, because the bool will get promoted to an integer, your test will return true whenever the strings start with different characters (tmp1 will be false, which gets converted to 0, and so tmp2 becomes true)

Don't try to outsmart the compiler. Writing fast code is not about writing as few lines of code as possible, or even as short lines as possible. Even if chaining together == in this manner was meaningful, there's no reason why it'd be faster. Just write code that you understand, and can write correctly.

jalf
+1  A: 

The example you've given won't even run correctly. strcmp() will stop at the first differing characters. If both strings are empty, as satisfied by your "special case" above, this will be handled just as quickly as the example you've given.

By adding a special handler for both strings empty as above, you've only made the cases where they aren't, correspondingly slower.

Matt Joiner
+4  A: 

Here's the code for strcmp():

int
strcmp (p1, p2)
     const char *p1;
     const char *p2;
{
  register const unsigned char *s1 = (const unsigned char *) p1;
  register const unsigned char *s2 = (const unsigned char *) p2;
  unsigned reg_char c1, c2;

  do
    {
      c1 = (unsigned char) *s1++;
      c2 = (unsigned char) *s2++;
      if (c1 == '\0')
    return c1 - c2;
    }
  while (c1 == c2);

  return c1 - c2;
}

It's already as fast as it could meaningfully be. Your extraneous check only makes the cases you're not interested in, slower.

Michael Foukarakis
I'd be surprised if this is the code used. For starters, it's an important-enough function that compiler intrinsics make sense. Otherwise, I'd expect it to be `inline`. The `register` keyword is ignored nowadays, so who'd still use it? And why is there a duplicate return statement? `while(c1 != 0 ` works too.
MSalters
But he wants to return 0 if either string is empty. This code does not meat that constraint
Martin York
@MSalters: perhaps whoever wrote it is more suitable to answer your questions. I just copied and pasted the code directly from glibc to prove a point with regard to the OPs need for fast execution. Besides, even his own code has conflicting constraints - he'd better clarify or read the manual entry for `strcmp()`.
Michael Foukarakis