import numpy as np
import time
# Python列表
size = 1000000
list1 = list(range(size))
list2 = list(range(size))
start = time.time()
result_list = [(x + y) for x, y in zip(list1, list2)]
end = time.time()
print(f"Python列表耗时: {(end - start) * 1000:.2f} 毫秒")
# Numpy数组
array1 = np.arange(size)
array2 = np.arange(size)
start = time.time()
result_array = array1 + array2
end = time.time()
print(f"NumPy数组耗时: {(end - start) * 1000:.2f} 毫秒")
Python列表耗时: 50.99 毫秒
NumPy数组耗时: 1.93 毫秒