2016-08-01 07:29:34 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2016-08-02 08:07:09 -07:00
|
|
|
#pragma once
|
2016-08-01 07:29:34 -07:00
|
|
|
|
2016-08-12 06:17:36 -07:00
|
|
|
#include <math.h>
|
2016-08-02 08:06:55 -07:00
|
|
|
#include <stdint.h>
|
2016-08-15 09:15:02 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-08-02 08:06:55 -07:00
|
|
|
#include <time.h>
|
2016-08-01 07:29:34 -07:00
|
|
|
|
2016-10-25 16:11:37 -07:00
|
|
|
#define NUM_REPETITIONS 1000
|
2016-08-01 07:29:34 -07:00
|
|
|
|
2017-01-31 09:28:10 -08:00
|
|
|
#define YGBENCHMARKS(BLOCK) \
|
2016-08-15 09:15:02 -07:00
|
|
|
int main(int argc, char const *argv[]) { \
|
|
|
|
clock_t __start; \
|
|
|
|
clock_t __endTimes[NUM_REPETITIONS]; \
|
|
|
|
{ BLOCK } \
|
|
|
|
return 0; \
|
2016-08-04 08:20:11 -07:00
|
|
|
}
|
2016-08-01 07:29:34 -07:00
|
|
|
|
2017-01-31 09:28:10 -08:00
|
|
|
#define YGBENCHMARK(NAME, BLOCK) \
|
2016-08-15 09:15:02 -07:00
|
|
|
__start = clock(); \
|
|
|
|
for (uint32_t __i = 0; __i < NUM_REPETITIONS; __i++) { \
|
|
|
|
{ BLOCK } \
|
|
|
|
__endTimes[__i] = clock(); \
|
|
|
|
} \
|
2016-08-12 06:17:36 -07:00
|
|
|
__printBenchmarkResult(NAME, __start, __endTimes);
|
|
|
|
|
2016-08-15 09:15:02 -07:00
|
|
|
int __compareDoubles(const void *a, const void *b) {
|
|
|
|
double arg1 = *(const double *) a;
|
|
|
|
double arg2 = *(const double *) b;
|
2016-08-12 06:17:36 -07:00
|
|
|
|
|
|
|
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++) {
|
2016-08-15 09:15:02 -07:00
|
|
|
timesInMs[i] = (endTimes[i] - lastEnd) / (double) CLOCKS_PER_SEC * 1000;
|
2016-08-12 06:17:36 -07:00
|
|
|
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);
|
2016-08-01 07:29:34 -07:00
|
|
|
}
|