# Python Fast conversion from int to string

## Question:

I am solving massive factorials in python and have found that when I am done calculating the factorial it takes the same time to convert to string to save to a file. I have tried to find a fast way to convert an int to string. I will put an example of computation and int conversion time. I am using the generic a = str(a) but fell like there is a better way like using a buffer or library.

EX:

Solving 100,000 factorial = 456,574 Digets

Computation time: 6.36 Seconds

Conversion time: 5.20 Seconds

If you have any segestions / solutions let me know! Anything will help.

``````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")
``````

You could try 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")
``````

Above test Output:

1.0336394309997559 Python conversion time

0.03306150436401367 gmpy2 conversion time

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

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

`int().__str__()` is faster than `str(int)` because `str()` needs to find which type it is converting.

``````import time

start=time.time()
x1=[x.__str__() for x in range(10000)]
print(f"__str__: {time.time()-start}")

start=time.time()
x2=[str(x) for x in range(10000)]
print(f"str: {time.time()-start}")

print(x1==x2)

>>>__str__: 0.005000591278076172
>>>str: 0.006000518798828125
>>>True
``````
Categories: questions
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.