``````

In [ ]:

import numpy as np

N = 1000000

A = np.random.rand(N)

``````
``````

In [ ]:

def mean_plain(A, N):
sum = 0.0
j = 0
while j < N:
sum += A[j]
j+=1

return sum / N

``````
``````

In [ ]:

%%timeit -n 1 -r 1

mean_plain(A, N)

``````
``````

In [ ]:

``````
``````

In [ ]:

%%cython
cimport numpy as np

def mean_cython1(A, N):
sum = 0.0
j = 0
while j < N:
sum += A[j]
j+=1

return sum / N

``````
``````

In [ ]:

%%timeit -n 1 -r 1

mean_cython1(A, N)

``````
``````

In [ ]:

%%cython
def mean_cython2(A, int N):
cdef double sum = 0.0
cdef int j = 0

while j < N:
sum += A[j]
j += 1

return sum / N

``````
``````

In [ ]:

%%timeit -n 1 -r 1

mean_cython2(A, N)

``````
``````

In [ ]:

%%cython
cimport numpy as np

def mean_cython3(np.ndarray[np.float64_t, ndim=1] A, int N):
cdef double sum = 0.0
cdef int j = 0

while j < N:
sum += A[j]
j += 1

return sum / N

``````
``````

In [ ]:

%%timeit -n 1 -r 1

mean_cython3(A, N)

``````
``````

In [ ]:

%%cython
cimport numpy as np
cimport cython

@cython.boundscheck(False)
@cython.wraparound(False)
def mean_cython4(np.ndarray[np.float64_t, ndim=1, mode="c"] A, int N):
cdef double sum = 0.0
cdef int j = 0

while j < N:
sum += A[j]
j += 1

return sum / N

``````
``````

In [ ]:

%%timeit -n 1 -r 1

mean_cython4(A, N)

``````
``````

In [ ]:

%%cython
cimport numpy as np
cimport cython

@cython.boundscheck(False)
@cython.wraparound(False)
def mean_cython5(np.ndarray[np.float64_t, ndim=1, mode="c"] A, int N):
cdef double sum = 0.0
cdef int j = 0

with nogil:
while j < N:
sum += A[j]
j += 1

return sum / N

``````
``````

In [ ]:

%%timeit -n 1 -r 1

mean_cython5(A, N)

``````