For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

array is not better than list when the number is large and append operate



  • 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()  
    

    下载.png
    原文来自于


Log in to reply
 

});