Skip to content

Instantly share code, notes, and snippets.

@dened
Last active August 5, 2022 07:33
Show Gist options
  • Save dened/03be8ae80a689cbd74149b98d4a17844 to your computer and use it in GitHub Desktop.
Save dened/03be8ae80a689cbd74149b98d4a17844 to your computer and use it in GitHub Desktop.
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