Open sc1101 opened 4 years ago
from math import hypot from random import random from scoop import futures import time
def test(tries): return sum(hypot(random(), random()) < 1 for _ in range(tries))
def calcPi(nbFutures, tries): expr = futures.map(test, [tries] nbFutures) return 4. sum(expr) / float(nbFutures * tries)
if name == "main": nbFutures = 3000 tries = 5000
bt = time.time()
# Divide the work among fewer processes to minimize overhead
chunk_size = 10
expr = futures.map(test, [chunk_size] * (nbFutures * tries // chunk_size))
print("pi = {}".format(4. * sum(expr) / float(nbFutures * tries)))
print('time:', time.time() - bt)
Hello! I run the example in the document, the code is as the following:
"""test.py""" from math import hypot from random import random from scoop import futures import time def test(tries): return sum(hypot(random(), random()) < 1 for _ in range(tries)) def calcPi(nbFutures, tries): expr = futures.map(test, [tries] * nbFutures) return 4. * sum(expr) / float(nbFutures * tries) if __name__ == "__main__": bt = time.time() print("pi = {}".format(calcPi(3000, 5000))) print('time:', time.time() - bt)
I run this code in the windows cmd with the command "python -m scoop test.py", however, it cost 33 seconds. Then I used "map" instead of "futures.map", it just cost 4 seconds.
What should I do to let SCOOP be able to speed up my code?
Thanks.