Created
July 1, 2021 17:35
-
-
Save emadflash/f5ef61b24c8d32feb18328832a934d81 to your computer and use it in GitHub Desktop.
benchmarking stl string stream - https://quick-bench.com/q/c5iiDBpM4ZpZGtnEK_bGBe9mMbg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct stringstream { | |
std::string stream; | |
stringstream() = default; | |
stringstream(std::string_view&& sv) : stream { std::move(sv) } | |
{} | |
std::string str() { | |
return stream; | |
} | |
std::string str(std::string&& s) { | |
this->stream = s; | |
return stream; | |
} | |
friend stringstream& operator<<(stringstream& ss, std::string_view&& s) { | |
ss.stream += s.data(); | |
return ss; | |
} | |
}; | |
static void MyStringStream(benchmark::State& state) { | |
for (auto _ : state) { | |
stringstream ss {}; | |
ss << "hello" << "world"; | |
benchmark::DoNotOptimize(ss); | |
} | |
} | |
BENCHMARK(MyStringStream); | |
#include <sstream> | |
static void STLStringStream(benchmark::State& state) { | |
for (auto _ : state) { | |
std::stringstream ss {}; | |
ss << "hello" << "world"; | |
benchmark::DoNotOptimize(ss); | |
} | |
} | |
BENCHMARK(STLStringStream); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment