yield slower than return. why?

落花浮王杯 提交于 2020-01-04 14:12:42

问题


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

回答1:


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.




回答2:


I have no idea what your h and g functions are doing but remember this,

Yield is a keyword that is used like return, except the function will return a generator and that is the reason it takes time.

There is a wonderful explanation about what yeild does. Check this answer on stackoverflow.



来源:https://stackoverflow.com/questions/41671005/yield-slower-than-return-why

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!