Why is the time identical for Cython and Python versions of function?

Viewed 40

I am just starting out learning about Cython. I have some code written in pure Python, and started to convert it to Cython. The first function is below (both versions). I expected Cython to be faster, but there is no difference in speed. Why is this? Am I doing something wrong, or is it expected?

import numpy as np
cimport numpy as np
import time

ctypedef np.int8_t DTYPE_int

cpdef np.ndarray[DTYPE_int, ndim=2] init_population(int N, int pop_size):
    
    cdef np.ndarray[DTYPE_int, ndim=2] p
    cdef np.ndarray[DTYPE_int, ndim=2] idx
    cdef double t1

    t1=time.time()
    p = np.full((pop_size, N), np.arange(N, dtype=np.int8))
    idx = np.random.rand(p.shape[0], p.shape[1]).argsort(axis=1).astype(np.int8)
    print("%.20f" % (time.time() - t1))
    return np.take_along_axis(p, idx, axis=1)

def py_init_population(N, pop_size):
    t1=time.time()
    p = np.full((pop_size, N), np.arange(N, dtype=np.int8))
    idx = np.random.rand(p.shape[0], p.shape[1]).argsort(axis=1).astype(np.int8)
    print("%.20f" % (time.time() - t1))
    return np.take_along_axis(p, idx, axis=1)

init_population(1000, 1000)
py_init_population(1000, 1000)

Output:
7.24499845504760742188
7.26293945312500000000

0 Answers
Related