From 765bb85d1e33e7ba35159fb38bd89fe0b343d71d Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Wed, 11 Jul 2018 08:51:30 -0700 Subject: [PATCH] Fix warnings when building benchmark Summary: There were a few missing prototypes and a -Wshadow violation. Merged YGBenchmark.h into YGBenchmark.c, added static where needed, fixed shadow violation. Reviewed By: davidaurelio Differential Revision: D8793124 fbshipit-source-id: c4b2dd348c38aa599169b5e9bea543c172439432 --- benchmark/YGBenchmark.c | 66 ++++++++++++++++++++++++++++++++++++-- benchmark/YGBenchmark.h | 71 ----------------------------------------- 2 files changed, 64 insertions(+), 73 deletions(-) delete mode 100644 benchmark/YGBenchmark.h diff --git a/benchmark/YGBenchmark.c b/benchmark/YGBenchmark.c index 1c5521ae..c06ca276 100644 --- a/benchmark/YGBenchmark.c +++ b/benchmark/YGBenchmark.c @@ -5,10 +5,72 @@ * LICENSE file in the root directory of this source tree. */ -#include "YGBenchmark.h" +#include +#include +#include +#include +#include #include +#define NUM_REPETITIONS 1000 + +#define YGBENCHMARKS(BLOCK) \ + int main(int argc, char const *argv[]) { \ + clock_t __start; \ + clock_t __endTimes[NUM_REPETITIONS]; \ + { BLOCK } \ + return 0; \ + } + +#define YGBENCHMARK(NAME, BLOCK) \ + __start = clock(); \ + for (uint32_t __i = 0; __i < NUM_REPETITIONS; __i++) { \ + { BLOCK } \ + __endTimes[__i] = clock(); \ + } \ + __printBenchmarkResult(NAME, __start, __endTimes); + +static int __compareDoubles(const void *a, const void *b) { + double arg1 = *(const double *) a; + double arg2 = *(const double *) b; + + if (arg1 < arg2) { + return -1; + } + + if (arg1 > arg2) { + return 1; + } + + return 0; +} + +static 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); +} + + static YGSize _measure(YGNodeRef node, float width, YGMeasureMode widthMode, @@ -97,7 +159,7 @@ YGBENCHMARKS({ YGNodeStyleSetHeight(grandGrandChild, 10); YGNodeInsertChild(grandChild, grandGrandChild, 0); - for (uint32_t iii = 0; iii < 10; iii++) { + for (uint32_t iiii = 0; iiii < 10; iiii++) { const YGNodeRef grandGrandGrandChild = YGNodeNew(); YGNodeStyleSetFlexDirection(grandGrandGrandChild, YGFlexDirectionRow); YGNodeStyleSetFlexGrow(grandGrandGrandChild, 1); diff --git a/benchmark/YGBenchmark.h b/benchmark/YGBenchmark.h deleted file mode 100644 index 54a625b7..00000000 --- a/benchmark/YGBenchmark.h +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include -#include -#include -#include - -#define NUM_REPETITIONS 1000 - -#define YGBENCHMARKS(BLOCK) \ - int main(int argc, char const *argv[]) { \ - clock_t __start; \ - clock_t __endTimes[NUM_REPETITIONS]; \ - { BLOCK } \ - return 0; \ - } - -#define YGBENCHMARK(NAME, BLOCK) \ - __start = clock(); \ - for (uint32_t __i = 0; __i < NUM_REPETITIONS; __i++) { \ - { BLOCK } \ - __endTimes[__i] = clock(); \ - } \ - __printBenchmarkResult(NAME, __start, __endTimes); - -int __compareDoubles(const void *a, const void *b) { - double arg1 = *(const double *) a; - double arg2 = *(const double *) b; - - 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); -}