Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Python is faster using "yield from" than yield in a loop?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

Yes, it’s faster when the inputs are long enough (though not by much, as you’ve seen), and yes, you may as well let Python do the work of yielding from the delegate iterator by default.

The one time you wouldn’t want to do this is when you are delegating to a generator that you don’t want to receive values from the caller sent with .send or .throw; when using plain yield, your generator receives them, when using yield from, the delegate generator receives them (usually the latter is what you want, and it’s the primary reason yield from exists in the first place).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading