6 #ifndef CHEBYSHEV_BENCHMARK_H
7 #define CHEBYSHEV_BENCHMARK_H
62 "name",
"averageRuntime",
"stdevRuntime",
"runsPerSecond"
92 const char** argv =
nullptr) {
96 for (
int i = 1; i < argc; ++i)
99 std::cout <<
"Starting benchmarks of the "
100 << moduleName <<
" module ..." << std::endl;
117 output::settings.quiet = settings.
quiet;
121 !output::settings.outputFiles.size() &&
128 std::vector<std::string> outputFiles;
136 std::cout <<
"Finished benchmarking " << settings.
moduleName <<
'\n';
161 template<
typename InputType,
typename Function>
162 inline long double runtime(Function func,
const std::vector<InputType>& input) {
164 if (input.size() == 0)
168 __volatile__
auto c = func(input[0]);
172 for (
unsigned int j = 0; j < input.size(); ++j)
186 template<
typename InputType =
double,
typename Function>
190 const std::vector<InputType>& input,
192 bool quiet =
false) {
198 long double averageRuntime;
201 long double sumSquares;
204 long double totalRuntime;
209 totalRuntime =
runtime(func, input);
210 averageRuntime = totalRuntime / input.size();
213 for (
unsigned int i = 1; i < runs; ++i) {
217 const long double currentRun =
runtime(func, input);
218 const long double currentAverage = currentRun / input.size();
219 totalRuntime += currentRun;
221 const long double tmp = averageRuntime;
222 averageRuntime = tmp + (currentAverage - tmp) / (i + 1);
223 sumSquares += (currentAverage - tmp)
224 * (currentAverage - averageRuntime);
236 res.iterations = input.size();
237 res.totalRuntime = totalRuntime;
238 res.averageRuntime = averageRuntime;
239 res.runsPerSecond = 1000.0 / res.averageRuntime;
244 res.stdevRuntime =
std::sqrt(sumSquares / (runs - 1));
260 template<
typename InputType =
double,
typename Function>
267 std::vector<InputType> input (opt.
iterations);
268 for (
unsigned int i = 0; i < opt.
iterations; ++i)
284 template<
typename InputType =
double,
typename Function>
291 bool quiet =
false) {
Structures for the benchmark module.
Timer class to measure elapsed time in milliseconds.
Definition: timer.h:18
#define CHEBYSHEV_BENCHMARK_ITER
Default number of benchmark iterations.
Definition: common.h:22
#define CHEBYSHEV_BENCHMARK_RUNS
Default number of benchmark runs.
Definition: common.h:27
Input generators for benchmarks.
auto uniform1D(long double a, long double b)
Uniform generator over a domain.
Definition: generator.h:21
long double runtime(Function func, const std::vector< InputType > &input)
Measure the total runtime of a function over the given input for a single run.
Definition: benchmark.h:162
void benchmark(const std::string &name, Function func, const std::vector< InputType > &input, unsigned int runs=settings.defaultRuns, bool quiet=false)
Run a benchmark on a generic function, with the given input vector.
Definition: benchmark.h:187
std::function< InputType(unsigned int)> InputGenerator
A function which takes in an index and returns a generated input element.
Definition: benchmark_structures.h:62
void setup(std::string moduleName, int argc=0, const char **argv=nullptr)
Setup the benchmark environment.
Definition: benchmark.h:89
void benchmark(const std::string &name, Function func, unsigned int runs=settings.defaultRuns, unsigned int iterations=settings.defaultIterations, InputGenerator< InputType > inputGenerator=generator::uniform1D(0, 1), bool quiet=false)
Run a benchmark on a generic function, with the given argument options.
Definition: benchmark.h:285
void terminate(bool exit=true)
Terminate the benchmarking environment.
Definition: benchmark.h:115
void print_results(const std::map< std::string, std::vector< ResultType >> &results, const std::vector< std::string > &fields, const std::vector< std::string > &filenames)
Print the test results to standard output and output files with their given formats,...
Definition: output.h:907
void setup()
Setup printing to the output stream with default options.
Definition: output.h:539
void terminate()
Terminate the output module by closing all output files and resetting its settings.
Definition: output.h:598
std::string string(size_t length)
Generate a random string made of human-readable ASCII characters.
Definition: random.h:102
void setup(uint64_t seed=0)
Initialize the random module.
Definition: random.h:32
General namespace of the framework.
Definition: benchmark_structures.h:16
Vector sqrt(const Vector &v)
Parallel element-wise evaluation of the sqrt function.
Definition: parallel.h:143
The pseudorandom number generation and sampling module.
A structure holding the options of a benchmark.
Definition: benchmark_structures.h:68
unsigned int iterations
Number of iterations.
Definition: benchmark_structures.h:74
bool quiet
Whether to print to standard output or not.
Definition: benchmark_structures.h:80
unsigned int runs
Number of runs (run with the same input values).
Definition: benchmark_structures.h:71
InputGenerator< InputType > inputGenerator
The function to use to generate input for the benchmark.
Definition: benchmark_structures.h:77
Structure holding the results of a benchmark.
Definition: benchmark_structures.h:23
std::string name
Identifying name of the function or test case.
Definition: benchmark_structures.h:26
benchmarks.
Definition: benchmark.h:69
std::map< std::string, std::vector< benchmark_result > > benchmarkResults
Results of the benchmarks.
Definition: benchmark.h:78
unsigned int failedBenchmarks
Number of failed benchmarks.
Definition: benchmark.h:75
unsigned int totalBenchmarks
Total number of benchmarks.
Definition: benchmark.h:72
of the benchmark module
Definition: benchmark.h:31
std::vector< std::string > benchmarkOutputFiles
The files to write benchmark results to (if empty, all results are output to a generic file).
Definition: benchmark.h:58
unsigned int defaultRuns
Default number of runs.
Definition: benchmark.h:44
std::map< std::string, bool > pickedBenchmarks
Target benchmarks marked for execution (all benchmarks will be executed if empty)
Definition: benchmark.h:54
std::vector< std::string > benchmarkColumns
Default columns to print for benchmarks.
Definition: benchmark.h:61
std::string moduleName
Name of the module currently being benchmarked.
Definition: benchmark.h:38
bool outputToFile
Whether to output results to a file.
Definition: benchmark.h:47
unsigned int defaultIterations
Default number of iterations.
Definition: benchmark.h:41
std::vector< std::string > outputFiles
The files to write all benchmark results to.
Definition: benchmark.h:50
bool quiet
Whether to print benchmark results to standard output.
Definition: benchmark.h:35
A timer class to measure elapsed time in milliseconds.