Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > More efficient strcmp

Reply
Thread Tools

More efficient strcmp

 
 
Gof
Guest
Posts: n/a
 
      03-15-2012
vaysagekv <(E-Mail Removed)> wrote:

> 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);
}

--
Gof
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      03-15-2012
"Gof" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> vaysagekv <(E-Mail Removed)> wrote:
>
>> Do you mean
>> int strcmp_fast(char* s,char* t){
>> unsigned long* uls=(unsigned long*)s;


> 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?

--
Bartc



 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      03-16-2012
Bl0ckeduser <(E-Mail Removed)> writes:

> Tim Rentsch wrote:
>> You have made an important semantic change (not counting the
>> changes that allow termination and provide for returning 0
>> if the strings are equal). Do you see what it is? Hint:
>> this version of strcmp does not match the specification for
>> strcmp() in the standard library (even ignoring the 'const'
>> modifiers in strcmp()'s prototype).

>
> 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, 'return' statements don't need parentheses
>> around the return expression. The statements
>>
>> return 0;
>> return strcmp_recursive( s+1, t+1 );
>>
>> will work just fine.

>
> 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.
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      03-16-2012
Ben Bacarisse wrote:
> pembed2012 <(E-Mail Removed)> writes:
>
>> I have created a more efficient strcmp function. However it only
>> works on big endian architectures. I would like someone with same to
>> run some timing tests to see how much is the speed up.

>
> Without having done tests, aren't you jumping the gun by saying it's
> more efficient?
>
>> int strcmp_fast(char* s,char* t){
>> uls=(unsigned long*)s;
>> ult=(unsigned long*)t;
>> if(*uls>*ult) return(1);
>> if(*uls<*ult) return(-1);
>> else return(strcmp(s,t));
>> }

<snip>
> 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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-16-2012
"Joachim Schmitz" <(E-Mail Removed)> writes:

> Ben Bacarisse wrote:
>> pembed2012 <(E-Mail Removed)> writes:
>>
>>> I have created a more efficient strcmp function. However it only
>>> works on big endian architectures. I would like someone with same to
>>> run some timing tests to see how much is the speed up.

>>
>> Without having done tests, aren't you jumping the gun by saying it's
>> more efficient?
>>
>>> int strcmp_fast(char* s,char* t){
>>> uls=(unsigned long*)s;
>>> ult=(unsigned long*)t;
>>> if(*uls>*ult) return(1);
>>> if(*uls<*ult) return(-1);
>>> else return(strcmp(s,t));
>>> }

> <snip>
>> 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?


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.

--
Ben.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      03-25-2012
On Thu, 2012-03-15, BartC wrote:
> "Gof" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> vaysagekv <(E-Mail Removed)> wrote:
>>
>>> Do you mean
>>> int strcmp_fast(char* s,char* t){
>>> unsigned long* uls=(unsigned long*)s;

>
>> 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?


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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
segfault when strcmp Robert Mens C Programming 6 10-23-2003 02:53 AM
strcmp muser C++ 6 10-09-2003 08:18 AM
strcmp problem Shane Peck C++ 6 09-22-2003 05:44 PM
strcmp but with '\n' as the terrminator Allan Bruce C Programming 53 07-30-2003 07:38 PM
please help with strcmp() Andrej Hocevar C Programming 3 07-19-2003 04:41 PM



Advertisments