Last active
February 6, 2021 02:10
-
-
Save eriky/f58b85c2d74981e6e3651d557643a0d0 to your computer and use it in GitHub Desktop.
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 time | |
import multiprocessing | |
# A CPU heavy calculation, just | |
# as an example. This can be | |
# anything you like | |
def heavy(n, myid): | |
for x in range(1, n): | |
for y in range(1, n): | |
x**y | |
print(myid, "is done") | |
def doit(n): | |
heavy(500, n) | |
def pooled(n): | |
# By default, our pool will have | |
# numproc slots | |
with multiprocessing.Pool() as pool: | |
pool.map(doit, range(n)) | |
if __name__ == "__main__": | |
start = time.time() | |
pooled(80) | |
end = time.time() | |
print("Took: ", end - start) | |
# This takes about 23 seconds as well. | |
# That's half of the threaded version. | |
# My pc has two cores, so it's exactly | |
# what we would expect. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment