Skip to content

Instantly share code, notes, and snippets.

@maxfischer2781
Created October 27, 2021 11:31
Show Gist options
  • Save maxfischer2781/36af2934b39264984a0c1b05d602a428 to your computer and use it in GitHub Desktop.
Save maxfischer2781/36af2934b39264984a0c1b05d602a428 to your computer and use it in GitHub Desktop.
Script for measuring the overhead of asyncio.gather
import asyncio
import time
async def no_op():
return
async def main_gather(count):
await asyncio.gather(*(asyncio.gather(no_op()) for _ in range(count)))
async def main_plain(count):
await asyncio.gather(*(no_op() for _ in range(count)))
print("Repetitions")
print(" | gather time vs plain time")
print(" | | gather overhead")
for i in range(10, 501, 10):
start_plain = time.time()
asyncio.run(main_plain(i))
stop_plain = time.time()
start_gather = time.time()
asyncio.run(main_gather(i))
stop_gather = time.time()
delta = (stop_gather-start_gather) - (stop_plain-start_plain)
print(
f"{i:3d}: {stop_gather-start_gather:.3f} vs {stop_plain-start_plain:.3f}",
f"[{100 + delta / (stop_plain-start_plain) * 100:3.0f}% of noop]"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment