Skip to content

Instantly share code, notes, and snippets.

@JunJaBoy
Created May 6, 2024 07:41
Show Gist options
  • Save JunJaBoy/bdafa5fe28fa038fe0bdf38ead590062 to your computer and use it in GitHub Desktop.
Save JunJaBoy/bdafa5fe28fa038fe0bdf38ead590062 to your computer and use it in GitHub Desktop.
void withIf() {
Map<int, void> items = {
1: (),
2: (),
3: (),
4: (),
5: (),
};
items.forEach(
(key, value) {
if (key % 2 == 1) {
odd(key);
}
if (key % 2 == 0) {
even(key);
}
},
);
}
void withPair() {
Map<int, Function> items = {
1: () => odd(1),
2: () => even(2),
3: () => odd(3),
4: () => even(4),
5: () => odd(5),
};
items.forEach((key, value) => value());
}
void odd(int value) {
print("ODD: $value");
}
void even(int value) {
print("even: $value");
}
main() {
print("IF===============");
Stopwatch ifWatch = Stopwatch();
ifWatch.start();
withIf();
ifWatch.stop();
print("IF ${ifWatch.elapsedMicroseconds}");
print("PAIR===============");
Stopwatch pairWatch = Stopwatch();
pairWatch.start();
withPair();
pairWatch.stop();
print("PAIR ${pairWatch.elapsedMicroseconds}");
}
// Execution result:
// IF===============
// ODD: 1
// even: 2
// ODD: 3
// even: 4
// ODD: 5
// IF 676
// PAIR===============
// ODD: 1
// even: 2
// ODD: 3
// even: 4
// ODD: 5
// PAIR 149
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment