Last active
May 13, 2018 14:10
-
-
Save miracle2k/69e43cda1dfbbe363575fbe9a7911995 to your computer and use it in GitHub Desktop.
asyncio.gather is quite slow
This file contains hidden or 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
""" | |
asyncio.gather might be getting faster: | |
See https://github.com/python/cpython/pull/4913 | |
""" | |
import os | |
NUM=int(os.environ.get('NUM', 1)) | |
def do_something_sync(num): | |
return num + 1 | |
async def do_something_async(num): | |
return num + 1 | |
####### | |
def test_sync(): | |
for x in range(1, 100000): | |
do_something_sync(x) | |
async def test_asyncio_gather(): | |
for x in range(1, 100000): | |
await asyncio.gather( | |
do_something_async(x) | |
) | |
async def test_async_direct(): | |
for x in range(1, 100000): | |
await do_something_async(x) | |
async def test_trio_nursery(): | |
for x in range(1, 100000): | |
async with trio.open_nursery() as n: | |
n.start_soon(do_something_async, x) | |
####### | |
if __name__ == '__main__': | |
import timeit, sys | |
mode = sys.argv[1] | |
if mode == 'sync': | |
print(timeit.timeit("test_sync()", setup="from __main__ import test_sync", number=NUM)) | |
if mode == 'asyncio': | |
import asyncio | |
def main_async(): | |
asyncio.get_event_loop().run_until_complete(test_async_direct()) | |
print(timeit.timeit("main_async()", setup="from __main__ import main_async", number=NUM)) | |
if mode == 'asyncio-gather': | |
import asyncio | |
def main_gather(): | |
asyncio.get_event_loop().run_until_complete(test_asyncio_gather()) | |
print(timeit.timeit("main_gather()", setup="from __main__ import main_gather", number=NUM)) | |
if mode == 'trio': | |
import trio | |
def main_trio(): | |
trio.run(test_async_direct) | |
print(timeit.timeit("main_trio()", setup="from __main__ import main_trio", number=NUM)) | |
if mode == 'trio-nursery': | |
import trio | |
def main_trio_nursery(): | |
trio.run(test_trio_nursery) | |
print(timeit.timeit("main_trio_nursery()", setup="from __main__ import main_trio_nursery", number=NUM)) | |
""" | |
Python 3.6.3, trio==0.4.0 (git checkout) | |
$ python speedtest.py sync | |
0.013295362994540483 | |
$ python speedtest.py async | |
0.024491631003911607 | |
$ python speedtest.py asyncio-gather | |
3.644775936001679 | |
$ python speedtest.py trio | |
0.02168167599302251 | |
$ NUM=1 python speedtest.py trio-nursery | |
9.267848733012215 | |
Approximate number of calls (NUM set to this value) to copmlete in 1s. | |
This is ignoring the range loop, so it includes the cost of setting up the event loop, | |
which is the reason why trio is so slow. | |
- sync: 7000.000 | |
- asyncio: 40.000 | |
- asyncio-gather: 16.000 | |
- trio: 1200 (this is probably ac | |
- trio-nursery: 1100 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment