# "python fast conversion from int to string" Code Answer

3

this code is faster (but not enough! :d)

# result:

``````╔═══╦════════════╦═════════════╦══════════════╦═══════════════════╗
║   ║ 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       ║
╚═══╩════════════╩═════════════╩══════════════╩═══════════════════╝
``````

anyway, i think you can do it faster with multi-threading.

import time import math import threading

``````res_dict = {}

if threadid == 1 :
res_dict[threadid] = (str(factorial // 10 ** (each_thread * (max_thread - 1))))
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

tr = []

for item in range(1, max_thread + 1):
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")
``````

# update:

just run your code with `pypy` it's amazingly fast!

``````╔═══╦════════════╦═════════════╦══════════════╦═══════════════════╗
║   ║ 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       ║
╚═══╩════════════╩═════════════╩══════════════╩═══════════════════╝
``````
By user2511140 on March 18 2022