Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary await Future.microtask(() #367

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions bench/algorithm/coro-prime-sieve/1.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
// Based on a version by hanabi1224 and kevmoo
// Modified by Christian Kleineidam

import 'dart:async';

Future<void> main(List<String> arguments) async {
final n = arguments.isNotEmpty ? int.parse(arguments[0]) : 5;
var stream = StreamIterator(generate());
for (var i = 0; i < n; i++) {
final n = arguments.isNotEmpty ? int.parse(arguments[0]) : 20;
StreamIterator<int> stream = StreamIterator(generate());
for (int i = 0; i < n; i++) {
await stream.moveNext();
final prime = stream.current;
print(prime);
stream = StreamIterator(filter(stream, prime));
print(stream.current);
stream = StreamIterator(filter(stream, stream.current));
}
}

Stream<int> generate() async* {
for (var i = 2;; i++) {
yield await Future.microtask(() => i);
}
for (int i = 2;; i++) {yield i;}
}

Stream<int> filter(StreamIterator<int> input, int prime) async* {
while (await input.moveNext()) {
final i = input.current;
if (i % prime != 0) {
yield await Future.microtask(() => i);
if (input.current % prime != 0) {
yield input.current;
}
}
}