calculate mu and sigma over repetitions not individual runs

This commit is contained in:
overflowerror 2024-01-18 11:40:57 +01:00
parent fd07b9b1f1
commit d350260d30
3 changed files with 19 additions and 14 deletions

View file

@ -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<double, std::milli> 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<double>(REPEATS) - 1)));
cout << "mu = " << mu << " ms, sigma = " << sigma << " ms\n";

View file

@ -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)

View file

@ -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"