New submission from YoSTEALTH:
# Link: https://docs.python.org/3/library/itertools.html#itertools-recipes
# Function pairwise() in Itertools -> Recipes could be improved!? Here is the
code:
import time
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
def new_pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
return zip(iterable, iterable[1:])
combine = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
if __name__ == '__main__':
start_time = time.time()
# Current
print('Current:')
print(list(pairwise(combine)))
# output: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8),
(8, 9)]
print()
# New
print('New:')
print(list(new_pairwise(combine)))
# output: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8),
(8, 9)]
# Benchmark
# for _ in range(1000000):
# list(pairwise(combine)) # Time: 2.61199975
# list(new_pairwise(combine)) # Time: 1.14828038
print('\n\nTime: {}'.format(round(time.time() - start_time, 8)), end='')
----------
messages: 272572
nosy: YoSTEALTH
priority: normal
severity: normal
status: open
title: Itertools -> Recipes -> pairwise()
type: performance
versions: Python 3.5
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue27751>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com