[libc] Show average runtime for math single-input-single-output performance tests.

Run performance tests in denormal and normal ranges separately and show more detailed results.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D116112
This commit is contained in:
Tue Ly 2021-12-21 10:31:42 -05:00
parent 3ce4b6a599
commit db8716a2ec

View file

@ -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<double>(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<uint64_t>(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<double>(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<uint64_t>(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);
}
};