Python 的 math.factorial 没有被记忆,它是一个简单的 for 循环,将值从 1 乘以你的 arg。如果您需要记忆,则需要明确地进行。
这是使用字典 setdefault 方法进行记忆的简单方法。
import math
cache = {}
def myfact(x):
return cache.setdefault(x,math.factorial(x))
print myfact(10000)
print myfact(10000)
Run Code Online (Sandbox Code Playgroud)
在此链接上搜索 math_factorial,您将找到它在 python 中的实现:
http://svn.python.org/view/python/trunk/Modules/mathmodule.c?view=markup
PS这是针对python2.6的