cpython vs cython vs numpy array performance

前端 未结 3 1720
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 18:10

I am doing some performance test on a variant of the prime numbers generator from http://docs.cython.org/src/tutorial/numpy.html. The below performance measures are with kma

3条回答
  •  既然无缘
    2021-01-01 18:44

    why is the numpy array so incredibly slower than a python list, when running on CPython?

    Because you didn't fully type it. Use

    cdef np.ndarray[dtype=np.int, ndim=1] p = np.empty(kmax, dtype=DTYPE)
    

    how do I cast a numpy array to a int*?

    By using np.intc as the dtype, not np.int (which is a C long). That's

    cdef np.ndarray[dtype=int, ndim=1] p = np.empty(kmax, dtype=np.intc)
    

    (But really, use a memoryview, they're much cleaner and the Cython folks want to get rid of the NumPy array syntax in the long run.)

提交回复
热议问题