You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am a newcomer to the Abseil library. I learned from the official documentation(https://abseil.io/docs/cpp/guides/strings) that StrAppend is more efficient than concatenation operations. I attempted to use Google Benchmark to test the performance of both, but I arrived at a contrary conclusion. I would like to inquire if there might be an issue with my usage or testing methodology.
The code is as follows:
#include <string>
#include <benchmark/benchmark.h>
#include "absl/strings/str_cat.h"
static void BM_StringCon(benchmark::State& state) {
std::string s1 = "A string";
std::string another = " and another string";
for (auto _ : state) {
s1 += " and some other string" + another;
}
}
// Register the function as a benchmark
BENCHMARK(BM_StringCon);
// Define another benchmark
static void BM_StringCat(benchmark::State& state) {
std::string s1 = "A string";
std::string another = " and another string";
for (auto _ : state) {
absl::StrAppend(&s1, " and some other string", another);
}
}
BENCHMARK(BM_StringCat);
BENCHMARK_MAIN();
google benchmark:
2024-04-20T18:56:39+08:00
Running ./str_append
Run on (48 X 2500.59 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x24)
L1 Instruction 32 KiB (x24)
L2 Unified 256 KiB (x24)
L3 Unified 30720 KiB (x2)
Load Average: 0.30, 0.19, 0.21
-------------------------------------------------------
Benchmark Time CPU Iterations
-------------------------------------------------------
BM_StringCon 156 ns 156 ns 5314965
BM_StringCat 185 ns 185 ns 3924664
Compile with g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, Abseil using the latest version
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am a newcomer to the Abseil library. I learned from the official documentation(https://abseil.io/docs/cpp/guides/strings) that StrAppend is more efficient than concatenation operations. I attempted to use Google Benchmark to test the performance of both, but I arrived at a contrary conclusion. I would like to inquire if there might be an issue with my usage or testing methodology.
The code is as follows:
google benchmark:
Compile with g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, Abseil using the latest version
Thanks for any help!
Beta Was this translation helpful? Give feedback.
All reactions