I would like to create some 50000 files using python and they are very simple files with each file with less than 20 lines in it.
I first tried adding threading just for the sake of it and it took 220 seconds on my i7 8th gen machine.
WITH THREAD
def random_files(i):
filepath = path+"/content/%s.html" %(str(i))
fileobj = open(filepath,"w+")
l1 = "---\n"
l2 = 'title: "test"\n'
l3 = "date: 2019-05-01T18:37:07+05:30"+"\n"
l4 = "draft: false"+"\n"
l5 = 'type: "statecity"'+"\n"
l6 = "---"+"\n"
data = l1+l2+l3+l4+l5+l6
fileobj.writelines(data)
fileobj.close()
if __name__ == "__main__":
start_time = time.time()
for i in range(0, 50000):
i = str(i)
threading.Thread(name='random_files', target=random_files, args=(i,)).start()
print("--- %s seconds ---" % (time.time() - start_time))
WITHOUT THREAD
Doing the non thread route takes 55 seconds.
def random_files():
for i in range(0, 50000):
filepath = path+"/content/%s.html" %(str(i))
fileobj = open(filepath,"w+")
l1 = "---\n"
l2 = 'title: "test"\n'
l3 = "date: 2019-05-01T18:37:07+05:30"+"\n"
l4 = "draft: false"+"\n"
l5 = 'type: "statecity"'+"\n"
l6 = "---"+"\n"
data = l1+l2+l3+l4+l5+l6
fileobj.writelines(data)
fileobj.close()
if __name__ == "__main__":
start_time = time.time()
random_files()
print("--- %s seconds ---" % (time.time() - start_time))
CPU usage is 10% for python task RAM usage is a meager 50mb Disk usage is an average 4.5 Mb/second
Can the speed be increased drastically.