Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

More efficient strcmp

 
 
pembed2012
Guest
Posts: n/a
 
      03-13-2012
Dear friends

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.

Thank you

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));
}
 
Reply With Quote
 
 
 
 
Kleuske
Guest
Posts: n/a
 
      03-13-2012
On Tue, 13 Mar 2012 21:59:41 +0000, pembed2012 saw fit to publish the
following:

> Dear friends
>
> 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.
>
> Thank you
>
> 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));
> }


What if the strings are longer than an unsigned long and differ only in the
last bit?

Just asking...

--
Women are more easily and more deeply terrified ... generating more
sheer horror than the male of the species.
-- Spock, "Wolf in the Fold", stardate 3615.4
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      03-13-2012
On 03/13/2012 05:59 PM, pembed2012 wrote:
> Dear friends
>
> 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.
>
> Thank you
>
> 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));
> }


char strings[0] = "a\0a\0b";

Compare the results of strcmp(strings, strings+2) and
strcmp_fast(strings, strings+2).

Fast is nice; defined behavior is better; correct behavior is even better.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      03-13-2012
pembed2012 <(E-Mail Removed)> writes:
>I have created a more efficient strcmp function.


What did you compare its efficiency with?
How did you measure that it is more efficient?

 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      03-13-2012


"Kleuske" <(E-Mail Removed)> wrote in message
news:jjogv1$iqp$(E-Mail Removed)...
> On Tue, 13 Mar 2012 21:59:41 +0000, pembed2012 saw fit to publish the
> following:


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


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

>
> What if the strings are longer than an unsigned long and differ only in
> the
> last bit?


Or shorter.

--
Bartc

 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      03-13-2012
On 13-Mar-12 16:59, pembed2012 wrote:
> 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.
>
> Thank you
>
> 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));
> }


You can make lots of things faster if you aren't worried about the code
crashing or giving incorrect answers when called with valid inputs.

However, that is a rather dubious definition of "more efficient".

Note that any reasonable strcmp() implementation _does_ use the
multi-byte comparison trick; however, unlike your code, they also ensure
that the pointers are aligned, strings are not compared past their ends,
etc. so that the results are _correct_, and that does slow things down a
bit. Ditto for memcmp().

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      03-13-2012
On 03/13/2012 06:16 PM, Kleuske wrote:
> On Tue, 13 Mar 2012 21:59:41 +0000, pembed2012 saw fit to publish the
> following:
>
>> Dear friends
>>
>> 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.
>>
>> Thank you
>>
>> 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));
>> }

>
> What if the strings are longer than an unsigned long and differ only in the
> last bit?


Then it ends up calling strcmp(s,t), and ends up actually being slower.
That's not one of the several serious problems with this code:

1. Either s or t might not have an alignment suitable for conversion to
unsigned long*. Behavior: undefined.
2. *uls or *ult each access sizeof(unsigned long) bytes; if the strings
pointed at are shorter than that, those expressions might attempt to
access memory that the program doesn't have permission to read.
Behavior: undefined.
3. The comparison compares the entire sizeof(unsigned long) bytes, even
if that includes bytes past the end of the strings being compared.
Behavior: incorrect
4. In the unlikely case that unsigned long has padding bits, they won't
participate in the comparison. Behavior: incorrect.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-13-2012
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));
> }


The trouble is it doesn't work, regardless of the machine's byte
ordering. There are two big issues. First, a pointer to a string won't
always be correctly aligned for a larger access, and, second:

char test[5] = {0, 0, 1};
strcmp_fast(test, test+1);

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!

--
Ben.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      03-13-2012
Le 13/03/12 23:19, James Kuyper a écrit :
>
> char strings[0] = "a\0a\0b";
>


?????

An array of zero chars?
And you assign a char * to it?

Can't parse that, sorry.

Maybe you wanted to write:

char *strings = "a\0a\0b";
 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      03-13-2012
On 2012-03-13, pembed2012 <(E-Mail Removed)> wrote:
> Dear friends
>
> I have created a more efficient strcmp function.


Gee, thanks a lot.

Now, you wouldn't happen to have a more efficient paper towel for wiping coffee
spit from a monitor?
 
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