Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
745 views
in Technique[技术] by (71.8m points)

python - yield slower than return. why?

I wrote two function f and g with same functionality

def f(l, count):
    if count > 1:
        for i in f(l, count-1):
            yield i + 1
    else:

        yield from l

for i in f(range(100000),900):
    pass
print('f')

and

def g(l, count):
    if count > 1:
        tmp = []
        for i in g(l, count-1):
            tmp.append(i+1)
        return tmp
    else:
        return l
for i in g(range(100000),900):
    pass
print('f')

and i I think f shuold be faster but g is faster when in run it

time for g

real    0m5.977s
user    0m5.956s
sys     0m0.020s

time for f

real    0m7.389s
user    0m7.376s
sys     0m0.012s
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a couple of big differences between a solution that yields a result and one that computes the complete result.

The yield keeps returning the next result until exhausted while the complete calculation is always done fully so if you had a test that might terminate your calculation early, (often the case), the yield method will only be called enough times to meet that criteria - this often results in faster code.

The yield result only consumes enough memory to hold the generator and a single result at any moment in time - the full calculation consumes enough memory to hold all of the results at once. When you get to really large data sets that can make the difference between something that runs regardless of the size and something that crashes.

So yield is slightly more expensive, per operation, but much more reliable and often faster in cases where you don't exhaust the results.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...