Last active
November 23, 2023 09:04
-
-
Save homm/2a84341987d20c97615c520af3defaf5 to your computer and use it in GitHub Desktop.
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 time | |
import collections | |
import functools | |
import itertools | |
TIMES = 200 | |
N = 200 | |
M = 10 | |
def timeit(fn, n=TIMES): | |
@functools.wraps(fn) | |
def wrapper(*args, **kwargs): | |
results = [] | |
for _ in range(n): | |
it = fn(*args, **kwargs) | |
start_time = time.time() | |
counter = itertools.count() | |
collections.deque(zip(it, counter), maxlen=0) | |
assert next(counter) == N + M | |
results.append(time.time() - start_time) | |
print('>>>', fn.__name__, sorted(results)[n // 3] * 1000) | |
return wrapper | |
def yilder_from(wrapp): | |
yield from wrapp | |
yield None | |
def yilder_for(wrapp): | |
for item in wrapp: | |
yield item | |
yield None | |
@timeit | |
def test_yilder(): | |
return iter(range(N + M)) | |
@timeit | |
def test_yilder_from(): | |
it = range(N) | |
for _ in range(M): | |
it = yilder_from(it) | |
return it | |
@timeit | |
def test_yilder_for(): | |
it = range(N) | |
for _ in range(M): | |
it = yilder_for(it) | |
return it | |
if __name__ == '__main__': | |
test_yilder() | |
test_yilder_from() | |
test_yilder_for() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment