I was going through a site to benchmark the small functions and methods in c++ for the purpose of optimization and performance and I found some results on strings that I am not able to figure out why. Link http://quick-bench.com/mNexo7ae75tYgt8HYoB17HEHgKc for the benchmark code.
static void StringCreation(benchmark::State& state) {
// Code inside this loop is measured repeatedly
for (auto _ : state) {
std::string created_string("hello");
// Make sure the variable is not optimized away by compiler
benchmark::DoNotOptimize(created_string);
}
}
// Register the function as a benchmark
BENCHMARK(StringCreation);
static void StringCreationMove(benchmark::State& state) {
// Code inside this loop is measured repeatedly
std::string x = "hello";
for (auto _ : state) {
std::string created_string_move(std::move(x));
// Make sure the variable is not optimized away by compiler
benchmark::DoNotOptimize(created_string_move);
}
}
// Register the function as a benchmark
BENCHMARK(StringCreationMove);
static void StringCopy(benchmark::State& state) {
// Code before the loop is not measured
std::string x = "hello";
for (auto _ : state) {
std::string copy(x.c_str());
benchmark::DoNotOptimize(copy);
}
}
BENCHMARK(StringCopy);
static void StringCopyByChar(benchmark::State& state) {
// Code before the loop is not measured
const char* x = "hello";
for (auto _ : state) {
std::string copyChar(x);
benchmark::DoNotOptimize(copyChar);
}
}
BENCHMARK(StringCopyByChar);

The length of the string will have a major effect on specific results, along with things like any small string optimisation. For example it might force a "byte by byte copy" even for a "move", while avoiding the cost of any memory allocations for other operations.
also not really sure what you are expecting.
static void StringCreation
Without optimisation the thing here is probably the need to determine the length on every iteration, as you give it a null terminated string, basically an extra strlen call.
But it is possible this pattern might be optimised when it sees effectively a strlen("literal string"), and then an allocation and copy of fixed length, and it might even see it can skip the allocation due to SSO, leaving it just to assign some fields to constant values.
GCC and Clang appear to do this, I couldn't get MSVC v142 (2019) to do this with a default /O2 release project, it optimised out the "strlen" but not the assignment itself.
static void StringCreationMove(benchmark::State& state) {
This keeps moving from a string which after the first loop is in an unspecified state, so that could do anything from a no-op to copying the entire string. This may depend on the string length, where the implementation has small string optimizations.
static void StringCopy(benchmark::State& state) {
By passing a c_str() this is like the first case, but it is unlikely any compiler will figure out the length is fixed and optimise it, assuming it is even possible within the constraints of the standard.
static void StringCopyByChar(benchmark::State& state) {
This one is like the first case, the compiler probably figures out that your const char will always be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With