Created
March 17, 2018 13:53
-
-
Save vincenzomanzoni/eec971bff2fe206dc3ec2894c19ca11e to your computer and use it in GitHub Desktop.
Playing with Python Multiprocessing
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 multiprocessing | |
import time | |
from multiprocessing import Pool | |
def f(x): | |
return x**2 | |
print("Number of CPUs available: ", multiprocessing.cpu_count()) | |
print("Start parallel computation") | |
start = time.clock() | |
with Pool() as p: | |
l = p.map(f, range(0, 10000000)) | |
end = time.clock() | |
print("End parallel computation: %.2gs" % (end - start)) | |
print("Start sequential computation") | |
start = time.clock() | |
list(map(f, range(0, 10000000))) | |
end = time.clock() | |
print("End parallel computation: %.2gs" % (end - start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment