Last active
January 28, 2023 17:09
-
-
Save leemurus/c54b267a2b39f5d466fa6f388ae06774 to your computer and use it in GitHub Desktop.
How effective is asynchrony for synchronous tasks?
This file contains 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
import asyncio | |
import time | |
def sync_print_performance(func): | |
def wrapper(*args, **kwargs): | |
begin = time.monotonic() | |
result = func(*args, **kwargs) | |
duration = time.monotonic() - begin | |
print(f'Time execution of {func.__name__}: {duration * 1000:.2f} ms') | |
return result | |
return wrapper | |
def async_print_performance(func): | |
async def wrapper(*args, **kwargs): | |
begin = time.monotonic() | |
result = await func(*args, **kwargs) | |
duration = time.monotonic() - begin | |
print(f'Time execution of {func.__name__}: {duration * 1000:.2f} ms') | |
return result | |
return wrapper | |
def sync_mul(a: int, b: int) -> int: | |
return a * b | |
async def async_mul(a: int, b: int) -> int: | |
return a * b | |
@sync_print_performance | |
def execute_sync_funcs(exc_number: int): | |
for i in range(exc_number): | |
sync_mul(i, i * 2) | |
@async_print_performance | |
async def execute_async_coros(exc_number: int): | |
for i in range(exc_number): | |
await async_mul(i, i * 2) | |
@async_print_performance | |
async def execute_async_tasks(exc_number: int): | |
for i in range(exc_number): | |
await asyncio.create_task(async_mul(i, i * 2)) | |
@async_print_performance | |
async def execute_async_all_coros(exc_number: int): | |
tasks = [] | |
for i in range(exc_number): | |
tasks.append(async_mul(i, i * 2)) | |
await asyncio.gather(*tasks) | |
@async_print_performance | |
async def execute_async_all_tasks(exc_number: int): | |
tasks = [] | |
for i in range(exc_number): | |
tasks.append(asyncio.create_task(async_mul(i, i * 3))) | |
await asyncio.gather(*tasks) | |
async def main(): | |
exc_number = 10000 | |
execute_sync_funcs(exc_number) | |
await execute_async_coros(exc_number) | |
await execute_async_tasks(exc_number) | |
await execute_async_all_coros(exc_number) | |
await execute_async_all_tasks(exc_number) | |
if __name__ == '__main__': | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Time execution of execute_sync_funcs: 1.69 ms
Time execution of execute_async_coros: 3.21 ms
Time execution of execute_async_tasks: 602.52 ms
Time execution of execute_async_all_coros: 124.38 ms
Time execution of execute_async_all_tasks: 129.87 ms