-
-
Save rmartinho/3d787388932af2989da0 to your computer and use it in GitHub Desktop.
This file contains 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
#define NONIUS_RUNNER | |
#include <nonius.h++> | |
#include <string> | |
#include <vector> | |
std::string f(std::string const& s) { | |
return s + "some text"; | |
} | |
void f(std::string const& s, std::string& result) { | |
result = s + "some text"; | |
} | |
void f2(std::string const& s, std::string& result) { | |
result.clear(); | |
result += s; | |
result += "some text"; | |
} | |
void byval(std::string const& s) { | |
for(int i = 0; i < 10; ++i) { | |
volatile std::string x = f(s); | |
(void)x; | |
} | |
} | |
void byref(std::string const& s) { | |
std::string x; | |
for(int i = 0; i < 10; ++i) { | |
f(s, x); | |
} | |
} | |
void byref2(std::string const& s) { | |
std::string x; | |
for(int i = 0; i < 10; ++i) { | |
f2(s, x); | |
} | |
} | |
NONIUS_BENCHMARK("return by value", [](nonius::chronometer meter) { | |
std::string x = "It was big. Really, really big. No, bigger than that. Even bigger. Keep going. More. No, more. Look, we're talking krakens and dreadnoughts for jewelry. It was big!"; | |
meter.measure([&]{ byval(x); }); | |
}) | |
NONIUS_BENCHMARK("return by ref", [](nonius::chronometer meter) { | |
std::string x = "It was big. Really, really big. No, bigger than that. Even bigger. Keep going. More. No, more. Look, we're talking krakens and dreadnoughts for jewelry. It was big!"; | |
meter.measure([&]{ byref(x); }); | |
}) | |
NONIUS_BENCHMARK("return by ref [optimised]", [](nonius::chronometer meter) { | |
std::string x = "It was big. Really, really big. No, bigger than that. Even bigger. Keep going. More. No, more. Look, we're talking krakens and dreadnoughts for jewelry. It was big!"; | |
meter.measure([&]{ byref2(x); }); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment