2019-06-06 19:36:56 -07:00
|
|
|
/*
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-08-01 07:29:34 -07:00
|
|
|
*
|
2019-10-15 10:30:08 -07:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-08-01 07:29:34 -07:00
|
|
|
*/
|
2019-10-15 10:30:08 -07:00
|
|
|
|
2018-07-11 08:51:30 -07:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2016-08-01 07:29:34 -07:00
|
|
|
|
2016-12-07 05:12:11 -08:00
|
|
|
#include <yoga/Yoga.h>
|
2016-08-01 07:29:34 -07:00
|
|
|
|
2018-07-11 08:51:30 -07:00
|
|
|
#define NUM_REPETITIONS 1000
|
|
|
|
|
|
|
|
#define YGBENCHMARKS(BLOCK) \
|
2019-01-08 12:47:53 -08:00
|
|
|
int main(int argc, char const* argv[]) { \
|
2018-07-11 08:51:30 -07:00
|
|
|
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++) { \
|
2019-01-08 12:47:53 -08:00
|
|
|
{BLOCK} __endTimes[__i] = clock(); \
|
2018-07-11 08:51:30 -07:00
|
|
|
} \
|
|
|
|
__printBenchmarkResult(NAME, __start, __endTimes);
|
|
|
|
|
2019-01-08 12:47:53 -08:00
|
|
|
static int __compareDoubles(const void* a, const void* b) {
|
|
|
|
double arg1 = *(const double*) a;
|
|
|
|
double arg2 = *(const double*) b;
|
2018-07-11 08:51:30 -07:00
|
|
|
|
|
|
|
if (arg1 < arg2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg1 > arg2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-08 12:47:53 -08:00
|
|
|
static void __printBenchmarkResult(
|
|
|
|
char* name,
|
|
|
|
clock_t start,
|
|
|
|
clock_t* endTimes) {
|
2018-07-11 08:51:30 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-01-08 12:47:53 -08:00
|
|
|
static YGSize _measure(
|
|
|
|
YGNodeRef node,
|
|
|
|
float width,
|
|
|
|
YGMeasureMode widthMode,
|
|
|
|
float height,
|
|
|
|
YGMeasureMode heightMode) {
|
2016-12-03 04:40:18 -08:00
|
|
|
return (YGSize){
|
2016-12-02 05:47:43 -08:00
|
|
|
.width = widthMode == YGMeasureModeUndefined ? 10 : width,
|
|
|
|
.height = heightMode == YGMeasureModeUndefined ? 10 : width,
|
2016-08-11 02:23:52 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-02 11:18:12 -08:00
|
|
|
YGBENCHMARKS({
|
|
|
|
YGBENCHMARK("Stack with flex", {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef root = YGNodeNew();
|
|
|
|
YGNodeStyleSetWidth(root, 100);
|
|
|
|
YGNodeStyleSetHeight(root, 100);
|
2016-08-01 07:29:34 -07:00
|
|
|
|
2016-08-11 02:23:52 -07:00
|
|
|
for (uint32_t i = 0; i < 10; i++) {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef child = YGNodeNew();
|
|
|
|
YGNodeSetMeasureFunc(child, _measure);
|
|
|
|
YGNodeStyleSetFlex(child, 1);
|
|
|
|
YGNodeInsertChild(root, child, 0);
|
2016-08-11 02:23:52 -07:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
|
|
|
YGNodeFreeRecursive(root);
|
2016-08-11 02:23:52 -07:00
|
|
|
});
|
|
|
|
|
2016-12-02 11:18:12 -08:00
|
|
|
YGBENCHMARK("Align stretch in undefined axis", {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef root = YGNodeNew();
|
2016-08-11 02:23:52 -07:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 10; i++) {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef child = YGNodeNew();
|
|
|
|
YGNodeStyleSetHeight(child, 20);
|
|
|
|
YGNodeSetMeasureFunc(child, _measure);
|
|
|
|
YGNodeInsertChild(root, child, 0);
|
2016-08-11 02:23:52 -07:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
|
|
|
YGNodeFreeRecursive(root);
|
2016-08-11 02:23:52 -07:00
|
|
|
});
|
|
|
|
|
2016-12-02 11:18:12 -08:00
|
|
|
YGBENCHMARK("Nested flex", {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef root = YGNodeNew();
|
2016-08-11 02:23:52 -07:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 10; i++) {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef child = YGNodeNew();
|
|
|
|
YGNodeStyleSetFlex(child, 1);
|
|
|
|
YGNodeInsertChild(root, child, 0);
|
2016-08-11 02:23:52 -07:00
|
|
|
|
|
|
|
for (uint32_t ii = 0; ii < 10; ii++) {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef grandChild = YGNodeNew();
|
|
|
|
YGNodeSetMeasureFunc(grandChild, _measure);
|
|
|
|
YGNodeStyleSetFlex(grandChild, 1);
|
|
|
|
YGNodeInsertChild(child, grandChild, 0);
|
2016-08-11 02:23:52 -07:00
|
|
|
}
|
2016-08-01 07:29:34 -07:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
|
|
|
YGNodeFreeRecursive(root);
|
2016-08-01 07:29:34 -07:00
|
|
|
});
|
|
|
|
|
2016-12-02 11:18:12 -08:00
|
|
|
YGBENCHMARK("Huge nested layout", {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef root = YGNodeNew();
|
2016-10-31 10:31:52 -07:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 10; i++) {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef child = YGNodeNew();
|
|
|
|
YGNodeStyleSetFlexGrow(child, 1);
|
|
|
|
YGNodeStyleSetWidth(child, 10);
|
|
|
|
YGNodeStyleSetHeight(child, 10);
|
|
|
|
YGNodeInsertChild(root, child, 0);
|
2016-10-31 10:31:52 -07:00
|
|
|
|
|
|
|
for (uint32_t ii = 0; ii < 10; ii++) {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef grandChild = YGNodeNew();
|
|
|
|
YGNodeStyleSetFlexDirection(grandChild, YGFlexDirectionRow);
|
|
|
|
YGNodeStyleSetFlexGrow(grandChild, 1);
|
|
|
|
YGNodeStyleSetWidth(grandChild, 10);
|
|
|
|
YGNodeStyleSetHeight(grandChild, 10);
|
|
|
|
YGNodeInsertChild(child, grandChild, 0);
|
2016-10-31 10:31:52 -07:00
|
|
|
|
|
|
|
for (uint32_t iii = 0; iii < 10; iii++) {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef grandGrandChild = YGNodeNew();
|
|
|
|
YGNodeStyleSetFlexGrow(grandGrandChild, 1);
|
|
|
|
YGNodeStyleSetWidth(grandGrandChild, 10);
|
|
|
|
YGNodeStyleSetHeight(grandGrandChild, 10);
|
|
|
|
YGNodeInsertChild(grandChild, grandGrandChild, 0);
|
2016-10-31 10:31:52 -07:00
|
|
|
|
2018-07-11 08:51:30 -07:00
|
|
|
for (uint32_t iiii = 0; iiii < 10; iiii++) {
|
2016-12-03 04:40:18 -08:00
|
|
|
const YGNodeRef grandGrandGrandChild = YGNodeNew();
|
2019-01-08 12:47:53 -08:00
|
|
|
YGNodeStyleSetFlexDirection(
|
|
|
|
grandGrandGrandChild, YGFlexDirectionRow);
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeStyleSetFlexGrow(grandGrandGrandChild, 1);
|
|
|
|
YGNodeStyleSetWidth(grandGrandGrandChild, 10);
|
|
|
|
YGNodeStyleSetHeight(grandGrandGrandChild, 10);
|
|
|
|
YGNodeInsertChild(grandGrandChild, grandGrandGrandChild, 0);
|
2016-10-31 10:31:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 04:40:18 -08:00
|
|
|
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
|
|
|
|
YGNodeFreeRecursive(root);
|
2016-10-31 10:31:52 -07:00
|
|
|
});
|
2016-08-01 07:29:34 -07:00
|
|
|
});
|