Python 从 int 到 string 的快速转换
Python Fast conversion from int to string
我正在求解 python 中的大量阶乘,我发现当我计算完阶乘后,转换为字符串以保存到文件中所需的时间相同。我试图找到一种将 int 转换为字符串的快速方法。我将举一个计算和 int 转换时间的例子。我正在使用通用的 a = str(a) 但觉得有更好的方法,比如使用缓冲区或库。
例如:
求解 100,000 个阶乘 = 456,574 个数字
计算时间: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), "Digets")
你可以试试 gmpy2 x.digits([base]).
import time
from gmpy2 import mpz
x = 123456789**12345
start = time.time()
python_str = str(x)
end = time.time()
print(end - start, "Python conversion time")
r = mpz(x)
start = time.time()
gmpy2_str = r.digits()
end = time.time()
print(end-start, "gmpy2 conversion time")
以上测试输出:
1.0336394309997559 Python 转换时间
0.03306150436401367 gmpy2转换时间
此代码速度更快(但还不够!:D)
结果:
╔═══╦════════════╦═════════════╦══════════════╦═══════════════════╗
║ ║ Count ║ Compute(s) ║ Convert(s) ║ M.T Convert(s) ║
╠═══╬════════════╬═════════════╬══════════════╬═══════════════════╣
║ 1 ║ 100,000 ║ 2.68 ║ 3.85 ║ 2.81 ║
║ 2 ║ 250,000 ║ 21.17 ║ 39.83 ║ 21.09 ║
╚═══╩════════════╩═════════════╩══════════════╩═══════════════════╝
无论如何,我认为你可以使用多线程更快地完成它。
导入时间
导入数学
导入线程
res_dict = {}
def int_str(threadID, each_thread, max_thread):
if threadID == 1 :
res_dict[threadID] = (str(factorial // 10 ** (each_thread * (max_thread - 1))))
elif threadID == max_thread:
res_dict[threadID] = (str(int(factorial % 10 ** (each_thread * 1))))
else:
tmp = (factorial % 10 ** (each_thread * (max_thread - threadID + 1))) // 10 ** (each_thread * (max_thread - threadID))
pre = "0" * ((digits // max_thread) - (math.floor(math.log10(tmp))+1))
res_dict[threadID] = (pre + str(int(tmp)))
factorial = 1
print(" ")
def fact(a,b):
if b == 1:
return 1
else:
return a * fact(a,b-1)
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()
digits = math.floor(math.log10(factorial))+1
max_thread = 3
each_thread = digits // max_thread
tr = []
for item in range(1, max_thread + 1):
t = threading.Thread(target=int_str, args=(item, each_thread, max_thread))
t.start()
tr.append(t)
for item in tr:
item.join()
last_res = ''
for item in sorted(res_dict):
if item != max_thread:
last_res += res_dict[item]
else:
last_res += ("0" * (digits - len(last_res) - len(res_dict[item]))) + res_dict[item]
f = open('Ans_2.txt','w')
f.write(last_res)
f.close()
end = time.time()
print(end - start, "Seconds to convert and save")
print(digits, "Digets")
更新:
只需 运行 你的代码 pypy
速度快得惊人!
╔═══╦════════════╦═════════════╦══════════════╦═══════════════════╗
║ ║ Count ║ Compute(s) ║ Convert(s) ║ pypy Convert(s) ║
╠═══╬════════════╬═════════════╬══════════════╬═══════════════════╣
║ 1 ║ 100,000 ║ 2.98 ║ 3.85 ║ 0.79 ║
║ 2 ║ 250,000 ║ 25.83 ║ 39.83 ║ 7.17 ║
╚═══╩════════════╩═════════════╩══════════════╩═══════════════════╝
我正在求解 python 中的大量阶乘,我发现当我计算完阶乘后,转换为字符串以保存到文件中所需的时间相同。我试图找到一种将 int 转换为字符串的快速方法。我将举一个计算和 int 转换时间的例子。我正在使用通用的 a = str(a) 但觉得有更好的方法,比如使用缓冲区或库。
例如:
求解 100,000 个阶乘 = 456,574 个数字
计算时间: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), "Digets")
你可以试试 gmpy2 x.digits([base]).
import time
from gmpy2 import mpz
x = 123456789**12345
start = time.time()
python_str = str(x)
end = time.time()
print(end - start, "Python conversion time")
r = mpz(x)
start = time.time()
gmpy2_str = r.digits()
end = time.time()
print(end-start, "gmpy2 conversion time")
以上测试输出:
1.0336394309997559 Python 转换时间
0.03306150436401367 gmpy2转换时间
此代码速度更快(但还不够!:D)
结果:
╔═══╦════════════╦═════════════╦══════════════╦═══════════════════╗
║ ║ Count ║ Compute(s) ║ Convert(s) ║ M.T Convert(s) ║
╠═══╬════════════╬═════════════╬══════════════╬═══════════════════╣
║ 1 ║ 100,000 ║ 2.68 ║ 3.85 ║ 2.81 ║
║ 2 ║ 250,000 ║ 21.17 ║ 39.83 ║ 21.09 ║
╚═══╩════════════╩═════════════╩══════════════╩═══════════════════╝
无论如何,我认为你可以使用多线程更快地完成它。
导入时间 导入数学 导入线程
res_dict = {}
def int_str(threadID, each_thread, max_thread):
if threadID == 1 :
res_dict[threadID] = (str(factorial // 10 ** (each_thread * (max_thread - 1))))
elif threadID == max_thread:
res_dict[threadID] = (str(int(factorial % 10 ** (each_thread * 1))))
else:
tmp = (factorial % 10 ** (each_thread * (max_thread - threadID + 1))) // 10 ** (each_thread * (max_thread - threadID))
pre = "0" * ((digits // max_thread) - (math.floor(math.log10(tmp))+1))
res_dict[threadID] = (pre + str(int(tmp)))
factorial = 1
print(" ")
def fact(a,b):
if b == 1:
return 1
else:
return a * fact(a,b-1)
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()
digits = math.floor(math.log10(factorial))+1
max_thread = 3
each_thread = digits // max_thread
tr = []
for item in range(1, max_thread + 1):
t = threading.Thread(target=int_str, args=(item, each_thread, max_thread))
t.start()
tr.append(t)
for item in tr:
item.join()
last_res = ''
for item in sorted(res_dict):
if item != max_thread:
last_res += res_dict[item]
else:
last_res += ("0" * (digits - len(last_res) - len(res_dict[item]))) + res_dict[item]
f = open('Ans_2.txt','w')
f.write(last_res)
f.close()
end = time.time()
print(end - start, "Seconds to convert and save")
print(digits, "Digets")
更新:
只需 运行 你的代码 pypy
速度快得惊人!
╔═══╦════════════╦═════════════╦══════════════╦═══════════════════╗
║ ║ Count ║ Compute(s) ║ Convert(s) ║ pypy Convert(s) ║
╠═══╬════════════╬═════════════╬══════════════╬═══════════════════╣
║ 1 ║ 100,000 ║ 2.98 ║ 3.85 ║ 0.79 ║
║ 2 ║ 250,000 ║ 25.83 ║ 39.83 ║ 7.17 ║
╚═══╩════════════╩═════════════╩══════════════╩═══════════════════╝