Skip to content

Instantly share code, notes, and snippets.

@dstuebe
Created March 12, 2013 19:30
Show Gist options
  • Save dstuebe/5146177 to your computer and use it in GitHub Desktop.
Save dstuebe/5146177 to your computer and use it in GitHub Desktop.
Coroutine examples and timing
For n= 100000
$ python test_coroutines.py
Coroutine Loop Result: '14.1121757499'; loop time (ms): 393.39017868
Numpy Loop Result: '14.1121757499'; loop time (ms): 26105.5378914
Numpy Dot Result: '14.1121757499'; dot time (ms): 0.148057937622
# can't pass a scalar by reference, try passing an array of size 1
Coroutine Loop No Result: '[ 14.11217575]'; loop time (ms): 438.895225525
#!/usr/bin/env python
"""
python dot product using coroutines
Goal is to test coroutine performance and syntactic sugar
"""
import numpy
import itertools
import time
# make some data
n = 100000
a = numpy.random.randn(n)
b = numpy.random.randn(n)
def coroutine(func):
def start(*args, **kwargs):
g = func(*args, **kwargs)
g.next()
return g
return start
@coroutine
def mult(target=None):
if target is None:
raise RuntimeError('Must specify targe coroutine!')
result = numpy.float64(0.0)
while True:
(a, b) = (yield result)
result = target.send(a*b)
@coroutine
def add():
result = numpy.float64(0.0)
while True:
m = (yield result)
result += m
dot_product_process = mult(add())
it = numpy.nditer([a, b])
tic = time.time()
for (a_val,b_val) in it:
dot_product = dot_product_process.send((a_val, b_val))
toc = time.time()
print "Coroutine Loop Result: '%s'; loop time (ms): %s" % (dot_product, 1000*(toc-tic))
it = numpy.nditer([a, b])
result = numpy.float64(0.0)
tic = time.time()
for (a_val,b_val) in it:
result += a*b
toc = time.time()
print "Numpy Loop Result: '%s'; loop time (ms): %s" % (dot_product, 1000*(toc-tic))
tic = time.time()
dot_product = numpy.dot(a,b)
toc = time.time()
print "Numpy Dot Result: '%s'; dot time (ms): %s" % (dot_product, 1000*(toc-tic))
# Coroutine processes without using return...
@coroutine
def mult_noresult(target=None):
if target is None:
raise RuntimeError('Must specify targe coroutine!')
while True:
(a, b) = (yield)
result = target.send(a*b)
@coroutine
def add_noresult(result):
while True:
m = (yield)
result[0] += m
result = numpy.zeros((1,))
dot_product_process_noresult = mult_noresult(add_noresult(result))
it = numpy.nditer([a, b])
tic = time.time()
for (a_val,b_val) in it:
dot_product_process_noresult.send((a_val, b_val))
toc = time.time()
print "Coroutine Loop No Result: '%s'; loop time (ms): %s" % (result, 1000*(toc-tic))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment