Skip to content

Instantly share code, notes, and snippets.

@1st1
Last active August 29, 2015 14:07
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 1st1/b38ac6785cb01a679722 to your computer and use it in GitHub Desktop.
Save 1st1/b38ac6785cb01a679722 to your computer and use it in GitHub Desktop.
import asyncio
import time
import statistics
import heapq
ITERS = 2000
NUMBER_OF_TASKS = 100000
loop = asyncio.get_event_loop()
print('NUMBER_OF_TASKS', NUMBER_OF_TASKS)
def cb(self):
pass
for x in range(NUMBER_OF_TASKS):
th = loop.call_later(3600, cb)
if x % 2 == 1:
th.cancel()
orig_data = loop._scheduled
results = {
1: [],
2: []
}
bench_start = time.time()
switch = 1
for x in range(ITERS):
start = time.monotonic()
loop._scheduled = orig_data
if switch == 1:
for handle in loop._scheduled:
if handle._cancelled:
handle._scheduled = False
loop._scheduled = [x for x in loop._scheduled if not x._cancelled]
heapq.heapify(loop._scheduled)
end = time.monotonic()
results[switch].append(end - start)
switch = 2
elif switch == 2:
new_scheduled = []
for handle in loop._scheduled:
if handle._cancelled:
handle._scheduled = False
else:
new_scheduled.append(handle)
loop._scheduled = new_scheduled
heapq.heapify(loop._scheduled)
end = time.monotonic()
results[switch].append(end - start)
switch = 1
print('\rITERATIONS -> {} out of {}'.format(x+1, ITERS), end='')
print()
print('2 loops: ', statistics.median(results[1]))
print('1 loop: ', statistics.median(results[2]))
print('TOTAL_BENCH_TIME', time.time() - bench_start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment