Skip to content

Instantly share code, notes, and snippets.

@emadflash
Created July 1, 2021 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emadflash/f5ef61b24c8d32feb18328832a934d81 to your computer and use it in GitHub Desktop.
Save emadflash/f5ef61b24c8d32feb18328832a934d81 to your computer and use it in GitHub Desktop.
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