我正在用python解决大量阶乘,发现当我完成计算阶乘时,转换成字符串并保存到文件需要花费相同的时间。我试图找到一种将int转换为字符串的快速方法。我将举一个计算和int转换时间的示例。我正在使用泛型a = str(a),但感觉像使用缓冲区或库那样有更好的方法。
例如:
解决100,000个阶乘= 456,574 Digets
计算时间:6.36秒
转换时间:5.20秒
如果您有任何阻塞/解决方案,请告诉我!一切都会有帮助的。
import time
factorial = 1
print(" ")
one = int(input("lower = "))
two = int(input("higher = "))
start = time.time()
for x in range(one,two + 1):
factorial = factorial * two
two = two - 1
end = time.time()
print("DONE! ")
print(end - start, "Seconds to compute")
start = time.time()
factorial = str(factorial)
f = open('Ans_1.txt','w')
f.write(factorial)
f.close()
end = time.time()
print(end - start, "Seconds to convert and save")
print(len(factorial), …Run Code Online (Sandbox Code Playgroud)