Skip to content

Commit e1a0d99

Browse files
committed
add compute
1 parent bef63a6 commit e1a0d99

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
# Flutter Multithreading
1+
# Flutter Multithreading
2+
https://habr.com/ru/post/532862/

lib/notifier.dart

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import 'dart:isolate';
22

3+
import 'package:flutter/foundation.dart';
34
import 'package:flutter/material.dart';
45
import 'package:flutter_riverpod/flutter_riverpod.dart';
56

67
enum TestType { createNumbersArray, calculateFactorial }
78

89
class TestResult {
910
final Duration mainThreadExecutionTime;
10-
final Duration anotherThreadExecutionTime;
11+
final Duration isolateExecutionTime;
12+
final Duration computeExecutionTime;
1113
final TestType testType;
1214

1315
TestResult({
1416
required this.testType,
17+
required this.computeExecutionTime,
1518
required this.mainThreadExecutionTime,
16-
required this.anotherThreadExecutionTime,
19+
required this.isolateExecutionTime,
1720
});
1821
}
1922

@@ -27,33 +30,40 @@ class TestsController extends ChangeNotifier {
2730
isTestRunning = true;
2831
notifyListeners();
2932

30-
late final Future<void> Function() test;
33+
late final Future<void> Function(int number) test;
34+
late final int testNumber;
3135

3236
switch (testType) {
3337
case TestType.createNumbersArray:
34-
test = () => createNumbersArray(10000);
38+
test = createNumbersArray;
39+
testNumber = 10000;
3540
break;
3641
case TestType.calculateFactorial:
37-
test = () => calculateFactorial(50);
42+
test = calculateFactorial;
43+
testNumber = 50;
3844
break;
3945
}
4046

41-
final anotherThreadExecutionTime = await measureExecutionTime(
42-
() => workInBackground(
47+
final isolateExecutionTime = await measureExecutionTime(
48+
() => workInIsolate(
4349
(port) async {
44-
await test();
50+
await test(testNumber);
4551
Isolate.exit(port, 'canceled');
4652
},
4753
),
4854
);
4955
final mainThreadExecutionTime = await measureExecutionTime(() async {
50-
await test();
56+
await test(testNumber);
57+
});
58+
final computeExecutionTime = await measureExecutionTime(() async {
59+
await compute<int, void>(test, testNumber);
5160
});
5261

5362
isTestRunning = false;
5463
results.add(
5564
TestResult(
56-
anotherThreadExecutionTime: anotherThreadExecutionTime,
65+
computeExecutionTime: computeExecutionTime,
66+
isolateExecutionTime: isolateExecutionTime,
5767
mainThreadExecutionTime: mainThreadExecutionTime,
5868
testType: TestType.createNumbersArray,
5969
),
@@ -77,7 +87,7 @@ Future<Duration> measureExecutionTime(
7787
return stopwatch.elapsed;
7888
}
7989

80-
Future<void> workInBackground(void Function(SendPort) test) async {
90+
Future<void> workInIsolate(void Function(SendPort) test) async {
8191
final port = ReceivePort('test');
8292
await Isolate.spawn(test, port.sendPort);
8393
await port.first;

lib/test_widget.dart

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TestWidget extends StatelessWidget {
1717
const SizedBox(height: 10),
1818
Text(
1919
'In Isolate: ' +
20-
result.anotherThreadExecutionTime.inMilliseconds.toString() +
20+
result.isolateExecutionTime.inMilliseconds.toString() +
2121
' milliseconds',
2222
),
2323
const SizedBox(height: 2),
@@ -26,6 +26,12 @@ class TestWidget extends StatelessWidget {
2626
result.mainThreadExecutionTime.inMilliseconds.toString() +
2727
' milliseconds',
2828
),
29+
const SizedBox(height: 2),
30+
Text(
31+
'In Compute: ' +
32+
result.computeExecutionTime.inMilliseconds.toString() +
33+
' milliseconds',
34+
),
2935
],
3036
);
3137
}

0 commit comments

Comments
 (0)