The two following single-threading and multi-threading scripts are taking the same time when I give as input a big number like 555550000
single thread
import threading, time
a=[]
def print_factors(x):
for i in range(1, x + 1):
if x % i == 0:
a.append(i)
n=int(input("Please enter a large number"))
print ("Starting time is %s" % ( time.ctime(time.time()) ))
print("The factors of",n,"are:")
thread = threading.Thread(target=print_factors,args=(n,))
thread.start()
thread.join()
print("Finishing time is %s" % (time.ctime(time.time())))
print(a)
multi thread
import threading, time
a=[]
def print_factors1(x):
for i in range(1, int(x/2)):
if x % i == 0:
a.append(i)
def print_factors2(x):
for i in range(int(x/2), x+1):
if x % i == 0:
a.append(i)
n=int(input("Please enter a large number"))
print ("Starting time is %s" % ( time.ctime(time.time()) ))
thread1 = threading.Thread(target=print_factors1,args=(n,))
thread2 = threading.Thread(target=print_factors2,args=(n,))
print("The factors of",n,"are:")
thread1.start()
thread2.start()
thread2.join()
print("Finishing time is %s" % (time.ctime(time.time())))
print(a)
I am trying to understand the difference between single-threading and multi-threading in terms of time taken to got the results.
I'm measuring similar timings for both types and I cannot figuring out the reasons.