Dựa trên câu trả lời của @unutbu, tôi đã so sánh hiệu suất lặp của hai danh sách giống hệt nhau khi sử dụng các hàm zip[]
của Python 3.6, hàm ____2 của Python, sử dụng bộ đếm thủ công [xem hàm count[]
], sử dụng danh sách chỉ mục và trong một danh sách Kịch bản đặc biệt trong đó các yếu tố của một trong hai danh sách [foo
hoặc bar
] có thể được sử dụng để lập chỉ mục danh sách khác. Các màn trình diễn của họ để in và tạo một danh sách mới, tương ứng, đã được nghiên cứu bằng hàm timeit[]
trong đó số lần lặp lại được sử dụng là 1000 lần. Một trong những kịch bản Python mà tôi đã tạo để thực hiện các cuộc điều tra này được đưa ra dưới đây. Các kích thước của danh sách foo
và bar
đã dao động từ 10 đến 1.000.000 yếu tố.
Results:
Đối với mục đích in ấn: Hiệu suất của tất cả các phương pháp được xem xét được quan sát là xấp xỉ tương tự như hàm
zip[]
, sau khi bao gồm dung sai chính xác là +/- 5%. Một ngoại lệ xảy ra khi kích thước danh sách nhỏ hơn 100 yếu tố. Trong một kịch bản như vậy, phương pháp danh sách chỉ số chậm hơn một chút so với hàmzip[]
trong khi hàmenumerate[]
nhanh hơn ~ 9%. Các phương pháp khác mang lại hiệu suất tương tự như hàmzip[]
. The performances of all the considered approaches were observed to be approximately similar to thezip[]
function, after factoring an accuracy tolerance of +/-5%. An exception occurred when the list size was smaller than 100 elements. In such a scenario, the index-list method was slightly slower than thezip[]
function while theenumerate[]
function was ~9% faster. The other methods yielded similar performance to thezip[]
function.Để tạo danh sách: Hai loại phương pháp tạo danh sách đã được khám phá: sử dụng phương pháp [a]
zip[]
3 và [b] Danh sách hiểu. Sau khi bao gồm dung sai chính xác là +/- 5%, đối với cả hai phương pháp này, hàmzip[]
đã được tìm thấy để thực hiện nhanh hơn hàmenumerate[]
, so với sử dụng chỉ số danh sách, hơn là sử dụng bộ đếm thủ công. Tăng hiệu suất của hàmzip[]
trong các so sánh này có thể nhanh hơn 5% đến 60%. Điều thú vị là, sử dụng phần tử củafoo
để lập chỉ mụcbar
có thể mang lại hiệu suất tương đương hoặc nhanh hơn [5% đến 20%] so với hàmzip[]
. Two types of list creation approaches were explored: using the [a]zip[]
3 method and [b] list comprehension. After factoring an accuracy tolerance of +/-5%, for both of these approaches, thezip[]
function was found to perform faster than theenumerate[]
function, than using a list-index, than using a manual counter. The performance gain by thezip[]
function in these comparisons can be 5% to 60% faster. Interestingly, using the element offoo
to indexbar
can yield equivalent or faster performances [5% to 20%] than thezip[]
function.
Có ý nghĩa về những kết quả này:
Một lập trình viên phải xác định lượng thời gian tính toán cho mỗi hoạt động có ý nghĩa hoặc có ý nghĩa.
Ví dụ, đối với mục đích in , chúng tôi thấy rằng các kích thước danh sách có hơn 144 yếu tố sẽ phải chịu chi phí tính toán đáng kể và tầm quan trọng đối với lập trình viên. Đó là, bất kỳ hiệu suất nào đạt được từ các phương pháp được đề cập trong cuộc điều tra này cho các kích thước danh sách nhỏ hơn sẽ không đáng kể đối với lập trình viên. Lập trình viên sẽ kết luận rằng hiệu suất của hàm zip[]
để lặp lại các câu lệnh in tương tự như các phương pháp khác.
Sự kết luận
Hiệu suất đáng chú ý có thể đạt được từ việc sử dụng hàm zip[]
để lặp lại thông qua hai danh sách song song trong quá trình tạo enumerate[]
2. Khi lặp lại thông qua hai danh sách song song để in ra các phần tử của hai danh sách, hàm zip[]
sẽ mang lại hiệu suất tương tự như hàm enumerate[]
, như sử dụng biến bộ đếm thủ công, như sử dụng danh sách chỉ mục và trong khi Kịch bản đặc biệt trong đó các yếu tố của một trong hai danh sách [foo
hoặc bar
] có thể được sử dụng để lập chỉ mục danh sách khác.
Tập lệnh Python & NBSP; 3.6 được sử dụng để điều tra việc tạo danh sách.
import timeit
import matplotlib.pyplot as plt
import numpy as np
def test_zip[ foo, bar ]:
store = []
for f, b in zip[foo, bar]:
#print[f, b]
store.append[ [f, b] ]
def test_enumerate[ foo, bar ]:
store = []
for n, f in enumerate[ foo ]:
#print[f, bar[n]]
store.append[ [f, bar[n]] ]
def test_count[ foo, bar ]:
store = []
count = 0
for f in foo:
#print[f, bar[count]]
store.append[ [f, bar[count]] ]
count += 1
def test_indices[ foo, bar, indices ]:
store = []
for i in indices:
#print[foo[i], bar[i]]
store.append[ [foo[i], bar[i]] ]
def test_existing_list_indices[ foo, bar ]:
store = []
for f in foo:
#print[f, bar[f]]
store.append[ [f, bar[f]] ]
list_sizes = [ 10, 100, 1000, 10000, 100000, 1000000 ]
tz = []
te = []
tc = []
ti = []
tii= []
tcz = []
tce = []
tci = []
tcii= []
for a in list_sizes:
foo = [ i for i in range[a] ]
bar = [ i for i in range[a] ]
indices = [ i for i in range[a] ]
reps = 1000
tz.append[ timeit.timeit[ 'test_zip[ foo, bar ]',
'from __main__ import test_zip, foo, bar',
number=reps
]
]
te.append[ timeit.timeit[ 'test_enumerate[ foo, bar ]',
'from __main__ import test_enumerate, foo, bar',
number=reps
]
]
tc.append[ timeit.timeit[ 'test_count[ foo, bar ]',
'from __main__ import test_count, foo, bar',
number=reps
]
]
ti.append[ timeit.timeit[ 'test_indices[ foo, bar, indices ]',
'from __main__ import test_indices, foo, bar, indices',
number=reps
]
]
tii.append[ timeit.timeit[ 'test_existing_list_indices[ foo, bar ]',
'from __main__ import test_existing_list_indices, foo, bar',
number=reps
]
]
tcz.append[ timeit.timeit[ '[[f, b] for f, b in zip[foo, bar]]',
'from __main__ import foo, bar',
number=reps
]
]
tce.append[ timeit.timeit[ '[[f, bar[n]] for n, f in enumerate[ foo ]]',
'from __main__ import foo, bar',
number=reps
]
]
tci.append[ timeit.timeit[ '[[foo[i], bar[i]] for i in indices ]',
'from __main__ import foo, bar, indices',
number=reps
]
]
tcii.append[ timeit.timeit[ '[[f, bar[f]] for f in foo ]',
'from __main__ import foo, bar',
number=reps
]
]
print[ f'te = {te}' ]
print[ f'ti = {ti}' ]
print[ f'tii = {tii}' ]
print[ f'tc = {tc}' ]
print[ f'tz = {tz}' ]
print[ f'tce = {te}' ]
print[ f'tci = {ti}' ]
print[ f'tcii = {tii}' ]
print[ f'tcz = {tz}' ]
fig, ax = plt.subplots[ 2, 2 ]
ax[0,0].plot[ list_sizes, te, label='enumerate[]', marker='.' ]
ax[0,0].plot[ list_sizes, ti, label='index-list', marker='.' ]
ax[0,0].plot[ list_sizes, tii, label='element of foo', marker='.' ]
ax[0,0].plot[ list_sizes, tc, label='count[]', marker='.' ]
ax[0,0].plot[ list_sizes, tz, label='zip[]', marker='.']
ax[0,0].set_xscale['log']
ax[0,0].set_yscale['log']
ax[0,0].set_xlabel['List Size']
ax[0,0].set_ylabel['Time [s]']
ax[0,0].legend[]
ax[0,0].grid[ b=True, which='major', axis='both']
ax[0,0].grid[ b=True, which='minor', axis='both']
ax[0,1].plot[ list_sizes, np.array[te]/np.array[tz], label='enumerate[]', marker='.' ]
ax[0,1].plot[ list_sizes, np.array[ti]/np.array[tz], label='index-list', marker='.' ]
ax[0,1].plot[ list_sizes, np.array[tii]/np.array[tz], label='element of foo', marker='.' ]
ax[0,1].plot[ list_sizes, np.array[tc]/np.array[tz], label='count[]', marker='.' ]
ax[0,1].set_xscale['log']
ax[0,1].set_xlabel['List Size']
ax[0,1].set_ylabel['Performances [ vs zip[] function ]']
ax[0,1].legend[]
ax[0,1].grid[ b=True, which='major', axis='both']
ax[0,1].grid[ b=True, which='minor', axis='both']
ax[1,0].plot[ list_sizes, tce, label='list comprehension using enumerate[]', marker='.']
ax[1,0].plot[ list_sizes, tci, label='list comprehension using index-list[]', marker='.']
ax[1,0].plot[ list_sizes, tcii, label='list comprehension using element of foo', marker='.']
ax[1,0].plot[ list_sizes, tcz, label='list comprehension using zip[]', marker='.']
ax[1,0].set_xscale['log']
ax[1,0].set_yscale['log']
ax[1,0].set_xlabel['List Size']
ax[1,0].set_ylabel['Time [s]']
ax[1,0].legend[]
ax[1,0].grid[ b=True, which='major', axis='both']
ax[1,0].grid[ b=True, which='minor', axis='both']
ax[1,1].plot[ list_sizes, np.array[tce]/np.array[tcz], label='enumerate[]', marker='.' ]
ax[1,1].plot[ list_sizes, np.array[tci]/np.array[tcz], label='index-list', marker='.' ]
ax[1,1].plot[ list_sizes, np.array[tcii]/np.array[tcz], label='element of foo', marker='.' ]
ax[1,1].set_xscale['log']
ax[1,1].set_xlabel['List Size']
ax[1,1].set_ylabel['Performances [ vs zip[] function ]']
ax[1,1].legend[]
ax[1,1].grid[ b=True, which='major', axis='both']
ax[1,1].grid[ b=True, which='minor', axis='both']
plt.show[]