Last active
April 11, 2020 11:34
-
-
Save truongsinh/b338c692111983202ab4a0eaba611677 to your computer and use it in GitHub Desktop.
right-way-to-do-cpu-bound-operations.dart
This file contains hidden or 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
void main() async { | |
final then = DateTime.now(); | |
print(then); | |
final r = await compute(veryLongRunningCpuBoundFunction, 10); | |
final now = DateTime.now(); | |
print('${now.difference(then)} later, the result is $r'); | |
} | |
int veryLongRunningCpuBoundFunction(int param) { | |
for (var i = 0; i < param; i++) { | |
if (param > 0) veryLongRunningCpuBoundFunction(param - 1); | |
} | |
return 42; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment