Merged
Conversation
Contributor
|
Unless you dislike the dependency again, you could shorten your return zip(chain.from_iterable(map(repeat, counter, repeat(len(seq)))), cycle(seq))to: return zip(repeat_each(counter, len(seq)), cycle(seq))(Another one I wrote was |
Contributor
|
Not a suggestion, just curiosity: I also tried return chain.from_iterable(
zip_longest((), seq, fillvalue=i)
for i in counter
)Times with your test case: But as expected, it's slow for a short iterable: benchmark scriptdef original(iterable, n=None):
iterable = tuple(iterable)
if not iterable:
return iter(())
counter = count() if n is None else range(n)
return ((i, item) for i in counter for item in iterable)
def zip_chain(iterable, n=None):
seq = tuple(iterable)
if not seq:
return iter(())
counter = count() if n is None else range(n)
return zip(
chain.from_iterable(map(repeat, counter, repeat(len(seq)))),
cycle(seq)
)
def chain_zip(iterable, n=None):
seq = tuple(iterable)
if not seq:
return iter(())
counter = count() if n is None else range(n)
return chain.from_iterable(
map(zip, map(repeat, counter), repeat(seq))
)
def chain_ziplongest(iterable, n=None):
seq = tuple(iterable)
if not seq:
return iter(())
counter = count() if n is None else range(n)
return chain.from_iterable(
zip_longest((), seq, fillvalue=i)
for i in counter
)
funcs = [
original,
zip_chain,
chain_zip,
chain_ziplongest,
]
from itertools import *
from collections import deque
from timeit import timeit
from statistics import mean, stdev
import sys
import random
# Correctness
for f in funcs:
print(*f('AB', 3))
# Speed
consume = deque(maxlen=0).extend
args = 'range(1000), 3'
args = 'range(3), 1000'
times = {f: [] for f in funcs}
def stats(f):
ts = [t * 1e6 for t in sorted(times[f])[:5]]
return f'{mean(ts):5.1f} ± {stdev(ts):3.1f} μs '
for _ in range(100):
for f in random.sample(funcs, len(funcs)):
t = timeit(f'consume(count_cycle({args}))', 'from __main__ import f as count_cycle, consume', number=10) / 10
times[f].append(t)
print(args)
for f in funcs:
print(stats(f), f.__name__)
print('\nPython:', sys.version) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prefer an iterator stack to nested generator expressions.
Baseline:
Patched: