Issue 27751: Itertools -> Recipes -> pairwise() (original) (raw)

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='')