Skip to content

Instantly share code, notes, and snippets.

@danielkraic
Created September 2, 2016 17:57
Show Gist options
  • Save danielkraic/f567a3d023517692cb6fb15854f5bc9f to your computer and use it in GitHub Desktop.
Save danielkraic/f567a3d023517692cb6fb15854f5bc9f to your computer and use it in GitHub Desktop.
google benchmark c++ example
#include <iostream>
#include <sstream>
#include <benchmark/benchmark.h>
using namespace std;
static void BM_bench01(benchmark::State& st) {
while (st.KeepRunning()) {
std::stringstream s;
for (size_t i = 0; i < st.range(0); i++) {
s << "nemam cas " << i << '\n';
}
auto str = s.str();
}
}
static void BM_bench02(benchmark::State& st) {
while (st.KeepRunning()) {
std::stringstream s;
for (size_t i = 0; i < st.range(0); i++) {
s << "nemam cas " << i << std::endl;
}
auto str = s.str();
}
}
static void BM_bench03(benchmark::State& st) {
while (st.KeepRunning()) {
std::string s;
for (size_t i = 0; i < st.range(0); i++) {
s += "nemam cas " + std::to_string(i) + "\n";
}
}
}
BENCHMARK(BM_bench01)->RangeMultiplier(2)->Range(8, 8<<10);;
BENCHMARK(BM_bench02)->RangeMultiplier(2)->Range(8, 8<<10);;
BENCHMARK(BM_bench03)->RangeMultiplier(2)->Range(8, 8<<10);;
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment