Skip to content

Instantly share code, notes, and snippets.

@rmartinho
Last active August 29, 2015 13:56
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 rmartinho/3d787388932af2989da0 to your computer and use it in GitHub Desktop.
Save rmartinho/3d787388932af2989da0 to your computer and use it in GitHub Desktop.
#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