In [3]:
%load_ext cythonmagic
The cythonmagic extension is already loaded. To reload it, use:
  %reload_ext cythonmagic

In [11]:
%%cython
from libc.stdlib cimport malloc, free
cimport numpy as np
import numpy as np
from cython cimport view

N = 10000

def memview_malloc():
    cdef int * m = <int *>malloc(N * sizeof(int))
    cdef int[::1] b = <int[:N]>m
    free(<void *>m)

def memview_ndarray():
    cdef int[::1] b = np.empty(N, dtype=np.int32)
    
def memview_cyarray():
    cdef int[::1] b = view.array(shape=(N,), itemsize=sizeof(int), format="i")
In [15]:
%timeit memview_malloc()
100000 loops, best of 3: 5.47 µs per loop

In [16]:
%timeit memview_ndarray()
100000 loops, best of 3: 3.55 µs per loop

In [17]:
%timeit memview_cyarray()
100000 loops, best of 3: 4.01 µs per loop