Skip to content

benchmark.hpp

Gecero edited this page Nov 24, 2018 · 4 revisions

benchmark.hpp

benchmark.hpp is a portable, small, one file only, compact, more or less cross plattform benchmarking library that uses the c++11 standard.


WARNING: this library DOES NOT do accurate benchmarks neither microbenchmarks. You should use this library for comparing the results with results of other benchmarks made by this benchmarker (remember that the benchmark results are different on different devices).

code examples and how to use

#include <iostream>
#include "benchmark.hpp"

// this is just a little function that we are going to benchmark
void benchmarkMe() {
	double sum = 0.0;
	for(int i = 0; i < 100; i++) {
		sum += rand()/5000.0;
	}
}

int main() {
	benchmark<double> bencher;
	// For the defualt constructer you should use float, double, or long double
	// if you want to get eg. the average time consumed by the benchmarks it is
	// going to return the time in seconds in eg. double floating point accuracy
	
	uint64_t benchmarks = 10000;
	// This is the amount of benchmarks being made. this isnt necessary but is
	// useful. i usually do 10000+ benchmarks because small functions get
	// benchmarked pretty fast and you can get a more precise average at the end
	
	bencher.setNumberOfEstimatedBenchmarks(benchmarks);
	// This isnt necessary too but can make the benchmarking a lot faster
	
	for(uint64_t i = 0; i < benchmarks; i++) {
		bencher.runFunctionBenchmark(benchmarkMe);
		// Do a function benchmark
		
		bencher.startIndependentBenchmark();
		// Start a benchmark that can benchmark code without needing a function
		
		benchmarkMe();
		
		bencher.stopIndependentBenchmark();
		// stop the function independent benchmark
		
	}
	
	std::cout << "results:" << std::endl;
	std::cout << "benchmarks done:\t" << bencher.getNumberOfBenchmarks() << std::endl;
	std::cout << "avg. time:\t\t" << bencher.getAverageBenchmarkTime() << std::endl;
	std::cout << "avg. cycles:\t\t" << bencher.getAverageBenchmarkCycles() << std::endl;
	// the function calls should be self explaining
	
	return 0;
}

In this code are all functions in the library and are explained by the comments nearby them. But here's a list of all functions nevertheless:

  • benchmark() // make a benchmarker that can test the speed of your code
  • ~benchmark() // deconstruct the benchmarker
  • T getAverageBenchmarkTime() // get the average run times of your code from the benchmarks in seconds
  • T getAverageBenchmarkCycles() // get the average amount of cycles needed from the benchmarks
  • uint64_t getNumberOfBenchmarks() // get the amount of benchmarks already made
  • void setNumberOfEstimatedBenchmarks(uint64_t estimatedNumberOfBenchmarks) // set the number of estimated benchmarks that are going to be made. this can make the benchmarking a lot faster
  • void runFunctionBenchmark(void(*function)()) // benchmark a function
  • void startIndependentBenchmark() // start an independent benchmark that needs no function as argument
  • void stopIndependentBenchmark() // stop the independent benchmark
Clone this wiki locally