sbaldu / benchmark

Basic tools for benchmarking C++ code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

benchmark

This project contains some basic tools for benchmarking C++ code.
These tools are thought to be simple to understand and to use

Installation

The library can be installed very simply by using CMake, with the following commands:

cmake -B build ;
cd build ;
sudo make install

Alternatively it can by used by adding it to an existing project as a git submodule:

git submodule add https://github.com/sbaldu/benchmark.git path_to_module_folder

Usage

Here is an example of the use of this library:

#include <algorithm>
#include <iostream>
#include <vector>

#include "Bench.hpp"

// function that calculates the sum of all the elements in a vector
template <typename T>
T accumulate(const std::vector<T>& vec) {
  T result{};
  for (const auto x : vec) {
	result += x;
  }

  return result;
}

int main() {
  const int N{1000000};

  // generate a vector and fill it with random elements
  std::vector<int> v(N);
  std::ranges::for_each(v, [](auto& x) { x = std::rand(); });

  // benchmark the accumulate function
  const int n_rep{1000};
  sb::Bench<long long int> b(n_rep);
  b.benchmark(accumulate<int>, v);
  b.print<sb::microseconds>();
}

About

Basic tools for benchmarking C++ code

License:GNU General Public License v2.0


Languages

Language:C++ 90.3%Language:CMake 9.7%