|
| 1 | +#![feature(async_closure)] |
| 2 | + |
| 3 | +#[macro_use] |
| 4 | +extern crate criterion; |
| 5 | +use criterion::{black_box, Benchmark, Criterion}; |
| 6 | + |
| 7 | +use futures::executor; |
| 8 | + |
| 9 | +fn bench_stream_iter(c: &mut Criterion) { |
| 10 | + executor::block_on(async { |
| 11 | + c.bench( |
| 12 | + "stream::iter", |
| 13 | + Benchmark::new("futures", |b| { |
| 14 | + b.iter(async move || { |
| 15 | + use futures::stream::{iter, StreamExt}; |
| 16 | + let mut stream = iter(1..=1000); |
| 17 | + while let Some(item) = stream.next().await { |
| 18 | + black_box(item); |
| 19 | + } |
| 20 | + }) |
| 21 | + }) |
| 22 | + .with_function("async_combinators", |b| { |
| 23 | + b.iter(async move || { |
| 24 | + use futures::stream::StreamExt; |
| 25 | + use futures_async_combinators::stream::iter; |
| 26 | + let mut stream = iter(1..=1000); |
| 27 | + while let Some(item) = stream.next().await { |
| 28 | + black_box(item); |
| 29 | + } |
| 30 | + }) |
| 31 | + }), |
| 32 | + ); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +fn bench_stream_next(c: &mut Criterion) { |
| 37 | + executor::block_on(async { |
| 38 | + c.bench( |
| 39 | + "stream::next", |
| 40 | + Benchmark::new("futures", |b| { |
| 41 | + b.iter(async move || { |
| 42 | + use futures::stream::{iter, StreamExt}; |
| 43 | + let mut stream = iter(1..=1000); |
| 44 | + while let Some(item) = stream.next().await { |
| 45 | + black_box(item); |
| 46 | + } |
| 47 | + }) |
| 48 | + }) |
| 49 | + .with_function("async_combinators", |b| { |
| 50 | + b.iter(async move || { |
| 51 | + use futures::stream::iter; |
| 52 | + use futures_async_combinators::stream::next; |
| 53 | + let mut stream = iter(1..=1000); |
| 54 | + while let Some(item) = next(&mut stream).await { |
| 55 | + black_box(item); |
| 56 | + } |
| 57 | + }) |
| 58 | + }), |
| 59 | + ); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +fn bench_stream_collect(c: &mut Criterion) { |
| 64 | + executor::block_on(async { |
| 65 | + c.bench( |
| 66 | + "stream::collect", |
| 67 | + Benchmark::new("futures", |b| { |
| 68 | + b.iter(async move || { |
| 69 | + use futures::stream::{iter, StreamExt}; |
| 70 | + let stream = iter(1..=1000); |
| 71 | + let vec: Vec<_> = stream.collect().await; |
| 72 | + black_box(vec) |
| 73 | + }) |
| 74 | + }) |
| 75 | + .with_function("async_combinators", |b| { |
| 76 | + b.iter(async move || { |
| 77 | + use futures::stream::iter; |
| 78 | + use futures_async_combinators::stream::collect; |
| 79 | + let stream = iter(1..=1000); |
| 80 | + let vec: Vec<_> = collect(stream).await; |
| 81 | + black_box(vec) |
| 82 | + }) |
| 83 | + }), |
| 84 | + ); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +fn bench_stream_map(c: &mut Criterion) { |
| 89 | + executor::block_on(async { |
| 90 | + c.bench( |
| 91 | + "stream::map", |
| 92 | + Benchmark::new("futures", |b| { |
| 93 | + b.iter(async move || { |
| 94 | + use futures::stream::{iter, StreamExt}; |
| 95 | + let stream = iter(1..=1000); |
| 96 | + let stream = stream.map(|x| x + 42); |
| 97 | + let vec: Vec<_> = stream.collect().await; |
| 98 | + black_box(vec) |
| 99 | + }) |
| 100 | + }) |
| 101 | + .with_function("async_combinators", |b| { |
| 102 | + b.iter(async move || { |
| 103 | + use futures::stream::{iter, StreamExt}; |
| 104 | + use futures_async_combinators::stream::map; |
| 105 | + let stream = iter(1..=1000); |
| 106 | + let stream = map(stream, |x| x + 42); |
| 107 | + let vec: Vec<_> = stream.collect().await; |
| 108 | + black_box(vec) |
| 109 | + }) |
| 110 | + }), |
| 111 | + ); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +criterion_group!( |
| 116 | + benches, |
| 117 | + bench_stream_iter, |
| 118 | + bench_stream_next, |
| 119 | + bench_stream_collect, |
| 120 | + bench_stream_map, |
| 121 | +); |
| 122 | +criterion_main!(benches); |
0 commit comments