example2_using_google_benchmark_library.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <benchmark/benchmark.h>
#include <vector>
#include <algorithm>

static void BM_VectorSort(benchmark::State& state) {
    for (auto _ : state) {
        state.PauseTiming();
        std::vector<int> vec(state.range(0));
        for (int& i : vec) {
            i = rand() % state.range(0);
        }
        state.ResumeTiming();

        std::sort(vec.begin(), vec.end());
    }
}

BENCHMARK(BM_VectorSort)
    ->Range(8, 8<<10);

BENCHMARK_MAIN();
Back to benchmarking