J
jl_post
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 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