469,604 Members | 2,381 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,604 developers. It's quick & easy.

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

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::operator==() 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::operator==()) 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::operator==() 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

Oct 4 '05 #1
3 16132
jl_p...@hotmail.com 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::operator==() 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::operator==()) 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::operator==() 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

Oct 4 '05 #2
jl*****@hotmail.com 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
Oct 4 '05 #3
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

Oct 4 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Angus Leeming | last post: by
2 posts views Thread by Matthias Käppler | last post: by
4 posts views Thread by Matthias | last post: by
3 posts views Thread by Heiko Hund | last post: by
9 posts views Thread by Jim Langston | last post: by
84 posts views Thread by Peter Olcott | last post: by
10 posts views Thread by lovecreatesbea... | last post: by
4 posts views Thread by daroman | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.