Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > strcmp() vs. std::string::operator==

Reply
Thread Tools

strcmp() vs. std::string::operator==

 
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      10-04-2005
Hi,

I recently wrote two benchmark programs that compared if two strings
were equal: one was a C program that used C char arrays with strcmp(),
and the other was a C++ program that used std::strings with
operator==().

In both programs, the first string consisted of one million
characters (all the letter 'a'). The second string was always one
character longer than the first string (with the letter 'a' for all the
characters).

In the first program (which was written in C), the comparison was
done like this:

int same = strcmp(string1, string2) ? 0 : 1;

In the second program (which was written in C++), the comparison was
done like this:

bool same = (string1 == string2);

These comparisons were performed in a loop that looped a large number
of times. Then the programs were timed to see which ran faster.

I expected the C++ code to run much, much faster than the C code,
since I would think that the std::string:perator==() method would
first check to see if string1.length() == string2.length() and return
false (since two strings of unequal length cannot be equivalent). This
should run much faster than C's strcmp() function, which has to run
until it finds the first unequal character (which, in this case, will
be the one-million-and-one-th character).

However, the results surprised me. The C++ code did indeed run
faster, but not by much. Apparently it's not checking the length of
the strings in the operator==() method.

In fact, by experimenting with different lengths of the strings, I
found that the C code (using strcmp()) would narrowly beat out the C++
code (using std::string:perator==()) if the string sizes were 1000
characters or less, but the C++ code would consistently run faster (but
not my much) if the string sizes were 10,000 characters or longer.

Please correct me if I'm wrong, but wouldn't it make more sense for
the std::string:perator==() method to check the sizes of the strings
before it proceeds to compare every character? I would think that it
would save a lot of processor time when comparing text from large files
with identical headers.

If you think I'm missing something obvious by making this argument,
please don't hesistate to educate me.

Thanks for any input.

-- Jean-Luc

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-04-2005
(E-Mail Removed) wrote:
> Hi,
>
> I recently wrote two benchmark programs that compared if two strings
> were equal: one was a C program that used C char arrays with strcmp(),
> and the other was a C++ program that used std::strings with
> operator==().
>
> In both programs, the first string consisted of one million
> characters (all the letter 'a'). The second string was always one
> character longer than the first string (with the letter 'a' for all the
> characters).
>
> In the first program (which was written in C), the comparison was
> done like this:
>
> int same = strcmp(string1, string2) ? 0 : 1;
>
> In the second program (which was written in C++), the comparison was
> done like this:
>
> bool same = (string1 == string2);
>
> These comparisons were performed in a loop that looped a large number
> of times. Then the programs were timed to see which ran faster.
>
> I expected the C++ code to run much, much faster than the C code,
> since I would think that the std::string:perator==() method would
> first check to see if string1.length() == string2.length() and return
> false (since two strings of unequal length cannot be equivalent). This
> should run much faster than C's strcmp() function, which has to run
> until it finds the first unequal character (which, in this case, will
> be the one-million-and-one-th character).
>
> However, the results surprised me. The C++ code did indeed run
> faster, but not by much. Apparently it's not checking the length of
> the strings in the operator==() method.
>
> In fact, by experimenting with different lengths of the strings, I
> found that the C code (using strcmp()) would narrowly beat out the C++
> code (using std::string:perator==()) if the string sizes were 1000
> characters or less, but the C++ code would consistently run faster (but
> not my much) if the string sizes were 10,000 characters or longer.
>
> Please correct me if I'm wrong, but wouldn't it make more sense for
> the std::string:perator==() method to check the sizes of the strings
> before it proceeds to compare every character? I would think that it
> would save a lot of processor time when comparing text from large files
> with identical headers.
>
> If you think I'm missing something obvious by making this argument,
> please don't hesistate to educate me.
>
> Thanks for any input.
>
> -- Jean-Luc


I think this is dependent on your standard library implementation. I
see that STL-port does length checking first.

Cheers! --M

 
Reply With Quote
 
 
 
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-04-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> If you think I'm missing something obvious by making this argument,
> please don't hesistate to educate me.


You are missing to post the code of that benchmark.

--
Salu2
 
Reply With Quote
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      10-04-2005
mlimber wrote:
>
> I think this is dependent on your standard
> library implementation. I see that STL-port
> does length checking first.



Hey, thanks! I went to http://www.stlport.org/ and looked around at
the source code. Sure enough, the operator==() method (in a file named
"_string.h" of STLport) does look like it checks the length before
comparing:

{
return __x.size() == __y.size()
&& _Traits::compare(__x.data(),
__y.data(),
__x.size()) == 0;
}

Of course, I still wonder why other implementations don't do the
same. I would think that the cost of comparing the size of the strings
is negligible, so I don't see any reason why it shouldn't be done.

Thanks again.

-- Jean-Luc

 
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




Advertisments