我有以下程序使用我从网上复制的双端队列。
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))
输出如下所示。
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
我正在寻找一种很好的方法来对齐数字seconds
和ops/second
。这在pythonI中是如何完成的,以下程序使用了我从网上复制的双端队列。
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_list = [container.__name__,operation.__name__, elapsed, num/elapsed]
print(tabulate(print_list, headers = ['Container , Operation , Tile elapsed , ops/sec'], floatfmt=".8f"))
我收到以下错误。
Traceback (most recent call last):
File "C:/playpen/python/concepts/data_structures/collections.py", line 66, in <module>
print(tabulate(print_list, headers = ['Container , Operation , Tile elapsed , ops/sec'], floatfmt=".8f"))
File "C:\Anaconda3\lib\site-packages\tabulate.py", line 894, in tabulate
list_of_lists, headers = _normalize_tabular_data(tabular_data, headers)
File "C:\Anaconda3\lib\site-packages\tabulate.py", line 649, in _normalize_tabular_data
rows = list(map(list,rows))
TypeError: 'float' object is not iterable
答案 0 :(得分:1)
您需要收集所有结果,然后一次打印:
print_list = []
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_list.append([container.__name__,operation.__name__, elapsed, num/elapsed])
print(tabulate(print_list, headers = ['Container , Operation , Tile elapsed , ops/sec'], floatfmt=".8f"))
tabulate
获取行列表(应该是值列表)。您的旧版本尝试迭代数字结果。