Skip to content

Instantly share code, notes, and snippets.

@truongsinh
Last active June 10, 2019 14:20
Show Gist options
  • Save truongsinh/7d19852a120ba1f89daf02fd51a72830 to your computer and use it in GitHub Desktop.
Save truongsinh/7d19852a120ba1f89daf02fd51a72830 to your computer and use it in GitHub Desktop.
naive-future-for-cpu-bound-operation.dart
void main() {
final then = DateTime.now();
print(then);
final r = veryLongRunningCpuBoundFunction(9);
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;
}
// takes around 0.009391 seconds on Macbook Pro 2013
void main() async {
final then = DateTime.now();
print(then);
final r = await veryLongRunningCpuBoundFunction(9);
final now = DateTime.now();
print('${now.difference(then)} later, the result is $r');
}
Future<int> veryLongRunningCpuBoundFunction(int param) async {
for (var i = 0; i < param; i++) {
if (param > 0) await veryLongRunningCpuBoundFunction(param - 1);
}
return 42;
}
// takes around 1.716475 seconds on Macbook Pro 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment