cpython vs cython vs numpy array performance

前端 未结 3 1719
隐瞒了意图╮
隐瞒了意图╮ 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:37

    cdef DTYPE_t [:] p_view = p
    

    Using this instead of p in the calculations. reduced the runtime from 580 ms down to 2.8 ms for me. About the exact same runtime as the implementation using *int. And that's about the max you can expect from this.

    DTYPE = np.int
    ctypedef np.int_t DTYPE_t
    
    @cython.boundscheck(False)
    def primes(DTYPE_t kmax):
        cdef DTYPE_t n, k, i
        cdef np.ndarray p = np.empty(kmax, dtype=DTYPE)
        cdef DTYPE_t [:] p_view = p
        k = 0
        n = 2
        while k < kmax:
            i = 0
            while i < k and n % p_view[i] != 0:
                i = i + 1
            if i == k:
                p_view[k] = n
                k = k + 1
            n = n + 1
        return p
    

提交回复
热议问题