I have the following program that uses a deque that I copied from the web.
from collections import Counter,deque
import re
import time
import my_ds
num = 100000
def append(c):
for i in range(num):
c.append(i)
def appendleft(c):
if isinstance(c, deque):
for i in range(num):
c.appendleft(i)
else:
for i in range(num):
c.insert(0,i)
def pop(c):
for i in range(num):
c.pop()
def popleft(c):
if isinstance(c,deque):
for i in range(num):
c.popleft()
else:
for i in range(num):
c.pop(0)
for container in [deque, list]:
for operation in [append, appendleft, pop, popleft]:
c = container(range(num))
start = time.time()
operation(c)
elapsed = time.time() - start
print('Completed {0}/{1} in {2} seconds: {3} ops/sec'.format(container.__name__,operation.__name__, elapsed, num/elapsed))
The output looks like below.
Completed deque/append in 0.011004447937011719 seconds: 9087234.595718866 ops/sec
Completed deque/appendleft in 0.00800323486328125 seconds: 12494947.56911344 ops/sec
Completed deque/pop in 0.00800323486328125 seconds: 12494947.56911344 ops/sec
Completed deque/popleft in 0.009003400802612305 seconds: 11106914.175250906 ops/sec
Completed list/append in 0.011004447937011719 seconds: 9087234.595718866 ops/sec
Completed list/appendleft in 8.727489709854126 seconds: 11458.048456601553 ops/sec
Completed list/pop in 0.01900768280029297 seconds: 5261030.555416185 ops/sec
Completed list/popleft in 1.781712532043457 seconds: 56125.776858800775 ops/sec
I am looking for a nice way to align the numbers seconds and ops/second.How is this done in python