Print more detailed benchmark results including median and stddev

Summary: Mean was previously printed just because it was the quickest. Median is a better measurement and we need to include stddev for the sake of comparison against previous diff.

Differential Revision: D3709165

fbshipit-source-id: 67aff0877192143df82a9c24cbedb1f49616eec7
This commit is contained in:
Emil Sjolander
2016-08-12 06:17:36 -07:00
committed by Facebook Github Bot 3
parent e307dc22d1
commit d125911894

View File

@@ -9,6 +9,7 @@
#pragma once #pragma once
#include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
@@ -19,7 +20,7 @@
#define CSS_BENCHMARKS(BLOCK) \ #define CSS_BENCHMARKS(BLOCK) \
int main(int argc, char const *argv[]) { \ int main(int argc, char const *argv[]) { \
clock_t __start; \ clock_t __start; \
clock_t __end; \ clock_t __endTimes[NUM_REPETITIONS]; \
{ BLOCK } \ { BLOCK } \
return 0; \ return 0; \
} }
@@ -27,15 +28,47 @@
#define CSS_BENCHMARK(NAME, BLOCK) \ #define CSS_BENCHMARK(NAME, BLOCK) \
__start = clock(); \ __start = clock(); \
for (uint32_t __i = 0; __i < NUM_REPETITIONS; __i++) { \ for (uint32_t __i = 0; __i < NUM_REPETITIONS; __i++) { \
BLOCK \ { BLOCK } \
__endTimes[__i] = clock(); \
} \ } \
__end = clock(); \ __printBenchmarkResult(NAME, __start, __endTimes);
__printBenchmarkResult(NAME, __start, __end);
void __printBenchmarkResult(char *name, clock_t start, clock_t end) {
float total = (end - start) / (double)CLOCKS_PER_SEC * 1000; int __compareDoubles(const void* a, const void* b) {
float mean = total / NUM_REPETITIONS; double arg1 = *(const double*)a;
printf("%s: ", name); double arg2 = *(const double*)b;
printf("%lf ms", mean);
printf("\n"); if (arg1 < arg2) {
return -1;
}
if (arg1 > arg2) {
return 1;
}
return 0;
}
void __printBenchmarkResult(char *name, clock_t start, clock_t *endTimes) {
double timesInMs[NUM_REPETITIONS];
double mean = 0;
clock_t lastEnd = start;
for (uint32_t i = 0; i < NUM_REPETITIONS; i++) {
timesInMs[i] = (endTimes[i] - lastEnd) / (double)CLOCKS_PER_SEC * 1000;
lastEnd = endTimes[i];
mean += timesInMs[i];
}
mean /= NUM_REPETITIONS;
qsort(timesInMs, NUM_REPETITIONS, sizeof(double), __compareDoubles);
double median = timesInMs[NUM_REPETITIONS / 2];
double variance = 0;
for (uint32_t i = 0; i < NUM_REPETITIONS; i++) {
variance += pow(timesInMs[i] - mean, 2);
}
variance /= NUM_REPETITIONS;
double stddev = sqrt(variance);
printf("%s: median: %lf ms, stddev: %lf ms\n", name, median, stddev);
} }