|
| 1 | +from collections.abc import Iterable |
| 2 | +from datetime import timedelta |
| 3 | +import timeit |
| 4 | +from streamable import stream |
| 5 | + |
| 6 | +N = 1_000_000 |
| 7 | +ints = stream(range(N)) |
| 8 | + |
| 9 | + |
| 10 | +def consume(s: Iterable): |
| 11 | + for _ in s: |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +baseline = timeit.timeit(lambda: consume(map(lambda _: _, ints)), number=10) / 10 |
| 16 | + |
| 17 | +for times, s in ( |
| 18 | + (50, ints.map(lambda _: _)), |
| 19 | + (50, ints.filter(lambda _: _)), |
| 20 | + (50, ints.do(lambda _: _)), |
| 21 | + (50, ints.skip(N)), |
| 22 | + (50, ints.catch(ValueError)), |
| 23 | + (50, ints.group(5)), |
| 24 | + (50, ints.take(N)), |
| 25 | + (10, ints.observe("ints", do=bool)), |
| 26 | + (10, ints.observe("ints", do=bool, every=N)), |
| 27 | + (10, ints.observe("ints", do=bool, every=timedelta(seconds=1))), |
| 28 | + (10, ints.throttle(N, per=timedelta(seconds=1))), |
| 29 | + (10, ints.group(5, by=bool)), |
| 30 | + (1, stream((i,) for i in range(N)).flatten()), |
| 31 | + (1, ints.buffer(N)), |
| 32 | + (1, ints.map(lambda _: _, concurrency=2)), |
| 33 | + (1, ints.map(lambda _: _, concurrency=2, as_completed=True)), |
| 34 | + (1, ints.group(within=timedelta(seconds=1))), |
| 35 | + (1, stream((i,) for i in range(N)).flatten(concurrency=2)), |
| 36 | +): |
| 37 | + duration = timeit.timeit(lambda: consume(s), number=times) / times |
| 38 | + print(s) |
| 39 | + print(f"is {duration / baseline:.2f}x slower than builtins.map") |
0 commit comments