SoFunction
Updated on 2024-11-10

Python gas pedal numba use details

python a gas pedal package, here do not talk about the principle, only about the application process, as well as give a few small examples, directly write on the line; also gives numba how to define the use of List

1. The simplest use

When the output return value is an integer or floating point number:

from numba import jit
@jit(nopython=True)
def f(x, y):
    return x+y

if __name__ == '__main__':
    a = 1
    b = 1
    print(f(a,b))

Explanation:

Use the decorator, also, nopython=True is to prevent numba from automatically changing the acceleration mode, i.e. using nopython.

The above code is equivalent:

from numba import njit
@njit  # aka jit(nopython=True)
def f(x, y):
    return x+y

if __name__ == '__main__':
    a = 1
    b = 1
    print(f(a,b))

2、Advancement

When the output return value is "value of different type":

Decorator usage: generated_jit

from numba import generated_jit, typed

@generated_jit(nopython=True)
def f2(x):
    if x==1:
        return lambda x: x+1
    else:
        return lambda x: [1,2]

if __name__ == '__main__':
    a = 1
    print(f2(a))

When performing matrix or vector operations:

Using the decorator: vectorize

from numba import vectorize, float64
import numpy as np

@vectorize([float64(float64, float64)])  # Note the brackets
def f(x, y):
    return x + y

if __name__ == '__main__':
    a = ([1,2])
    b = ([2,2])
    print(f(a,b))

replenishment

Define lists within numba:

import numpy as np
import time
NUM = 160
from numba import jit
from  import List

a = List()
(1) # Also need to specify the data type, stuff a 1, the data type is int

@jit(nopython=True)
def f(a):
    for i in range(NUM):
        (i)

if __name__ == '__main__':
   for i in range(5):
    start = ()
    f(a)
    print(()-start)

to this article on the use of python gas pedal numba detailed article is introduced to this, more related python gas pedal numba content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!