More efficient strcmp

G

Gof

vaysagekv said:
Do you mean
int strcmp_fast(char* s,char* t){
unsigned long* uls=(unsigned long*)s;
unsigned long* ult=(unsigned long*)t;
if(*uls>*ult) return(1);
if(*uls<*ult) return(-1);
else return(strcmp(s,t));
}

Besides obvious misconceptions, pointed by other people in this thread,
I'm sensitive to constness of variables and arguments. By passing pointers
to a function without const qualifiers, you make people think that your
function is going to modify the strings.

I think that it would be better this way (still with errors mentoined
in other posts):

int strcmp_fast(const char *s, const char *t)
{
const unsigned long *uls = (const unsigned long *) s;
const unsigned long *ult = (const unsigned long *) t;

if (*uls > *ult)
return 1;

if (*uls < *ult)
return -1;

return strcmp(s, t);
}
 
B

BartC

Besides obvious misconceptions, pointed by other people in this thread,
I'm sensitive to constness of variables and arguments. By passing pointers
to a function without const qualifiers, you make people think that your
function is going to modify the strings.

I think that it would be better this way (still with errors mentoined
in other posts):

int strcmp_fast(const char *s, const char *t)
{
const unsigned long *uls = (const unsigned long *) s;
const unsigned long *ult = (const unsigned long *) t;

if (*uls > *ult)
return 1;

if (*uls < *ult)
return -1;

return strcmp(s, t);
}

That's fine. But you've turned a simple function that could be taken in at a
glance, into something where you almost have to hunt for the code amidst all
the 'consts'!

Wouldn't just a comment to the same effect do?
 
T

Tim Rentsch

Bl0ckeduser said:
Hmm, I have to do the comparisons using the unsigned char type (or
perhaps another unsigned type, like the OP), right ?
Right!



By the way, thanks for reviewing my code :). As a hobbyist, it's
always a thrill to talk with experts.

You're welcome. Some other ideas, starting at the previous
version:
int strcmp_recursive(char* s,char* t){
if(!*s && *s==*t) return(0);
if(*s>*t) return(1);
if(*s<*t) return(-1);
return(strcmp_recursive(s+1,t+1));
}


Personally I prefer more use of horizontal white space.
Also, sometimes you may want to combine several cases
into a single return and then using a conditional expression
in the return statement:

int
ustrcmp( const unsigned char *s, const unsigned char *t ){
if( *s != *t || *s == 0 ) return *s<*t ? -1 : *s>*t ? 1 : 0;
return ustrcmp( s+1, t+1 );
}

There is a well-known idiom for the expression in the first
return statement:

int
ustrcmp( const unsigned char *s, const unsigned char *t ){
if( *s != *t || *s == 0 ) return (*s > *t) - (*s < *t);
return ustrcmp( s+1, t+1 );
}

We can use the conditional-expression-folding technique again to
reduce this last version to a single return statement;

int
ustrcmp( const unsigned char *s, const unsigned char *t ){
return *s && *s == *t ? ustrcmp( s+1, t+1 ) : (*s>*t) - (*s<*t);
}

Obviously the sub-branches of the conditional expression could
be written in the other order with the condition reversed;
it depends on how you think of the function as working.

Please bear in mind, I don't mean to advocate any of these as
being better than some other way of writing this function.
But I think it's useful to see different variations and go
back and forth between them easily.
 
J

Joachim Schmitz

Ben said:
Without having done tests, aren't you jumping the gun by saying it's
more efficient?
By the way, the Campaign to Remove All Spaces from Source would no
approve of your half-hearted effort -- four whole spaces have survived
the delete key!

I count 14 spaces (plus 6 linefeeds) that are not strictly required?

Bye, Jojo
 
B

Ben Bacarisse

Joachim Schmitz said:
I count 14 spaces (plus 6 linefeeds) that are not strictly required?

CRASS laments the fact, but very few people go all the way -- I was
assuming a style that had some indentation (and therefore also line
break) rules.

But, yes, you are right, a truly committed supporter of CRASS would go
much further.
 
J

Jorgen Grahn

That's fine. But you've turned a simple function that could be taken in at a
glance, into something where you almost have to hunt for the code amidst all
the 'consts'!

Wouldn't just a comment to the same effect do?

No; people expect to see such things expressed using the 'const'
keyword rather than using comments. If I see a comment saying "I won't
be modifying this piece of data, honest" I get suspicious.

(Not that I understand what the code is trying to do. I haven't
followed the thread, but it seems fundamentally broken, with or
without const.)

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top