发电机与功能? [英] Generators vs. Functions?

查看:77
本文介绍了发电机与功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


in< dr ********** @wake.carmen.se> ;,Magnus Lycka< ly *** @ carmen。 SE>发布了一个简短测试的结果

似乎表明恢复发电机比调用一个函数需要花费更多时间。


如果在一般情况下实际上也是如此,而不是由于最终上述测试的非代表性,那么仅仅是因为

减去 - 当前Pyython

翻译中的生成器的优化实现并因此可能在将来发生变化,或者这是一个原因并且因此将永远保持这样的状态?


TIA,


此致,


Wolfgang Keller

解决方案

Wolfgang Keller写道:

如果在一般情况下这实际上也是如此,而不是由于最终<上面提到的测试的非代表性,仅仅是因为当前Pyython中的生成器实现不够优秀翻译,因此可能会在未来发生变化,或者这是原理的问题,因此会永远保持这样的状态吗?




我不是CPython或PyPy黑客,但我猜它总是会因为校长问题而变慢。当恢复生成器时,你有
来重置上次调用函数时所处的状态,我认为应该总是比使用函数调用函数更昂贵

清洁状态。


有人想纠正我吗?


不管差异是否显着我不确定。它b $ b可能足够小,对于大多数应用程序没有人关心。


Joseph Garvin写道:


有人想纠正我吗?


如果要返回数千个(大)值,列表

(由函数返回)可能足够大以至于需要内存

分页,而生成器一次只返回一个值。

虽然我不确定,但差异是否显着。它可能足够小,对于大多数应用程序没有人关心。




我刚写了一个应用程序,它从300mb中检索值

数据库,并使用迭代器获得了显着的加速。


--Max


Joseph Garvin写道:

Wolfgang Keller写道:

如果在一般情况下这实际上也是如此,而不是由于最终上述测试的非代表性,是否仅仅是由于在当前的Pyython解释器中实现不太优化的生成器因此可能在将来发生变化,或者这是原理的问题,因此会永远保持这样的状态吗? / blockquote>

我不是CPython或PyPy黑客,但我猜它总是会因为校长问题而变慢。当恢复生成器时,你可以重置上次调用函数所处的状态,我认为应该总是比调用具有干净状态的函数更昂贵。 br />
有人想纠正我吗?




当然。 你必须重置函数的状态......取决于

whatresetup意思是(不是通常的英文单词,所以我们可能都会想到

的不同含义),

最后一句的第一部分或第二部分都是假的。 br />

更准确地说,当收益率

出现时,函数的状态被*保存*,所以你当然不能从头开始重新创建它,但只是

恢复状态,这肯定比创建它更快

从头开始。我没有看过源代码,但是

除了一点点内存复制之外,这并不需要太多,或者即使是一些指针改变也不需要b $ b,而原作可能涉及很多

的工作,具体取决于传递了多少参数,有多少本地人存在,等等。


-Peter


Hello,

in <dr**********@wake.carmen.se>, Magnus Lycka <ly***@carmen.se> posts the
result of a short test that seems to indicate that resuming a generator takes
more time than calling a function.

If this is actually also true in the general case, and not due to eventual
non-representativeness of the test mentioned above, is it simply due to a
less-than-optimum implementation of generators in the current Pyython
interpreter and thus likely to change in the future or is this a matter of
principle and will consequently remain like this forever?

TIA,

Sincerely,

Wolfgang Keller

解决方案

Wolfgang Keller wrote:

If this is actually also true in the general case, and not due to eventual
non-representativeness of the test mentioned above, is it simply due to a
less-than-optimum implementation of generators in the current Pyython
interpreter and thus likely to change in the future or is this a matter of
principle and will consequently remain like this forever?



I am not a CPython or PyPy hacker, but I would guess that it will always
be slower as a matter of principal. When resuming a generator you have
to resetup the state the function was in when it was last called, which
I think should always be more costly than calling the function with a
clean state.

Someone want to correct me?

Whether or not the difference is that significant though I am unsure. It
may be small enough that for most applications no one cares.


Joseph Garvin wrote:


I am not a CPython or PyPy hacker, but I would guess that it will always
be slower as a matter of principal. When resuming a generator you have
to resetup the state the function was in when it was last called, which
I think should always be more costly than calling the function with a
clean state.

Someone want to correct me?
In cases where there are thousands of (large) values to return, the list
(as returned by the function) may be large enough to require memory
paging, whereas the generator only returns one value at a time.

Whether or not the difference is that significant though I am unsure. It
may be small enough that for most applications no one cares.



I just wrote an application which retrieves values from a 300mb
database, and got a significant speedup using iterators.

--Max


Joseph Garvin wrote:

Wolfgang Keller wrote:

If this is actually also true in the general case, and not due to eventual
non-representativeness of the test mentioned above, is it simply due to a
less-than-optimum implementation of generators in the current Pyython
interpreter and thus likely to change in the future or is this a matter of
principle and will consequently remain like this forever?



I am not a CPython or PyPy hacker, but I would guess that it will always
be slower as a matter of principal. When resuming a generator you have
to resetup the state the function was in when it was last called, which
I think should always be more costly than calling the function with a
clean state.

Someone want to correct me?



Sure. "You have to resetup the state of the function"... depending on
what "resetup" means (not a usual English word, so we might all imagine
different meanings for it), either the first or the second part of the
last sentence is false.

More precisely, the state of the function is *saved* when a yield
occurs, so you certainly don''t *recreate* it from scratch, but merely
restore the state, and this should definitely be faster than creating it
from scratch in the first place. I haven''t looked at the source, but
this wouldn''t have to involve much beyond a little memory copying, or
even a few pointer changes, whereas the original could involve a lot of
work, depending on how many arguments were passed, how many locals
exist, and so on.

-Peter


这篇关于发电机与功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆