Numba (software package)

from Wikipedia, the free encyclopedia
Numba
Basic data

Publishing year 2012
Current  version 0.49.1
(May 7, 2020)
Current preliminary version 0.51.0dev0
(June 3, 2020)
operating system Windows , macOS , Linux
programming language Python, C.
category Just-in-time compiler
License BSD 2 Clause Simplified
https://github.com/numba/numba

The software package numba is a just-in-time compiler for Python that translates numerical functions into machine code , which speeds up the execution of these functions. Likewise, it allows for the parallel processing of NumPy - objects on multiple cores of the processor as well as the graphics card .

numba is based on LLVM and the Python package llvmlite.

The numba project was initiated by Travis Oliphant , co-founder of Anaconda Inc. Anaconda publishes the Python distribution of the same name ; Numba is mainly developed by Anaconda Inc. DARPA , Intel , nvidia and AMD support the project.

example

This program shows all prime numbers between 1 and 500,000. The only changes compared to normal Python code are the lines for importing the numba package and the decorator.

import numba
import math

# Sogenannter decorator; teilt mit, dass diese Funktion
# kompiliert werden soll - mit Angabe des Funktionsprototypen (die Funktion ist
# vom Typ boolean und erhält als Aufrufparameter einen uint32).
@numba.jit(numba.boolean(numba.uint32))
def isprime(n):
    if n < 2:
        return False    # 0 und 1 sind keine Primzahlen.
    if n == 2:
        return True     # 2 ist prim
    if (n % 2) == 0:
        return False    # Modulo 2 wird gesondert behandelt.

    # Es genuegt, Teiler bis Wurzel(n) zu testen.
    divMax = math.floor(math.sqrt(n))

    # Nur ungerade Zahlen als Teiler testen.
    for divisor in range(3, divMax, 2):
        if (n % divisor) == 0:
            # Ein Teiler wurde gefunden, n kann keine Primzahl sein --> Abbruch
            return False
        # end if
    # end for
    return True     # Kein Teiler gefunden, n ist prim.
# end method

for i in range(1, 500000):
    if isprime(i):
        print(i)
    # end if
# end for

The runtime of this program is comparable to a variant programmed in C ++ .

See also

Web links