Last active
June 10, 2019 14:20
-
-
Save truongsinh/7d19852a120ba1f89daf02fd51a72830 to your computer and use it in GitHub Desktop.
naive-future-for-cpu-bound-operation.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() { | |
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 |
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 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