Why is numpy.array so slow?

前端 未结 4 1350
南笙
南笙 2020-11-27 21:54

I am baffled by this

def main():
    for i in xrange(2560000):
        a = [0.0, 0.0, 0.0]

main()

$ time python test.py

real     0m0.793s
<
4条回答
  •  清歌不尽
    2020-11-27 22:54

    Holy CPU cycles batman!, indeed.

    But please rather consider something very fundamental related to numpy; sophisticated linear algebra based functionality (like random numbers or singular value decomposition). Now, consider these seamingly simple calculations:

    In []: A= rand(2560000, 3)
    In []: %timeit rand(2560000, 3)
    1 loops, best of 3: 296 ms per loop
    In []: %timeit u, s, v= svd(A, full_matrices= False)
    1 loops, best of 3: 571 ms per loop
    

    and please trust me that this kind of performance will not be beaten significantly by any package currently available.

    So, please describe your real problem, and I'll try to figure out decent numpy based solution for it.

    Update:
    Here is some simply code for ray sphere intersection:

    import numpy as np
    
    def mag(X):
        # magnitude
        return (X** 2).sum(0)** .5
    
    def closest(R, c):
        # closest point on ray to center and its distance
        P= np.dot(c.T, R)* R
        return P, mag(P- c)
    
    def intersect(R, P, h, r):
        # intersection of rays and sphere
        return P- (h* (2* r- h))** .5* R
    
    # set up
    c, r= np.array([10, 10, 10])[:, None], 2. # center, radius
    n= 5e5
    R= np.random.rand(3, n) # some random rays in first octant
    R= R/ mag(R) # normalized to unit length
    
    # find rays which will intersect sphere
    P, b= closest(R, c)
    wi= b<= r
    
    # and for those which will, find the intersection
    X= intersect(R[:, wi], P[:, wi], r- b[wi], r)
    

    Apparently we calculated correctly:

    In []: allclose(mag(X- c), r)
    Out[]: True
    

    And some timings:

    In []: % timeit P, b= closest(R, c)
    10 loops, best of 3: 93.4 ms per loop
    In []: n/ 0.0934
    Out[]: 5353319 #=> more than 5 million detection's of possible intersections/ s
    In []: %timeit X= intersect(R[:, wi], P[:, wi], r- b[wi])
    10 loops, best of 3: 32.7 ms per loop
    In []: X.shape[1]/ 0.0327
    Out[]: 874037 #=> almost 1 million actual intersections/ s
    

    These timings are done with very modest machine. With modern machine, a significant speed-up can be still expected.

    Anyway, this is only a short demonstration how to code with numpy.

提交回复
热议问题