Description
Benchmark is Google Benchmark, which is mainly used to test the benchmark function of functions in C++.
A great example is as follows.
demo.cpp:
#include <benchmark/benchmark.h>
#include <iostream>
#include <string>
using namespace std;
void demo()
{
string str = "hello world";
str.size();
}
static void BM_demo(benchmark::State& state) {
for (auto _ : state)
demo();
}
// Register the function as a benchmark
BENCHMARK(BM_demo); //register test function
BENCHMARK_MAIN(); //program entry
In the code above, demo() is the function to be tested. static void BM_demo(...) is a part of the benchmark framework, which is responsible for testing the performance of the function demo().
In the result above, the first column is the program name, the second column is the clock time that iterates once, the third column is the CPU time that iterates once, and the fourth column is the number of iterations of the loop.
In this project, a basic cpp file is list_bench.cpp. This is the code.
#include <benchmark/benchmark.h>
#include "list.hpp"
static list<long> gen(int64_t n) {
auto normal = list<long>();
for (long i=0; i<n; i++)
normal << i;
return normal;
}
static void N (benchmark::State& state) {
auto list = gen(state.range(0));
for (auto _ : state)
N (list);
}
BENCHMARK (N)
->Arg(1)
->Arg(2)
->Arg(4)
->Arg(8)
->Arg(16)
->Arg(32)
->Arg(64)
->Arg(128)
->Arg(256)
->Arg(512)
->Arg(1024);
Gen(...) is used to generate a linked list, static void N(...) function is used to test the performance of the N(list) function.