Skip to content

Instantly share code, notes, and snippets.

@truongsinh
Last active April 11, 2020 11:34
Show Gist options
  • Save truongsinh/b338c692111983202ab4a0eaba611677 to your computer and use it in GitHub Desktop.
Save truongsinh/b338c692111983202ab4a0eaba611677 to your computer and use it in GitHub Desktop.
right-way-to-do-cpu-bound-operations.dart
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