update primefac func name
This commit is contained in:
parent
10df6f8625
commit
12db9a813e
|
@ -3,22 +3,23 @@ from random import randint
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
# This example compare the speed of CPU-bound operations
|
# This example compare the speed of CPU-bound operations
|
||||||
# when using a sequential method and also when using a
|
# when using a sequential method and also when using a
|
||||||
# multi-process method with the 'multiprocessing' Python
|
# multi-process method with the 'multiprocessing' Python
|
||||||
# package
|
# package
|
||||||
|
|
||||||
|
|
||||||
def calculatePrimeFactors(n):
|
def calculatePrimeFactors(n):
|
||||||
primfac = []
|
prime_factors = []
|
||||||
d = 2
|
d = 2
|
||||||
while d*d <= n:
|
while d*d <= n:
|
||||||
while (n % d) == 0:
|
while (n % d) == 0:
|
||||||
primfac.append(d) # supposing you want multiple factors repeated
|
# supposing you want multiple factors repeated
|
||||||
|
prime_factors.append(d)
|
||||||
n //= d
|
n //= d
|
||||||
d += 1
|
d += 1
|
||||||
if n > 1:
|
if n > 1:
|
||||||
primfac.append(n)
|
prime_factors.append(n)
|
||||||
return primfac
|
return prime_factors
|
||||||
|
|
||||||
|
|
||||||
# Sequential Example
|
# Sequential Example
|
||||||
|
@ -34,14 +35,13 @@ def seq_crunch():
|
||||||
return f'{t1-t0:.2f}'
|
return f'{t1-t0:.2f}'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Multi-processing Example
|
# Multi-processing Example
|
||||||
|
|
||||||
|
|
||||||
def executeProc():
|
def executeProc():
|
||||||
for i in range(1000):
|
for i in range(1000):
|
||||||
rand = randint(20000, 100000000)
|
rand = randint(20000, 100000000)
|
||||||
calculatePrimeFactors(rand)
|
calculatePrimeFactors(rand)
|
||||||
|
|
||||||
|
|
||||||
def multi_proc_crunch():
|
def multi_proc_crunch():
|
||||||
|
@ -60,11 +60,11 @@ def multi_proc_crunch():
|
||||||
t1 = time()
|
t1 = time()
|
||||||
return f'{t1-t0:.2f}'
|
return f'{t1-t0:.2f}'
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
seq_time = seq_crunch()
|
seq_time = seq_crunch()
|
||||||
multi_proc_time = multi_proc_crunch()
|
multi_proc_time = multi_proc_crunch()
|
||||||
print('\n==================================')
|
print('\n==================================')
|
||||||
print(f'Time to crunch prime factors sequentially: {seq_time}s')
|
print(f'Crunch prime factors sequentially: {seq_time}s')
|
||||||
print(f'Time to crunch prime factors using multi-processing: {multi_proc_time}s')
|
print(f'Crunch prime factors using multi-processing: {multi_proc_time}s')
|
||||||
print('==================================')
|
print('==================================')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue