From fd07b9b1f197c18fa9f8d08f7825dba252c80bb6 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Thu, 18 Jan 2024 00:13:27 +0100 Subject: [PATCH] change benchmark to calculate deviation on the fly --- c++/turing/benchmark.cpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/c++/turing/benchmark.cpp b/c++/turing/benchmark.cpp index ff41cc3..ce3b73b 100644 --- a/c++/turing/benchmark.cpp +++ b/c++/turing/benchmark.cpp @@ -38,15 +38,30 @@ void resetTestData() { int main() { resetTestData(); - auto t1 = high_resolution_clock::now(); + double sum = 0; + double sum_squared = 0; + for (int i = 0; i < CASES; i++) { - if (test(inputs[i]) != results[i]) { + 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; } - } - auto t2 = high_resolution_clock::now(); - duration ms = t2 - t1; - cout << ms.count() << " ms, " << (ms.count() / CASES) << " ms/case" << endl; + duration ms = t2 - t1; + double tmp = ms.count(); + + sum += tmp; + sum_squared += tmp * tmp; + } + + double mu = sum / CASES; + + // from https://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods + double sigma = sqrt(CASES * sum_squared - sum * sum) / CASES; + + cout << "mu = " << mu << " ms, sigma = " << sigma << " ms\n"; } \ No newline at end of file