Skip to content

Instantly share code, notes, and snippets.

@ra0x3
Created November 5, 2019 19:45
Show Gist options
  • Save ra0x3/f728f60f1c643e7e46846c9e85f9d35b to your computer and use it in GitHub Desktop.
Save ra0x3/f728f60f1c643e7e46846c9e85f9d35b to your computer and use it in GitHub Desktop.
Compare factorization sequentially vs concurrently
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