import time print('Last updated: %s' %time.strftime('%d/%m/%Y')) import collections a = collections.deque([1,2,3]) a.rotate(2) print(a) import timeit import collections from copy import copy labels = ['.append()', '.insert(0,x)\n.appendleft(x)', '.pop()', '.pop(0)\n.popleft()', '.reverse()'] list_time, deque_time = [], [] n = 10000 l = [i for i in range(n)] d = collections.deque([i for i in range(n)]) l_cp, d_cp = copy(l), copy(d) list_time.append(min(timeit.Timer('l_cp.append(1)', 'from __main__ import l_cp').repeat(repeat=3, number=1000))) deque_time.append(min(timeit.Timer('d_cp.append(1)', 'from __main__ import d_cp').repeat(repeat=3, number=1000))) l_cp, d_cp = copy(l), copy(d) list_time.append(min(timeit.Timer('l_cp.insert(0,1)', 'from __main__ import l_cp').repeat(repeat=3, number=1000))) deque_time.append(min(timeit.Timer('d_cp.appendleft(1)', 'from __main__ import d_cp').repeat(repeat=3, number=1000))) l_cp, d_cp = copy(l), copy(d) list_time.append(min(timeit.Timer('l_cp.pop()', 'from __main__ import l_cp').repeat(repeat=3, number=1000))) deque_time.append(min(timeit.Timer('d_cp.pop()', 'from __main__ import d_cp').repeat(repeat=3, number=1000))) l_cp, d_cp = copy(l), copy(d) list_time.append(min(timeit.Timer('l_cp.pop(0)', 'from __main__ import l_cp').repeat(repeat=3, number=1000))) deque_time.append(min(timeit.Timer('d_cp.popleft()', 'from __main__ import d_cp').repeat(repeat=3, number=1000))) l_cp, d_cp = copy(l), copy(d) list_time.append(min(timeit.Timer('l_cp.reverse()', 'from __main__ import l_cp').repeat(repeat=3, number=1000))) deque_time.append(min(timeit.Timer('d_cp.reverse()', 'from __main__ import d_cp').repeat(repeat=3, number=1000))) import platform import multiprocessing def print_sysinfo(): print('\nPython version :', platform.python_version()) print('compiler :', platform.python_compiler()) print('\nsystem :', platform.system()) print('release :', platform.release()) print('machine :', platform.machine()) print('processor :', platform.processor()) print('CPU count :', multiprocessing.cpu_count()) print('interpreter:', platform.architecture()[0]) print('\n\n') %matplotlib inline import matplotlib.pyplot as plt def plot(): plt.rcParams.update({'font.size': 12}) fig = plt.figure(figsize=(11,13)) # Setting the positions and width for the bars pos = list(range(len(list_time))) width = 0.2 # Plotting the bars fig, ax = plt.subplots(figsize=(8,6)) plt.bar(pos, list_time, width, alpha=0.5, color='g', label=labels[0]) plt.bar([p + width for p in pos], deque_time, width, alpha=0.5, color='b', label=labels[1]) # Setting axis labels and ticks ax.set_ylabel('time in seconds\n(for 1,000 operations on starting size 10,000)') ax.set_title('Python list and collections.deque operations') ax.set_xticks([p + 1.5 * width for p in pos]) ax.set_xticklabels(labels) # Setting the x-axis and y-axis limits plt.xlim(0-width, len(list_time)) plt.ylim([0, max(list_time + deque_time) * 1.5]) # Adding the legend and showing the plot plt.legend(['list', 'collections.deque'], loc='upper left') plt.grid() plt.show() plot() print_sysinfo()