For some reason i keep getting 0.0 for the series time and the delta time intervals USING PYTHON MODIFY CODE SO DESIRED OUTPUT IS REACHED DESIRED OUTPUT : correct time calculation to find series and the paralell allow user input to find sequence and time intervals ******************************************************************************************************************************* import multiprocessing as mp import json import time import sys def fib(n: int) -> int: #Calculate n'th number in Fibonacci secuence #return n'th number if n < 2: return 1 else: return fib(n - 1) + fib(n - 2) def parallel(num1: int, num2: int) -> list: #Calculates Fibonacci sequence between num1 and num2 parallel #return list of time taken by calculation, results pool = mp.Pool(processes=mp.cpu_count()) a: float = time.time() results = [pool.map(fib, (x for x in range(num1-1, num2)))] b: float = time.time() print("parallel: "+str(results)) print(f'parallel time:{b-a}') return [b-a, results] def normal(num1: int, num2: int) -> list: #Calculates Fibonacci sequence between num1 and num2 series #return time taken by calculation results = [] for x in range(num1-1, num2): results.append(fib(x)) a: float = time.time() b: float = time.time() print(f'series time:{b-a}') return [b-a, results] def resulter(num1: int, num2: int) -> tuple: #Makes calculations series and parallel #return num1, num2, series calc time, parallel calc time, series/parallel b: float = parallel(num1, num2)[0] a: float = normal(num1, num2)[0] print(f'delta time (series/parallel):{a/b}') return (num1, num2, a, b, a/b) #Main that sets the values def main(): data_start: int = 0 data_end: int = 0 print(f"cpu core count:{mp.cpu_count()}") try: data_start = int(sys.argv[1]) data_end = int(sys.argv[2]) print(f"values are {data_start} - {data_end}") except: data_start = 4 data_end = 20 print("values are 4 - 20") return resulter(data_start, data_end) if __name__ == '__main__': main()
For some reason i keep getting 0.0 for the series time and the delta time intervals
USING PYTHON MODIFY CODE SO DESIRED OUTPUT IS REACHED
DESIRED OUTPUT : correct time calculation to find series and the paralell
allow user input to find sequence and time intervals
*******************************************************************************************************************************
import multiprocessing as mp
import json
import time
import sys
def fib(n: int) -> int:
#Calculate n'th number in Fibonacci secuence
#return n'th number
if n < 2:
return 1
else:
return fib(n - 1) + fib(n - 2)
def parallel(num1: int, num2: int) -> list:
#Calculates Fibonacci sequence between num1 and num2 parallel
#return list of time taken by calculation, results
pool = mp.Pool(processes=mp.cpu_count())
a: float = time.time()
results = [pool.map(fib, (x for x in range(num1-1, num2)))]
b: float = time.time()
print("parallel: "+str(results))
print(f'parallel time:{b-a}')
return [b-a, results]
def normal(num1: int, num2: int) -> list:
#Calculates Fibonacci sequence between num1 and num2 series
#return time taken by calculation
results = []
for x in range(num1-1, num2):
results.append(fib(x))
a: float = time.time()
b: float = time.time()
print(f'series time:{b-a}')
return [b-a, results]
def resulter(num1: int, num2: int) -> tuple:
#Makes calculations series and parallel
#return num1, num2, series calc time, parallel calc time, series/parallel
b: float = parallel(num1, num2)[0]
a: float = normal(num1, num2)[0]
print(f'delta time (series/parallel):{a/b}')
return (num1, num2, a, b, a/b)
#Main that sets the values
def main():
data_start: int = 0
data_end: int = 0
print(f"cpu core count:{mp.cpu_count()}")
try:
data_start = int(sys.argv[1])
data_end = int(sys.argv[2])
print(f"values are {data_start} - {data_end}")
except:
data_start = 4
data_end = 20
print("values are 4 - 20")
return resulter(data_start, data_end)
if __name__ == '__main__':
main()
Step by step
Solved in 4 steps with 3 images
can you modify it to use more than two cpus, i compiled and ran and the time intervals are still showing 0.0 but everything else is done as desired thank you. i think it shows the 0.0 time becuase of my computer, i see on yours your cpu count is 8