Last active
April 30, 2023 09:11
-
-
Save alifgiant/42bb081a8be4873c96c09c8dd9bad9eb to your computer and use it in GitHub Desktop.
Testing Isolate Perf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:isolate'; | |
void main() async { | |
const jsonStr = '{' | |
'"name": "alif",' | |
'"age": 26,' | |
'"gender": "male",' | |
'"office": "work from home"' | |
'}'; | |
const testN = 100; | |
Future asyncRun<Q>(Function(Q) function, Q message) async { | |
function(message); | |
} | |
final stopwatchA = Stopwatch()..start(); | |
for (var i = 0; i < testN; i++) { | |
jsonDecode(jsonStr); | |
} | |
print('$testN sync run : ${stopwatchA.elapsed}'); | |
final stopwatchB = Stopwatch()..start(); | |
await Future.wait( | |
[for (var i = 0; i < testN; i++) asyncRun(jsonDecode, jsonStr)], | |
); | |
print('$testN async run : ${stopwatchB.elapsed}'); | |
final stopwatchC = Stopwatch()..start(); | |
await Future.wait( | |
[for (var i = 0; i < testN; i++) Isolate.spawn(jsonDecode, jsonStr)], | |
); | |
print('$testN isolate.spawn: ${stopwatchC.elapsed}'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, thank you for the response!