Skip to content

Instantly share code, notes, and snippets.

@schlamar
Created July 10, 2013 14:33
Show Gist options
  • Save schlamar/5966805 to your computer and use it in GitHub Desktop.
Save schlamar/5966805 to your computer and use it in GitHub Desktop.
tulip.coroutine vs. tulip.task
import timeit
import tulip
loop = tulip.get_event_loop()
def async_func(value):
p = tulip.Future()
loop.call_later(0.01, p.set_result, value)
return p
@tulip.coroutine
def sub_sub():
yield from async_func(1)
yield from async_func(1)
@tulip.coroutine
def sub():
yield from async_func(1)
yield from async_func(1)
yield from sub_sub()
@tulip.coroutine
def main():
yield from async_func(1)
yield from sub()
yield from sub()
def bench1():
loop.run_until_complete(main())
@tulip.task
def sub_sub_t():
yield from async_func(1)
yield from async_func(1)
@tulip.task
def sub_t():
yield from async_func(1)
yield from async_func(1)
yield from sub_sub_t()
@tulip.task
def main_t():
yield from async_func(1)
yield from sub_t()
yield from sub_t()
def bench2():
loop.run_until_complete(main_t())
if __name__ == '__main__':
print(timeit.timeit('bench1()', 'from __main__ import bench1', number=400))
print(timeit.timeit('bench2()', 'from __main__ import bench2', number=400))
# 56.1653537544945
# 56.156518258656334
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment