Created
March 20, 2017 17:17
-
-
Save MSeifert04/c0dfd2c07ca2ffcd35f7384380f08a37 to your computer and use it in GitHub Desktop.
This file contains 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
from iteration_utilities import roundrobin | |
from itertools import chain, cycle, islice | |
def shuffle_mseifert(L): | |
return list(roundrobin(L[:len(L)//2], L[len(L)//2:])) | |
def shuffle_mitch(l): | |
return list(chain.from_iterable(zip(l[:len(l)//2], l[len(l)//2:]))) | |
def shuffle_user2357112(l): | |
result = [None] * len(l) | |
# Put the first half of l in the even indices of result | |
result[::2] = l[:len(l)//2] | |
# Put the second half of l in the odd indices of result | |
result[1::2] = l[len(l)//2:] | |
return result | |
def shuffle_itertools(L): | |
return list(roundrobin_itertools(L[:len(L)//2], L[len(L)//2:])) | |
def roundrobin_itertools(*iterables): | |
pending = len(iterables) | |
nexts = cycle(iter(it).__next__ for it in iterables) | |
while pending: | |
try: | |
for next in nexts: | |
yield next() | |
except StopIteration: | |
pending -= 1 | |
nexts = cycle(islice(nexts, pending)) | |
# Timing | |
results = {shuffle_user2357112: [], | |
shuffle_mitch: [], | |
shuffle_mseifert: [], | |
shuffle_itertools: []} | |
for i in range(1, 7): | |
print('-'*20, 10**i, '-'*20) | |
for func in results: | |
lst = list(range(10**i)) | |
print(func) | |
res = %timeit -o func(lst) | |
results[func].append(res) | |
# Plotting | |
import matplotlib.pyplot as plt | |
plt.figure() | |
for func, timinglist in results.items(): | |
plt.plot([10**i for i in range(1, 7)], | |
[timing.best for timing in timinglist], | |
label=func.__name__) | |
plt.xlabel('items in list') | |
plt.ylabel('time [s]') | |
plt.loglog() | |
plt.legend() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment