Last active
August 5, 2022 07:33
-
-
Save dened/03be8ae80a689cbd74149b98d4a17844 to your computer and use it in GitHub Desktop.
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(List<String> args) { | |
awaitSerial(); //🚶♂️ slow running | |
awaitParallel(); //🚴♂️ fast running | |
} | |
Future<int> getTotalClients() => Future.delayed(const Duration(seconds: 2), () => 1500000); | |
Future<int> getTotalUsers() => Future.delayed(const Duration(seconds: 2), () => 1500000); | |
Future<void> awaitSerial() async { | |
final startTime = Stopwatch()..start(); | |
final totalUsers = await getTotalUsers(); | |
final totalClients = await getTotalClients(); | |
startTime.stop(); | |
print( | |
'awaitSerial: ${startTime.elapsed.inSeconds}, total: ${totalUsers + totalClients}', | |
); | |
} | |
Future<void> awaitParallel() async { | |
final startTime = Stopwatch()..start(); | |
final getTotals = await Future.wait([ | |
getTotalClients(), | |
getTotalUsers(), | |
]); | |
startTime.stop(); | |
print( | |
'awaitParallel: ${startTime.elapsed.inSeconds}, total: ${getTotals.fold<int>(0, (previousValue, element) => previousValue + element)}', | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment