Effect of local buffer optimization

How much faster is the small_string compared to the simple_string? That depends, of course, on what you need to do with it. Let's start with just creating and deleting the strings. To avoid typing the same benchmark code twice, we can use the template benchmark, as follows:

template <typename T>void BM_string_create_short(benchmark::State& state) {    const char* s = "Simple string";    for (auto _ : state) {        REPEAT({ T S(s); benchmark::DoNotOptimize(S); })    }    state.SetItemsProcessed(32*state.iterations());}BENCHMARK_TEMPLATE1(BM_string_create_short, simple_string);BENCHMARK_TEMPLATE1(BM_string_create_short, small_string);

The result is quite impressive:

It gets even better when we try the same test on multiple ...

Get Hands-On Design Patterns with C++ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.