Skip to content

Instantly share code, notes, and snippets.

@darwin-morocho
Created January 26, 2023 16:09
Show Gist options
  • Save darwin-morocho/2bbd2d4493bfec1d306ab5721e649056 to your computer and use it in GitHub Desktop.
Save darwin-morocho/2bbd2d4493bfec1d306ab5721e649056 to your computer and use it in GitHub Desktop.
flying-aurora-8609
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