# sequential time

import time
import os
from timeit import default_timer as timer
from multiprocessing import Pool, cpu_count


# calculate fahrenheit
def fahrenheit(celsius):
    time.sleep(1)
    print("child process id:", os.getpid())
    return (celsius * 1.8) + 32

def main():

    start = timer()

    print(f'starting computations on {cpu_count()} cores')
    celsius = (37, 45, 30, 20, 10, 35, 25, 33, 29)
    print("parent process id:", os.getpid())
    res = list(map(fahrenheit, celsius))
    print(res)
    end = timer()
    print(f'elapsed time: {end - start}')


if __name__ == '__main__':
    main()



# multiprocessing time (parallel time)

import time
import os
from timeit import default_timer as timer
from multiprocessing import Pool, cpu_count

# calculate fahrenheit
def fahrenheit(celsius):
    time.sleep(1)
    print("child process id:", os.getpid())
    return (celsius * 1.8) + 32


def main():

    start = timer()

    print(f'starting computations on {cpu_count()} cores')

    celsius = (37, 45, 30, 20, 10, 35, 25, 33, 29)

    print("parent process id:", os.getpid())
    with Pool() as pool:
        res = pool.map(fahrenheit, celsius)
        print(res)

    end = timer()
    print(f'elapsed time: {end - start}s')
if __name__ == '__main__':
    main()