Created with <3 with dartpad.dev.
Created
January 26, 2023 16:09
-
-
Save darwin-morocho/2bbd2d4493bfec1d306ab5721e649056 to your computer and use it in GitHub Desktop.
flying-aurora-8609
This file contains 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 list = List.generate(10000, (index) => index); | |
imperative(list); | |
declarative(list); | |
} | |
void imperative(List<int> list) { | |
final start = DateTime.now(); | |
int result = 0; | |
for (final e in list) { | |
if (e % 2 == 0) { | |
result += e; | |
} | |
} | |
final end = DateTime.now(); | |
print('IMPERATIVE'); | |
print('result: $result'); | |
print('microseconds ${end.difference(start).inMicroseconds}'); | |
} | |
void declarative(List<int> list) { | |
final start = DateTime.now(); | |
int result = list | |
.where( | |
(e) => e % 2 == 0, | |
) | |
.reduce( | |
(a, b) => a + b, | |
); | |
final end = DateTime.now(); | |
print('\nDECLARATIVE'); | |
print('result: $result'); | |
print('microseconds ${end.difference(start).inMicroseconds}'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment