In [1]:
%load_ext Cython


/data/miniconda3/envs/demo/lib/python3.5/site-packages/Cython/Distutils/old_build_ext.py:30: UserWarning: Cython.Distutils.old_build_ext does not properly handle dependencies and is deprecated.
  "Cython.Distutils.old_build_ext does not properly handle dependencies "

Void, no exception value


In [2]:
%%cython

cdef void foo0():
    return

def main0():
    cdef:
        int i, n=10**6
    for i in range(n):
        foo0()

In [3]:
%%timeit

main0()


The slowest run took 24.80 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 134 ns per loop

int, no exception value


In [4]:
%%cython

cdef int foo0bis():
    return 1

def main0bis():
    cdef:
        int i, n=10**6
    for i in range(n):
        foo0bis()

In [5]:
%%timeit

main0bis()


The slowest run took 19.96 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 133 ns per loop

exception value is -1


In [6]:
%%cython

cdef int foo1() except -1:
    return 1

def main1():
    cdef:
        int i, n=10**6
    for i in range(n):
        foo1()

In [7]:
%%timeit

main1()


The slowest run took 24.74 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 149 ns per loop

exception value is -1, then checked. returning 1


In [8]:
%%cython

cdef int foo2() except? -1:
    return 1

def main2():
    cdef:
        int i, n=10**6
    for i in range(n):
        foo2()

In [9]:
%%timeit

main2()


The slowest run took 22.01 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 3: 154 ns per loop

exception value is -1, then checked. returning -1


In [10]:
%%cython

cdef int foo2bis() except? -1:
    return -1

def main2bis():
    cdef:
        int i, n=10**6
    for i in range(n):
        foo2bis()

In [11]:
%%timeit

main2bis()


100 loops, best of 3: 16.7 ms per loop

Exception is always checked


In [12]:
%%cython

cdef int foo3() except *:
    return 1

def main3():
    cdef:
        int i, n=10**6
    for i in range(n):
        foo3()

In [13]:
%%timeit

main3()


100 loops, best of 3: 14.7 ms per loop

In [ ]: