Created
April 4, 2014 23:15
-
-
Save armonge/9984898 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 itertools | |
import random | |
import math | |
def map_d(c): | |
return math.hypot(random.random(), random.random()) | |
def reduce_d(count_inside, d): | |
if d < 1: | |
return count_inside + 1 | |
return count_inside | |
count = 100000000 | |
d_list = itertools.imap(map_d, xrange(0, count)) | |
count_inside = reduce(reduce_d, d_list ) | |
print 4.0 * count_inside / count |
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 itertools | |
import random | |
import math | |
from celery import Celery | |
from celery import group | |
app = Celery('hello', broker='redis://localhost:6379/', backend='redis://localhost') | |
def map_d(c): | |
return math.hypot(random.random(), random.random()) | |
@app.task(name='count_list') | |
def count_list(count): | |
d_list = itertools.imap(map_d, xrange(0, count)) | |
count_inside = sum(1 for d in d_list if d < 1) | |
return count_inside | |
if __name__ == '__main__': | |
count = 1000000 | |
tasks = 1000 | |
job = group(count_list.s(count) for i in range(0, tasks)).apply_async() | |
count_list = job.get() | |
total_count_inside = sum(count_list) | |
print 4.0 * total_count_inside / (count * tasks) |
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
from concurrent import futures | |
import itertools | |
import random | |
import math | |
def map_d(c): | |
return math.hypot(random.random(), random.random()) | |
def reduce_d(count_inside, d): | |
if d < 1: | |
return count_inside + 1 | |
return count_inside | |
count = 100000 | |
with futures.ProcessPoolExecutor(max_workers=4) as executor: | |
d_list = executor.map(map_d, xrange(0, count)) | |
count_inside = sum(1 for d in d_list if d < 1) | |
print 4.0 * count_inside / count |
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
amqp==1.4.4 | |
anyjson==0.3.3 | |
argparse==1.2.1 | |
billiard==3.3.0.16 | |
celery==3.1.10 | |
celery-with-redis==3.0 | |
futures==2.1.6 | |
kombu==3.0.14 | |
pytz==2014.2 | |
redis==2.9.1 | |
wsgiref==0.1.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment