Skip to content

Commit be3ac73

Browse files
committed
Correctly run benchmark
1 parent 8fdfdd3 commit be3ac73

File tree

4 files changed

+52
-50
lines changed

4 files changed

+52
-50
lines changed

Diff for: example/globals.d.ts

-7
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,3 @@ declare var _LABEL: string | undefined;
1212
* Sets the global console object
1313
*/
1414
declare var _setGlobalConsole: (console: any) => void;
15-
16-
declare var performance: { now: () => number };
17-
18-
/**
19-
* Logs to the native console window (Xcode logs/Android logcat)
20-
*/
21-
declare var _log: (message: string) => void | undefined;

Diff for: example/src/App.tsx

+50-41
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import {
1010
Button,
1111
} from 'react-native';
1212
import { spawnThread } from 'react-native-multithreading';
13+
import 'react-native-reanimated';
1314
import { runOnJS, runOnUI } from 'react-native-reanimated';
1415

16+
// calculates the fibonacci number - that can be optimized really good so it's really really fast.
1517
const fibonacci = (num: number): number => {
1618
'worklet';
1719
// Uses array to store every single fibonacci number
@@ -26,13 +28,40 @@ const fibonacci = (num: number): number => {
2628
return fib[fib.length - 1];
2729
};
2830

31+
// complex branches with modular if so that it cannot be optimized very well. Takes ~4-5 seconds on my i9.
32+
const benchmark = () => {
33+
'worklet';
34+
// global.performance is not yet installed. I will do that soon.
35+
const start = performance.now();
36+
function p(n: number) {
37+
for (var i = 2; i * i <= n; i++) {
38+
if (n % i === 0) {
39+
return false;
40+
}
41+
}
42+
return true;
43+
}
44+
45+
var sum = 0;
46+
for (var k = 2; k < 1000000; k++) {
47+
if (p(k)) {
48+
sum++;
49+
}
50+
}
51+
const end = performance.now();
52+
return {
53+
result: sum,
54+
duration: end - start,
55+
};
56+
};
57+
2958
export default function App() {
3059
const [isBenchmarking, setIsBenchmarking] = React.useState(false);
3160
const [isRunning, setIsRunning] = React.useState(false);
3261
const [input, setInput] = React.useState('5');
3362
const [result, setResult] = React.useState<number | undefined>();
3463

35-
const run = React.useCallback(async (parsedInput: number) => {
64+
const runFibonacci = React.useCallback(async (parsedInput: number) => {
3665
setIsRunning(true);
3766
try {
3867
const fib = await spawnThread(() => {
@@ -55,38 +84,22 @@ export default function App() {
5584
}
5685
}, []);
5786

87+
React.useEffect(() => {
88+
const parsedInput = Number.parseInt(input, 10);
89+
runFibonacci(parsedInput);
90+
}, [runFibonacci, input]);
91+
5892
const runBenchmark = React.useCallback(
5993
async (thread: 'react' | 'custom' | 'ui') => {
60-
const benchmark = () => {
61-
'worklet';
62-
const start = performance.now();
63-
function p(n: number) {
64-
for (var i = 2; i * i <= n; i++) {
65-
if (n % i === 0) {
66-
return false;
67-
}
68-
}
69-
return true;
70-
}
71-
72-
var sum = 0;
73-
for (var k = 2; k < 1000000; k++) {
74-
if (p(k)) {
75-
sum++;
76-
}
77-
}
78-
const end = performance.now();
79-
return {
80-
result: sum,
81-
duration: end - start,
82-
};
83-
};
8494
setIsBenchmarking(true);
8595
switch (thread) {
8696
case 'react': {
8797
for (let i = 0; i < 5; i++) {
8898
const r = benchmark();
89-
console.log(`REACT: Run #${i}: ${r.result} (took ${r.duration}ms)`);
99+
global.nativeLoggingHook(
100+
`REACT: Run #${i}: ${r.result} (took ${r.duration}ms)`,
101+
1
102+
);
90103
}
91104
setIsBenchmarking(false);
92105
break;
@@ -97,7 +110,7 @@ export default function App() {
97110
for (let i = 0; i < 5; i++) {
98111
const r = benchmark();
99112
// can't use console.log because that just dispatches to React-JS thread, which might be blocked.
100-
console.log(
113+
global._log(
101114
`CUSTOM: Run #${i}: ${r.result} (took ${r.duration}ms)`
102115
);
103116
}
@@ -106,27 +119,23 @@ export default function App() {
106119
break;
107120
}
108121
case 'ui': {
109-
runOnUI(() => {
110-
'worklet';
111-
for (let i = 0; i < 5; i++) {
112-
const r = benchmark();
113-
// can't use console.log because that just dispatches to React-JS thread, which might be blocked.
114-
global._log(`UI: Run #${i}: ${r.result} (took ${r.duration}ms)`);
115-
}
116-
runOnJS(setIsBenchmarking)(false);
117-
})();
122+
// couldn't manage to get this working, some weird undefined errors
123+
// runOnUI(() => {
124+
// 'worklet';
125+
// for (let i = 0; i < 5; i++) {
126+
// const r = benchmark();
127+
// // can't use console.log because that just dispatches to React-JS thread, which might be blocked.
128+
// global._log(`UI: Run #${i}: ${r.result} (took ${r.duration}ms)`);
129+
// }
130+
// runOnJS(setIsBenchmarking)(false);
131+
// })();
118132
break;
119133
}
120134
}
121135
},
122136
[]
123137
);
124138

125-
React.useEffect(() => {
126-
const parsedInput = Number.parseInt(input, 10);
127-
run(parsedInput);
128-
}, [run, input]);
129-
130139
return (
131140
<View style={styles.container}>
132141
<Text>Input:</Text>

Diff for: package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@
9999
"root": true,
100100
"globals": {
101101
"_setGlobalConsole": true,
102-
"_LABEL": true,
103-
"performance": true
102+
"_LABEL": true
104103
},
105104
"extends": [
106105
"@react-native-community",

Diff for: src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'react-native-reanimated';
12
import { runOnJS } from 'react-native-reanimated';
23

34
const g = global as any;

0 commit comments

Comments
 (0)