from array import array import numpy as np import time # append数据 content_append_by_array=array('d') content_append_by_list=list() # 尝试分别给每个增加10**n的数据(n从1,到11),计算时间 # n=range(1,12) array_time=[] list_time=[] for i in range(1,9): print(i) t1=time.time() for x in range(10**i): content_append_by_array.append(x) t2=time.time() array_time.append(t2-t1) t1=time.time() for x in range(10**i): content_append_by_list.append(x) t2=time.time() list_time.append(t2-t1) import pandas as pd import matplotlib.pyplot as plt df=pd.DataFrame(index=range(1,9)) df['array']=array_time df['list']=list_time df.plot() plt.show()
从中可以看出,在append数据这一项上,在数量非常多的时候,array比list效率要差很多。