python array太慢_python – 为什么numpy.array()有时非常慢?
我使用numpy.array()函数从列表中创建numpy.float64 ndarrays.我注意到,当列表包含None或提供列表列表时,这是非常慢的.以下是一些时代的例子.有明显的解决方法,但为什么这么慢?无列表示例:### Very slow to call array() with list of NoneIn [3]: %timeit numpy.array([None]*100000,
我使用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 we use fromiter() insead of array()
In [6]: %timeit numpy.fromiter([None]*100000, dtype=numpy.float64)
100 loops, best of 3: 3.29 ms per loop
列表列表的示例:
### Very slow to create column matrix
In [7]: %timeit numpy.array([[0.0]]*100000, dtype=numpy.float64)
1 loops, best of 3: 353 ms per loop
### No problem to create column vector and reshape
In [8]: %timeit numpy.array([0.0]*100000, dtype=numpy.float64).reshape((-1,1))
100 loops, best of 3: 10 ms per loop
### Can use itertools to flatten input lists
In [9]: %timeit numpy.fromiter(itertools.chain.from_iterable([[0.0]]*100000),dtype=numpy.float64).reshape((-1,1))
100 loops, best of 3: 9.65 ms per loop
更多推荐
所有评论(0)