Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import timeit
from mytools import *
from mytools_other import *
import more_itertools_on_code as more_itertools
#
#
print(
'slide_each_simple (iterator)',
timeit.timeit(
stmt='slide_each_simple(range(1000), 3)',
globals=globals()
)
)
print(
'slide_each_faster_A (iterator)',
timeit.timeit(
stmt='slide_each_faster_A(range(1000), 3)',
globals=globals()
)
)
print(
'slide_each_faster_B (iterator)',
timeit.timeit(
stmt='slide_each_faster_B(range(1000), 3)',
globals=globals()
)
)
print(
'slide_each_quanon (iterator)',
timeit.timeit(
stmt='slide_each_quanon(range(1000), 3)',
globals=globals()
)
)
print(
'more-itertools.windowed (iterator)',
timeit.timeit(
stmt='more_itertools.windowed(range(1000), 3)',
globals=globals()
)
)
#
#
t_ls = [i for i in range(1000)]
print(
'slide_each_simple (list)',
timeit.timeit(
stmt='slide_each_simple(t_ls, 3)',
globals=globals()
)
)
print(
'slide_each_faster_A (list)',
timeit.timeit(
stmt='slide_each_faster_A(t_ls, 3)',
globals=globals()
)
)
print(
'slide_each_faster_B (list)',
timeit.timeit(
stmt='slide_each_faster_B(t_ls, 3)',
globals=globals()
)
)
print(
'slide_each_quanon (list)',
timeit.timeit(
stmt='slide_each_quanon(t_ls, 3)',
globals=globals()
)
)
print(
'more-itertools.windowed (list)',
timeit.timeit(
stmt='more_itertools.windowed(t_ls, 3)',
globals=globals()
)
)
#
#
t_tp = tuple(i for i in range(1000))
print(
'slide_each_simple (tuple)',
timeit.timeit(
stmt='slide_each_simple(t_tp, 3)',
globals=globals()
)
)
print(
'slide_each_faster_A (tuple)',
timeit.timeit(
stmt='slide_each_faster_A(t_tp, 3)',
globals=globals()
)
)
print(
'slide_each_faster_B (tuple)',
timeit.timeit(
stmt='slide_each_faster_B(t_tp, 3)',
globals=globals()
)
)
print(
'slide_each_quanon (tuple)',
timeit.timeit(
stmt='slide_each_quanon(t_tp, 3)',
globals=globals()
)
)
print(
'more-itertools.windowed (tuple)',
timeit.timeit(
stmt='more_itertools.windowed(t_tp, 3)',
globals=globals()
)
)
#
# Copied from https://github.com/erikrose/more-itertools/blob/master/more_itertools/more.py
#
from collections import deque
def windowed(seq, n, fillvalue=None, step=1):
if n < 0:
raise ValueError('n must be >= 0')
if n == 0:
yield tuple()
return
if step < 1:
raise ValueError('step must be >= 1')
it = iter(seq)
window = deque([], n)
append = window.append
# Initial deque fill
for _ in range(n):
append(next(it, fillvalue))
yield tuple(window)
# Appending new items to the right causes old items to fall off the left
i = 0
for item in it:
append(item)
i = (i + 1) % step
if i % step == 0:
yield tuple(window)
# If there are items from the iterable in the window, pad with the given
# value and emit them.
if (i % step) and (step - i < n):
for _ in range(step - i):
append(fillvalue)
yield tuple(window)
from itertools import tee
def slide_each_quanon(iterable, n):
its = tee(iterable, n)
for i in range(1, n):
for j in range(i, n):
next(its[j])
return zip(*its)
from copy import copy
def split_each(iterable, n):
return zip(
* [iter(iterable)]*n
)
def slide_each_simple(iterable, n):
sequence = tuple(iterable)
return zip(
* [sequence[i:] for i in range(n)]
)
def slide_each_faster_A(iterable, n):
iterator = iter(iterable)
return zip(
* [
[copy(iterator), next(iterator)][0]
for _ in range(n)
]
)
def slide_each_faster_B(iterable, n):
def _func(iterator, n):
for _ in range(n):
yield copy(iterator)
next(iterator)
return zip(
*_func(iter(iterable), n)
)
slide_each = slide_each_faster_A
slide_each_gen = slide_each_faster_A
slide_each_seq = slide_each_simple
from copy import copy
import unittest
from mytools import *
from mytools_other import *
import more_itertools_on_code as more_itertools
class TestMySlideTools(unittest.TestCase):
def setUp(self):
self._t_it = range(5)
self.ans_it = [(0, 1, 2), (1, 2, 3), (2, 3, 4)]
self.t_ls = [3, 1, 4, 1, 5]
self.ans_ls = [(3, 1, 4), (1, 4, 1), (4, 1, 5)]
@property
def t_it(self):
return copy(self._t_it)
def test_slide_each_simple(self):
self.assertEqual(
list(
slide_each_simple(self.t_it, 3)
),
self.ans_it
)
self.assertEqual(
list(
slide_each_simple(self.t_ls, 3)
),
self.ans_ls
)
def test_slide_each_faster_A(self):
self.assertEqual(
list(
slide_each_faster_A(self.t_it, 3)
),
self.ans_it
)
self.assertEqual(
list(
slide_each_faster_A(self.t_ls, 3)
),
self.ans_ls
)
def test_slide_each_faster_B(self):
self.assertEqual(
list(
slide_each_faster_B(self.t_it, 3)
),
self.ans_it
)
self.assertEqual(
list(
slide_each_faster_B(self.t_ls, 3)
),
self.ans_ls
)
def test_slide_each_quanon(self):
self.assertEqual(
list(
slide_each_quanon(self.t_it, 3)
),
self.ans_it
)
self.assertEqual(
list(
slide_each_quanon(self.t_ls, 3)
),
self.ans_ls
)
def test_windowed(self):
self.assertEqual(
list(
more_itertools.windowed(self.t_it, 3)
),
self.ans_it
)
self.assertEqual(
list(
more_itertools.windowed(self.t_ls, 3)
),
self.ans_ls
)
class TestMySplitTools(unittest.TestCase):
def setUp(self):
self._t_it = range(6)
self.ans_it = [(0, 1, 2), (3, 4, 5)]
self.t_ls = [3, 1, 4, 1, 5, 9]
self.ans_ls = [(3, 1, 4), (1, 5, 9)]
@property
def t_it(self):
return copy(self._t_it)
def test_split_each(self):
self.assertEqual(
list(
split_each(self.t_it, 3)
),
self.ans_it
)
self.assertEqual(
list(
split_each(self.t_ls, 3)
),
self.ans_ls
)
if __name__ == '__main__':
MORE_SPECIFIC = 2
unittest.main(verbosity=MORE_SPECIFIC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.