迭代迭代? [英] Iteration over recursion?

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

问题描述

大家好,


我一直在搞乱创建试验分工因素分解

函数我自然对优化感兴趣它和

可能一样多。


我被告知python中的迭代通常更多

时间效率而不是递归。这是真的吗?


这是我的算法。任何建议表示赞赏!


来自数学导入*


def factorize(x):

"" "返回因子x""

因子= []

lim = int(sqrt(x))

y = divmod( x,2)

如果y [1] == 0:#x是偶数

因子=因子+ [2] +因子分解(y [0])

else:#x是奇数

i = 3

而i< = lim:

y = divmod(x, i)

如果y [1] == 0:

因子=因子+ [i] +因子化(y [0])

i = lim + 1

else:

i + = 2

if factors == []:#正确递归的必要步骤

因子= [x]


返回因子

解决方案

< blockquote>

MTD写道:

大家好,


(剪辑)

我''有人告诉我,python中的迭代通常比递归更具时间效率。这是真的吗?




(剪辑)


AFAIK,在大多数语言中它都是记忆的东西。每次函数

调用自身时,该函数的''state''必须存储在某个地方

,以便它可以继续,就像递归函数一样返回。

因此,您可以有效地将其视为创建N个对象

,直到最后一次嵌套调用才开始发布

返回。这(取决于堆栈大小和实现等...)

可能会强制进行多次内存分配。然后当然,当它开始

回到''向上''(朝向递归调用的发起者

)时,垃圾收集器可以启动释放内存......


根据返回值,迭代将只需要空间来支持每个函数的

返回值(在大多数情况下 - 我

可以想象适合堆叠),所以虽然它有效地实现了同样的事情,但它的内存却更少。


我可能没有太好解释,我甚至可能都不对。所以

带上一小撮盐。


一切顺利,


Jon。




文章< 11 ********************** @ h76g2000cwa.googlegroups .com>,

" Jon Clements" <乔**** @ googlemail.com>写道:

|> MTD写道:

|>

|> >我被告知python中的迭代通常更多

|> >比递归更节省时间。这是真的吗?

|>

|> AFAIK,在大多数语言中它都是记忆的东西。每次一个功能

|>自称,该功能的状态必须存储在某个地方

|>这样它就可以继续,就像递归函数返回一样。


好​​吧,含糊地说。在Python中可能就是这种情况,但并不总是这样,并且不是唯一的问题。


删除尾递归通常可以消除内存消耗,但是必须编写

代码才能工作 - 而且我不知道是否知道这需要什么呢。

Python是否适用。


在编译的第3版GL中语言,有很多优化问题

其中迭代通常会产生比递归更好的代码,但很少有b $ b(如果有的话)与Python相关。

问候,

Nick Maclaren。


Nick Maclaren写道:

(snip)

删除尾部递归通常可以消除内存消耗,但是必须编写代码才能使用 - 而且我不知道是否知道Python是否可以。




它没有。技术可行,但是BDFL的决定......


-

bruno desthuilliers

python -c" print ''@''。join([''。''。join([w [:: - 1] for p in p.split(''。'')])for

p in ''o **** @ xiludom.gro''。split(''''')])"


Hello all,

I''ve been messing about for fun creating a trial division factorizing
function and I''m naturally interested in optimising it as much as
possible.

I''ve been told that iteration in python is generally more
time-efficient than recursion. Is that true?

Here is my algorithm as it stands. Any suggestions appreciated!

from math import *

def factorize(x):
""" Return factors of x """
factors = []
lim = int(sqrt(x))
y = divmod(x,2)
if y[1] == 0: # x is even
factors = factors + [2] + factorize(y[0])
else: # x is odd
i = 3
while i <= lim:
y = divmod(x,i)
if y[1] == 0:
factors = factors + [i] + factorize(y[0])
i = lim+1
else:
i += 2

if factors == []: # necessary step for correct recursion
factors = [x]

return factors

解决方案


MTD wrote:

Hello all,

(snip)
I''ve been told that iteration in python is generally more
time-efficient than recursion. Is that true?



(snip)

AFAIK, in most languages it''s a memory thing. Each time a function
calls itself, the ''state'' of that function has to be stored somewhere
so that it may continue, as was, when the recursive function returns.
Therefore, you can effectively think of it as creating N many objects
which don''t start getting released until the very last nested call
returns. This (depending on the stack size and implementation etc...)
may force several memory allocations. Then of course, as it starts
going back ''upwards'' (towards the initiator of the recursive call that
is), the garbage collector may kick in freeing memory...

Depending on return values, iterating will just require space for the
returned value from each function in term (which in most cases - I
would imagine fits on the stack), so although it''s doing effectively
the same thing, it''s doing so with less memory.

I probably haven''t explained too well, and I may not even be right. So
take with a pinch of salt.

All the best,

Jon.



In article <11**********************@h76g2000cwa.googlegroups .com>,
"Jon Clements" <jo****@googlemail.com> writes:
|> MTD wrote:
|>
|> > I''ve been told that iteration in python is generally more
|> > time-efficient than recursion. Is that true?
|>
|> AFAIK, in most languages it''s a memory thing. Each time a function
|> calls itself, the ''state'' of that function has to be stored somewhere
|> so that it may continue, as was, when the recursive function returns.

Well, vaguely. That is probably the case in Python, but it isn''t always
true, and is not the only issue.

Tail recursion removal can often eliminate the memory drain, but the
code has to be written so that will work - and I don''t know offhand
whether Python does it.

In compiled "3rd GL" languages, there are a lot of optimisation issues
where iteration will often produce better code than recursion, but few
(if any) are relevant to Python.
Regards,
Nick Maclaren.


Nick Maclaren wrote:
(snip)

Tail recursion removal can often eliminate the memory drain, but the
code has to be written so that will work - and I don''t know offhand
whether Python does it.



It doesn''t. Technical possible, but BDFL''s decision...

--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


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

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