论坛首页 编程语言技术论坛

python的stack是不是太小了?

浏览 13229 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-12-15  
Python:
def f(n);:
 	if n == 0:
 		return 1
 	return n + f(n - 1);
>>> f(992);
492529
>>> f(993);
RuntimeError: maximum recursion depth exceeded


同样的东东,在java里面
    public static int f(int n); {
        return n > 0 ? n + f(n - 1); : 1;
    }
f(8592);
36915529
f(8593);
Exception in thread "main" java.lang.StackOverflowError

这差距也太大了吧!!!
   发表时间:2005-12-15  
javascript:

function f(n); {
    return n > 0 ? n + f(n - 1); : 1;
}

ie:  f(1112);=618829,  f(1113); Stack overflow
ff:  f(998);=498501,  f(999); too much recursion
0 请登录后投票
   发表时间:2005-12-15  
有问题吗?没什么问题吧
0 请登录后投票
   发表时间:2005-12-15  
c/c++在这方面比较强,算到10万多,结果int都溢出
int f(int n); {
	return n > 0 ? n + f(n - 1); : 1;
}
f(130000);
-139869591
0 请登录后投票
   发表时间:2005-12-15  
ruby:
def f(n);
    n>1?n+f(n-1);:1
end

puts f(746);

引用

untitled1.rb:2:in `f': stack level too deep (SystemStackError)
from untitled1.rb:2:in `f'
from untitled1.rb:2:in `f'
from untitled1.rb:2:in `f'
from untitled1.rb:2:in `f'
from untitled1.rb:2:in `f'
from untitled1.rb:2:in `f'
from untitled1.rb:2:in `f'
from untitled1.rb:2:in `f'
... 729 levels...
from untitled1.rb:2:in `f'
from untitled1.rb:2:in `f'
from untitled1.rb:2:in `f'
from untitled1.rb:5
>Exit code: 1

0 请登录后投票
   发表时间:2005-12-16  
setrecursionlimit( limit)

Set the maximum depth of the Python interpreter stack to limit. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.
The highest possible limit is platform-dependent. A user may need to set the limit higher when she has a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.


import sys
def f(n);:
	if n == 0:
		return 1
	return n + f(n - 1);
sys.setrecursionlimit(100000);
print f(10000);

在偶的机上,当print f(12000)的时候就没有输出了:(
0 请登录后投票
   发表时间:2005-12-16  
import sys
print sys.getrecursionlimit()

sys.setrecursionlimit(limit)

默认是1000.
计算机配置不同有差异.比如我的:
>>> f(995)
495511
0 请登录后投票
   发表时间:2005-12-16  
PythonWin里面次数会少一些
dos下运行python,次数是准确的,默认1000能算到f(998), 从0开始,一共999次。
recursionlimit增加后,算到f(11000)都没问题,尝试f(11500) python就crash了。

recursionlimit应该和机器配置无关
0 请登录后投票
   发表时间:2006-01-16  
有什么地方会用到这么大的stack?
0 请登录后投票
   发表时间:2006-04-26  
Rebol 可以到846

REBOL/Core 2.5.6.3.1
Copyright 1997-2003 REBOL Technologies
REBOL is a Trademark of REBOL Technologies
All rights reserved.

Component: "REBOL Mezzanine Extensions" 1.1.2.1 (29-Nov-2002/19:29:09);
Component: "REBOL Internet Protocols" 1.59.2.15 (14-Feb-2003/1:45:14);
Finger protocol loaded
Whois protocol loaded
Daytime protocol loaded
SMTP protocol loaded
POP protocol loaded
IMAP protocol loaded
HTTP protocol loaded
FTP protocol loaded
NNTP protocol loaded
Component: "System Port" 1.1.2.5 (30-Nov-2002/17:24:03);
Script: "REBOL Extended Definitions" (none);
>> f: func [n] [
[     either n = 0 [
[          return 1
[         ]
[     [
[          return n + f n - 1
[         ]
[    ]
>> f 846
== 358282
>> f 847
** Internal Error: Stack overflow
** Near: either n = 0 [
    return 1
]
>>
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics