diff --git a/libc/test/src/math/differential_testing/SingleInputSingleOutputDiff.h b/libc/test/src/math/differential_testing/SingleInputSingleOutputDiff.h index 9a516f9069ce..7cc7059db03a 100644 --- a/libc/test/src/math/differential_testing/SingleInputSingleOutputDiff.h +++ b/libc/test/src/math/differential_testing/SingleInputSingleOutputDiff.h @@ -46,28 +46,56 @@ public: log << "Total number of differing results: " << diffCount << '\n'; } - static void runPerf(Func myFunc, Func otherFunc, const char *logFile) { - auto runner = [](Func func) { + static void runPerfInRange(Func myFunc, Func otherFunc, UIntType startingBit, + UIntType endingBit, + testutils::OutputFileStream &log) { + auto runner = [=](Func func) { volatile T result; - for (UIntType bits = 0;; ++bits) { + for (UIntType bits = startingBit;; ++bits) { T x = T(FPBits(bits)); result = func(x); - if (bits == UIntMax) + if (bits == endingBit) break; } }; - testutils::OutputFileStream log(logFile); Timer timer; timer.start(); runner(myFunc); timer.stop(); - log << " Run time of my function: " << timer.nanoseconds() << " ns \n"; + + UIntType numberOfRuns = endingBit - startingBit + 1; + double myAverage = static_cast(timer.nanoseconds()) / numberOfRuns; + log << "-- My function --\n"; + log << " Total time : " << timer.nanoseconds() << " ns \n"; + log << " Average runtime : " << myAverage << " ns/op \n"; + log << " Ops per second : " + << static_cast(1'000'000'000.0 / myAverage) << " op/s \n"; timer.start(); runner(otherFunc); timer.stop(); - log << "Run time of other function: " << timer.nanoseconds() << " ns \n"; + + double otherAverage = + static_cast(timer.nanoseconds()) / numberOfRuns; + log << "-- Other function --\n"; + log << " Total time : " << timer.nanoseconds() << " ns \n"; + log << " Average runtime : " << otherAverage << " ns/op \n"; + log << " Ops per second : " + << static_cast(1'000'000'000.0 / otherAverage) << " op/s \n"; + + log << "-- Average runtime ratio --\n"; + log << " Mine / Other's : " << myAverage / otherAverage << " \n"; + } + + static void runPerf(Func myFunc, Func otherFunc, const char *logFile) { + testutils::OutputFileStream log(logFile); + log << " Performance tests with inputs in denormal range:\n"; + runPerfInRange(myFunc, otherFunc, /* startingBit= */ UIntType(0), + /* endingBit= */ FPBits::MAX_SUBNORMAL, log); + log << "\n Performance tests with inputs in normal range:\n"; + runPerfInRange(myFunc, otherFunc, /* startingBit= */ FPBits::MIN_NORMAL, + /* endingBit= */ FPBits::MAX_NORMAL, log); } };