-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorial.py
30 lines (23 loc) · 843 Bytes
/
factorial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/use/bin/env python3.8
### Standard Packages ###
import math
import time
from argparse import ArgumentParser, Namespace
from joblib import Parallel, delayed
def main():
parser: ArgumentParser = ArgumentParser()
parser.add_argument('--parallel', '-p', action='store_true', help='Run in parallel if flag enabled')
parser.add_argument('--jobs', '-j', type=int, default=-1, help='Number of parallel jobs')
args: Namespace = parser.parse_args()
results: list
t1: float = time.time()
if args.parallel:
### With parallelization ###
results = Parallel(n_jobs=args.jobs)(delayed(math.factorial)(x) for x in range(10_000))
else:
### Without parallelization ###
results = [math.factorial(x) for x in range(10_000)]
t2: float = time.time()
print(f'Runtime: { t2 -t1 }')
if __name__ == '__main__':
main()