Skip to content

Commit

Permalink
Merge pull request #242 from MSeifert04/ilen-performance
Browse files Browse the repository at this point in the history
Improve performance of ilen function.
  • Loading branch information
bbayles committed Nov 28, 2018
2 parents f82887c + 5161c34 commit 73f76fd
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions more_itertools/more.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,12 @@ def ilen(iterable):
This consumes the iterable, so handle with care.
"""
length = 0
for length, _ in enumerate(iterable, 1):
pass
return length
# This approach was selected because benchmarks showed it's likely the
# fastest of the known implementations at the time of writing.
# See GitHub tracker: #236, #230.
counter = count()
deque(zip(iterable, counter), maxlen=0)
return next(counter)


def iterate(func, start):
Expand Down

0 comments on commit 73f76fd

Please sign in to comment.