我正在使用numpy.array()函数从列表中创建numpy.float64 ndarrays.
我注意到当列表包含None或列表列表时,这非常慢.
以下是一些有时间的例子.有明显的解决方法,但为什么这么慢?
无列表的示例:
### Very slow to call array() with list of None
In [3]: %timeit numpy.array([None]*100000, dtype=numpy.float64)
1 loops, best of 3: 240 ms per loop
### Problem doesn't exist with array of zeroes
In [4]: %timeit numpy.array([0.0]*100000, dtype=numpy.float64)
100 loops, best of 3: 9.94 ms per loop
### Also fast if we use dtype=object and convert to float64
In [5]: %timeit numpy.array([None]*100000, dtype=numpy.object).astype(numpy.float64)
100 loops, best of 3: 4.92 ms per loop
### Also fast if …Run Code Online (Sandbox Code Playgroud)