473,570 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strcmp() vs. std::string::op erator==

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::op erator==() 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::op erator==()) 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::op erator==() 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 17082
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::op erator==() 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::op erator==()) 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::op erator==() 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::compar e(__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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
8155
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That...
2
5838
by: Matthias Kšppler | last post by:
Hi, I just browsed libstdc++6-doc and stumbled over a string operation I haven't noticed before: string::push_back. If for example I want to append single characters to an std::string, which one would be better, or is there any difference at all: mystr.push_back( c ); or
4
2231
by: Matthias | last post by:
Now I am confused. I am doing a "less" comparison in my program between strings, to see which strings in some range precede others. This is my approach: class SortedByName { public: bool operator() (const boost::filesystem::path& lhs, const boost::filesystem::path& rhs) {
3
2833
by: Heiko Hund | last post by:
Hi, I do not understand the deeper reason for the following compiler error $ g++ test.cpp test.cpp: In function `int main()': test.cpp:41: error: `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' is an inaccessible base of `Path' I'm using Debians
9
3289
by: Jim Langston | last post by:
#include <string> int main () { std::string MyString = "Testing"; MyString = " " + MyString; } This works in Microsoft Visual C++ .net 2003
84
15809
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
10
9038
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s contain a single '\'? Is it better to use std::string::size or std::string::empty to deal with this condition? Thank you. string s = ""; if (s == ""); if (s.size == 0); if (s.empty());
4
11216
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and with GCC (Cygwin and Linux) and my problem is when i try do this int main(int argc, char **argv) { array<std::stringa(10); a = "Huhuhu"; <---...
16
3648
by: yu_kuo | last post by:
Is there any comparison data on perfomance difference between std::string and c style string? Or maybe if there are source code which could be used to measuer on different compiler/platform, in a systematic way?
0
7636
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7948
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8150
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7700
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5247
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.