Skip to content

Instantly share code, notes, and snippets.

@nhumrich
Last active September 27, 2018 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhumrich/26a0c915f567944fd02116493dd26c8c to your computer and use it in GitHub Desktop.
Save nhumrich/26a0c915f567944fd02116493dd26c8c to your computer and use it in GitHub Desktop.
async vs threading cpu bound benchmark
import asyncio
from multiprocessing.pool import ThreadPool
from timeit import timeit
from functools import partial
pool = ThreadPool(processes=400)
loop = asyncio.get_event_loop()
def fib(x):
if x in (0, 1):
return 1
a = fib(x-2)
b = fib(x-1)
return a + b
def fib_threaded(x):
if x in (0, 1):
return 1
a, b = pool.map(fib_threaded, (x-2, x-1))
return a + b
async def fib_async(x):
if x in (0, 1):
return 1
a, b = await asyncio.gather(fib_async(x-2),fib_async(x-1))
return a + b
y = 14
def normal():
return fib(y)
def threaded():
return fib_threaded(y)
def async_():
return loop.run_until_complete(fib_async(y))
num=100
a = timeit(normal, number=num)
b = timeit(async_, number=num)
c = timeit(threaded, number=num)
print('Normal:', a)
print('Async:', b)
print('Threaded:', c)
concurrently = 10
async def fib_async_wrap(x):
return fib(x)
def multiple_times():
for i in range(concurrently):
fib(y)
def threaded_at_once():
threads = [y for i in range(concurrently)]
results = pool.map(fib, threads)
def async_at_once():
coros = [fib_async_wrap(y) for i in range(concurrently)]
loop.run_until_complete(asyncio.gather(*coros))
num=100
a = timeit(multiple_times, number=num)
b = timeit(async_at_once, number=num)
c = timeit(threaded_at_once, number=num)
print()
print('--concurrently--')
print('Normal:', a)
print('Async:', b)
print('Threaded:', c)
@nhumrich
Copy link
Author

Updated snippet to be more correct. There was a bug in reporting, and how the numbers were run. New numbers updated in blog post: https://hackernoon.com/async-through-the-looking-glass-d69a0a88b661

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment