I tried multiple ranges for this example I let above:
import time
def reader():
for a in range(100000000):
yield a
def reader_wrapper(gen):
for i in gen:
yield i
def reader_wrapper_enhanced(gen):
yield from gen
wrap = reader_wrapper_enhanced(reader())
start = time.perf_counter()
for i in wrap:
...
print("LAST: %s " % (time.perf_counter() - start))
wrap = reader_wrapper(reader())
start = time.perf_counter()
for i in wrap:
...
print("LAST: %s " % (time.perf_counter() - start))
My main question is that yield from is actually faster than a normal yield using loop.
Result for ranges:
Note: First result is the yield from and the second the yield.
RANGE | Yield from | yield+loop |
---|---|---|
100 | 1.4644000000001156e-05 | 1.3087000000000168e-05 |
100000 | 0.010678924 | 0.012484127000000005 |
100000000 | 7.763497913 | 8.586706118000002 |
10000000000 | 794.1722820499999 | 807.1722820400000 |
In case it is faster, should not we always just use it in this kind of cases?