Created
November 23, 2024 21:16
-
-
Save 1st1/5afaad7dc0b39629cfa871853ae39fef to your computer and use it in GitHub Desktop.
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
import asyncio | |
import time | |
WARMUP_RUNS = 5 | |
BENCH_RUNS = 20 | |
FACTOR = 200 | |
num_tasks = 0 | |
async def i5(): | |
await asyncio.sleep(0) | |
await asyncio.sleep(0) | |
await asyncio.sleep(0) | |
async def i4(): | |
global num_tasks | |
for _ in range(FACTOR): | |
num_tasks += 1 | |
await asyncio.create_task(i5()) | |
async def i3(): | |
await asyncio.sleep(0) | |
await i4() | |
async def i2(): | |
await asyncio.sleep(0) | |
await i3() | |
async def i1(): | |
global num_tasks | |
async with asyncio.TaskGroup() as g: | |
for _ in range(FACTOR): | |
g.create_task(i2()) | |
num_tasks += 1 | |
for _ in range(WARMUP_RUNS): | |
asyncio.run(i1()) | |
print('w', end='', flush=True) | |
started = time.monotonic() | |
for _ in range(BENCH_RUNS): | |
asyncio.run(i1()) | |
print('b', end='', flush=True) | |
ended = time.monotonic() | |
print() | |
num_tasks //= (WARMUP_RUNS + BENCH_RUNS) | |
print( | |
f'time={(ended - started) / BENCH_RUNS} ' | |
f'{num_tasks=}' | |
) | |
# normal: | |
# | |
# ~/d/p/cpython (main %) » ./python.exe bench.py | |
# wwwwwbbbbbbbbbbbbbbbbbbbb | |
# time=0.12399349374973098 num_tasks=40200 | |
# ~/d/p/cpython (main %) » ./python.exe bench.py | |
# wwwwwbbbbbbbbbbbbbbbbbbbb | |
# time=0.12461646664960427 num_tasks=40200 | |
# ~/d/p/cpython (main %) » ./python.exe bench.py | |
# wwwwwbbbbbbbbbbbbbbbbbbbb | |
# time=0.12489991874972475 num_tasks=40200 | |
# stack: | |
# | |
# ~/d/p/cpython (stack %) » ./python.exe bench.py | |
# wwwwwbbbbbbbbbbbbbbbbbbbb | |
# time=0.12497415624966379 num_tasks=40200 | |
# ~/d/p/cpython (stack %) » ./python.exe bench.py | |
# wwwwwbbbbbbbbbbbbbbbbbbbb | |
# time=0.12572984584985533 num_tasks=40200 | |
# ~/d/p/cpython (stack %) » ./python.exe bench.py | |
# wwwwwbbbbbbbbbbbbbbbbbbbb | |
# time=0.1256675312499283 num_tasks=40200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment