나는 factorials를 생성하기 위해 파이썬에서 몇 가지 모듈을 썼다. 그리고 나는 실행 시간을 테스트하려고한다. 나는 here 프로파일의 예를 발견하고 내 모듈 프로필에 해당 템플릿을 사용 : 그러나파이썬 프로파일 링
import profile #fact
def main():
x = raw_input("Enter number: ")
profile.run('fact(int(x)); print')
profile.run('factMemoized(int(x)); print')
def fact(x):
if x == 0: return 1
elif x < 2: return x
else:
return x * fact(x-1)
def factMemoized(x):
if x == 0: return 1
elif x < 2: return x
dict1 = dict()
dict1[0] = 1
dict1[1] = 1
for i in range (0, x+1):
if dict1.has_key(i): pass
else: dict1[i] = i * dict1[i-1]
return dict1[x]
if __name__ == "__main__":
main()
을, 나는 다음과 같은 오류가 발생합니다 : 내가 잘못 여기
Enter number: 10
Traceback (most recent call last):
File "fact.py", line 32, in <module>
main()
File "fact.py", line 7, in main
profile.run('fact(int(x)); x')
File "C:\Python27\lib\profile.py", line 70, in run
prof = prof.run(statement)
File "C:\Python27\lib\profile.py", line 447, in run
return self.runctx(cmd, dict, dict)
File "C:\Python27\lib\profile.py", line 453, in runctx
exec cmd in globals, locals
File "<string>", line 1, in <module>
NameError: name 'x' is not defined
어떤 생각을하고 있어요 뭐? 티아! ~ craig
감사 @Constantinius보다 낫다! 나의 일부에 doh 순간이었다 :) – Craig