2024-heraklion-parallel-python/exercises/exerciseB/numerical_integration_solution.py
2024-08-29 18:23:32 +03:00

31 lines
873 B
Python

import sys
from numerical_integration import compute_error
def main(arg):
ns = [10_000, 25_000, 50_000, 75_000]
match arg:
case 'for':
errors = []
for n in ns:
errors += [compute_error(n)]
case 'lc':
errors = [compute_error(n) for n in ns]
case 'map':
errors = list(map(compute_error, ns))
case 'mp':
from multiprocessing import Pool as ProcessPool
with ProcessPool() as pool:
errors = pool.map(compute_error, ns)
case 'mt':
from multiprocessing.pool import ThreadPool
with ThreadPool(10) as pool:
errors = pool.map(compute_error, ns)
for n, e in zip(ns, errors):
print(f'{n} {e:.8%}')
if __name__ == '__main__':
arg = (sys.argv[1:] + ['for'])[0]
main(arg)