back to Index

coroutine_spin_test

2014-07-19

testing performance of coroutines


In [38]:
# Cross-notebook include shim
with open("nbinclude.ipynb") as nbinclude_f: # don't rename nbinclude_f
    import IPython.nbformat.current
    get_ipython().run_cell(IPython.nbformat.current.read(nbinclude_f, 'json').worksheets[0].cells[0].input)

In [39]:
class CoroutineSpinner:
    def __init__(self, coroutine):
        self.coroutine=coroutine
        self.running=False
    def run(self):
        self.running=True
        while(self.running):
            delay=next(self.coroutine)
            if delay < 0:
                return
            time.sleep(delay)

In [40]:
import time
import numpy as np
times=np.zeros(shape=[100], dtype=np.float32)
def time_test():
    i=0
    last_time=time.time()
    while True:
        now=time.time()
        times[i]=now-last_time
#         print i, times[i]
        last_time=now
        i+=1
        if i == 100:
            yield -1
        yield .01

In [41]:
cs=CoroutineSpinner(time_test())

In [42]:
cs.run()

In [43]:
%matplotlib inline

In [44]:
import matplotlib.pyplot as plt

In [45]:
plt.plot(times)
plt.xlabel


Out[45]:
<function matplotlib.pyplot.xlabel>

In [37]:


In [ ]: