I have written a program that take 0.1092 seconds to generate a number but 787.26012 seconds =13 minute to write that number (3.81 MB) in/on a text file, which is quite surprising, can anyone explain why that is happening? Also, is there a faster way to write that large number on a text file?
import time
import sys
import math
start = time.time()
input_file="First1_58MB_enwik9.txt"
with open(input_file, "rb") as file: #--> open file in binary read mode
byte_obj = file.read() #--> read all binary data
g=int.from_bytes( byte_obj, byteorder=sys.byteorder)
binary_dt=bin(g)
int_v=int(binary_dt,2)
length = math.ceil(math.log(int_v, 256))
res = int.to_bytes(int_v, length=length, byteorder='little', signed=False)
open("output_file_2.txt", "wb").write(res)
end = time.time()
print("Total time (in seconds) = ",(end - start))
#---------------------------
start = time.time()
with open("output_file_1.txt", 'w') as f:
f.write('%d' % g)
end = time.time()
print("Total time (in seconds) = ",(end - start))