See this : https://quick-bench.com/q/HCX5vCh3XSrwH6DpWNRFIDjbkBs
The code is below(gcc -O3 build):
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 StringCopy(benchmark::State& state) {
// Code before the loop is not measured
std::string x = "hello";
for (auto _ : state) {
std::string copy(x);
}
}
BENCHMARK(StringCopy);
The link result says StringCreation
is 2.6 times faster than StringCopy
.
I did it locally on x86_64, and the result is following:
Run on (4 X 3192.01 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x4)
L1 Instruction 32 KiB (x4)
L2 Unified 256 KiB (x4)
L3 Unified 12288 KiB (x4)
Load Average: 0.15, 0.98, 0.63
---------------------------------------------------------
Benchmark Time CPU Iterations
---------------------------------------------------------
StringCreation 1.27 ns 1.27 ns 552871452
StringCopy 3.99 ns 3.98 ns 177720572
I don’t understand what metric can be used to compare the two in a fair
way ? If iterations are not same, we can’t just say the shortest Time
is the best implementation, right ? How the 2.6 times faster
come from ?