Created
November 5, 2019 19:45
-
-
Save ra0x3/f728f60f1c643e7e46846c9e85f9d35b to your computer and use it in GitHub Desktop.
Compare factorization sequentially vs concurrently
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import random | |
from typing import * | |
N_TASKS = 20 | |
UPPER_BOUND = 10e12 | |
LOWER_BOUND = 10e10 | |
def sequential_factorization(): | |
output = [] | |
tasks = [Task(i, get_prime_factors, random.randint(LOWER_BOUND, UPPER_BOUND)) for i in range(N_TASKS)] | |
for task in tasks: | |
output.append(task.run()) | |
return output | |
def concurrent_factorization(): | |
manager = ProcessManager() | |
manager.start() | |
for i in range(N_TASKS): | |
manager.add_task(Task(i, get_prime_factors, random.randint(LOWER_BOUND, UPPER_BOUND))) | |
manager.add_terminator() | |
manager.join() | |
results = manager.fetch_results() | |
return results | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment