Created
July 17, 2018 12:07
-
-
Save vxgmichel/75467f62673ad34a1567e5d66c6c3925 to your computer and use it in GitHub Desktop.
Test parallelism in python
This file contains hidden or 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
"""Test parallelism in python. | |
$ \time -f "%e s - %P" python3 par.py 1 > /dev/null | |
6.74 s - 100% | |
$ \time -f "%e s - %P" python3 par.py 4 > /dev/null | |
1.87 s - 385% | |
$ bc -l <<< "6.74/1.87" | |
3.60... | |
""" | |
import sys | |
from concurrent.futures import ProcessPoolExecutor | |
def solve(x): | |
def slow_pow(a, b, m, r=1): | |
for _ in range(b): | |
r *= a | |
r %= m | |
return r | |
return slow_pow(x, 10**7, 982451653) | |
def main(): | |
max_workers = int(sys.argv[1]) | |
with ProcessPoolExecutor(max_workers) as executor: | |
for result in executor.map(solve, range(100, 108)): | |
print(result) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment