Fast Python?

Presentations

Fast Python?

https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/python3-gcc.html

Where’s the problem?

def add_two_numbers(x, y):
    return x + y


z = add_two_numbers(123, 456)
print(z)

Howto solve it?

Cython

Compile to C

cdef add_two_numbers(x, y):
    return x + y


z = add_two_numbers(123, 456)
print(z)

Explicit parameter types

cdef add_two_numbers(int x, int y):
    return x + y


z = add_two_numbers(123, 456)
print(z)

Explicit return type

cdef int add_two_numbers(int x, int y):
    return x + y


z = add_two_numbers(123, 456)
print(z)
cdef int add_two_numbers(int x, int y) nogil:
    return x + y


z = add_two_numbers(123, 456)
print(z)

Use C standard function

from libc.stdio cimport printf


cdef int add_two_numbers(int x, int y) nogil:
    printf("%i\n", x)
    return x + y


z = add_two_numbers(123, 456)
print(z)

Numba

Decorator

from numba import jit

@jit
def funkce1():
    pass

Simpler and faster print

Force JIT

@jit(nopython=True)

Some comparisons

images/benchmarks_1.png images/benchmarks_2.png