Numpy loading csv TOO slow compared to Matlab

前端 未结 5 1625
無奈伤痛
無奈伤痛 2020-12-01 05:08

I posted this question because I was wondering whether I did something terribly wrong to get this result.

I have a medium-size csv file and I tried to use numpy to l

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 05:53

    FWIW the built-in csv module works great and really is not that verbose.

    csv module:

    %%timeit
    with open('test.csv', 'r') as f:
        np.array([l for l in csv.reader(f)])
    
    
    1 loop, best of 3: 1.62 s per loop
    

    np.loadtext:

    %timeit np.loadtxt('test.csv', delimiter=',')
    
    1 loop, best of 3: 16.6 s per loop
    

    pd.read_csv:

    %timeit pd.read_csv('test.csv', header=None).values
    
    1 loop, best of 3: 663 ms per loop
    

    Personally I like using pandas read_csv but the csv module is nice when I'm using pure numpy.

提交回复
热议问题