2

I have tested c++ std::string.length() vs strlen() function on VS2017 as below. The result is:

msvs 2017

what surprised me is that string.length() is 7x slower than strnlen(). But I suppose string.length() is an O(1) operation, and strlen() is an O(n) operation.

I have also tested it on GNU GCC v7.1.1 on coding ground

And it shows string.length() is slight faster than strlen() (not as much as I expected.)

enter image description here

Why is that? something wrong in my test code?

class stopwatch
{
public:
    stopwatch()
    {
        start = std::chrono::system_clock::now();
    }
    ~stopwatch()
    {
        auto end = std::chrono::system_clock::now();
        auto elasped = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
        cout << "elasped: " << elasped.count() << endl;
    }

private:
    chrono::system_clock::time_point start;
};

void test_std_str(const string& ss)
{
    stopwatch sw;
    const int max = 100000;
    int sum = 0;
    for (int i = 0; i < max; i++)
    {
        sum += ss.length();
    }
    cout<< "test_string: sum = " << sum << endl;
}

void test_c_str(const char* str)
{
    stopwatch sw;
    const int max = 100000;
    int sum = 0;
    for (int i = 0; i < max; i++)
    {
        sum += strlen(str);
    }
    cout << "test_c_str: sum = " << sum << endl;
}

int main()
{
    std::string ss = "abcdef";
    const char* str = "abcdef";
    int x;
    cin >> x;
    if (x > 0)
    {
        ss = "helloworld";
        str = "helloworld";
    }
    test_std_str(ss);
    test_c_str(str);
    return 0;
}
ricky
  • 2,058
  • 4
  • 23
  • 49
  • 1
    Are you sure you are testing `length()` and not the optimiser, or the default non-optimised code? The first test maybe has hidden initialisation costs, so what happens if you swap the order? Multiple runs on your coding.ground link gives widely varying results, because you are measuring wallclock time, not processor time. – Ken Y-N Nov 14 '18 at 01:42
  • @KenY-N Yes, I was just typing the same thing. Swapping the order shows the opposite results of what the OP sees. – DeiDei Nov 14 '18 at 01:46
  • Also, coding.ground allows me to add `-O2`, so we get 2 milliseconds for both, if we add an extra call to `test_std_str()` that serves to (I guess) initialise `std::cout` buffers, etc. – Ken Y-N Nov 14 '18 at 01:48
  • @KenY-N Release build on vs seems has close time cost. But why test_std_str() is not significant faster? – ricky Nov 14 '18 at 01:53
  • 3
    @ricky Your `stopwatch` local variable is also timing the `cout` calls in each of those functions. So these results are basically meaningless, unless you localize the scope of `stopwatch`. Example: `{ stopwatch sw; ... /*code without the cout*/ } std::cout << ...`. That is what your test shoujld be doing. – PaulMcKenzie Nov 14 '18 at 01:59
  • 2
    @PaulMcKenzie You are right! localize the scope of `stopwatch` makes `test_std_str` output elasped: 0 – ricky Nov 14 '18 at 02:04
  • Well, you had a good idea making `stopwatch` an RAII type, but the scope was too large. – PaulMcKenzie Nov 14 '18 at 02:08
  • @ricky you should have posted your solution as an answer instead of as an edit. – Remy Lebeau Nov 14 '18 at 03:58
  • @RemyLebeau sure! – ricky Nov 14 '18 at 04:03

1 Answers1

2

To answer my own question:

As suggested by @Ken Y-N and @PaulMcKenzie, I localize the scope of stopwatch to exclude the time of cout, and build it with Release build. The result is make sense to me now!

enter image description here

ricky
  • 2,058
  • 4
  • 23
  • 49