diff --git a/c++/turing/benchmark.cpp b/c++/turing/benchmark.cpp index ce3b73b..75cb00f 100644 --- a/c++/turing/benchmark.cpp +++ b/c++/turing/benchmark.cpp @@ -41,14 +41,15 @@ int main() { double sum = 0; double sum_squared = 0; - for (int i = 0; i < CASES; i++) { + for (int r = 0; r < REPEATS; r++) { auto t1 = high_resolution_clock::now(); - bool result = test(inputs[i]); - auto t2 = high_resolution_clock::now(); - - if (result != results[i]) { - cout << i << ": " << inputs[i] << ", " << results[i] << " -> "<< "FAIL" << endl; + for (int i = 0; i < CASES; i++) { + bool result = test(inputs[i]); + if (result != results[i]) { + cout << i << ": " << inputs[i] << ", " << results[i] << " -> " << "FAIL" << endl; + } } + auto t2 = high_resolution_clock::now(); duration ms = t2 - t1; double tmp = ms.count(); @@ -57,10 +58,9 @@ int main() { sum_squared += tmp * tmp; } - double mu = sum / CASES; - + double mu = sum / REPEATS; // from https://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods - double sigma = sqrt(CASES * sum_squared - sum * sum) / CASES; + double sigma = sqrt((REPEATS * sum_squared - sum * sum) / (REPEATS * (static_cast(REPEATS) - 1))); cout << "mu = " << mu << " ms, sigma = " << sigma << " ms\n"; diff --git a/c++/turing/common.h b/c++/turing/common.h index 3261eaa..c77c94e 100644 --- a/c++/turing/common.h +++ b/c++/turing/common.h @@ -1,7 +1,11 @@ #ifndef OOP_BENCHMARK_COMMON_H #define OOP_BENCHMARK_COMMON_H -#define CASES (100000) +#ifndef REPEATS +#define REPEATS (1000) +#endif + +#define CASES (1000) #define MAX_VALUE (0xffff) #define MAX_BAND_SIZE (MAX_VALUE + 1) diff --git a/c++/turing/runAll.sh b/c++/turing/runAll.sh index 2399403..e3b49a6 100755 --- a/c++/turing/runAll.sh +++ b/c++/turing/runAll.sh @@ -3,15 +3,16 @@ run() { test="$1" optimization="$2" + repeats="$3" - clang++ -std=c++14 -O"$optimization" "benchmark.cpp" "$1.cpp" - echo "$test $optimization: " + clang++ -std=c++14 -DREPEATS="$repeats" -O"$optimization" "benchmark.cpp" "$1.cpp" + echo "$test $optimization (n = $repeats): " ./a.out } runOpt() { - run "$1" 0 - run "$1" 3 + run "$1" 0 1000 + run "$1" 3 20000 } runOpt "oop-with-virtual-methods"