Make Python code 1000x Faster with Numba

Jack of Some · 466K views · 2019 · 20:33

Open on YouTube

Software performance & optimizationPython, Ruby & scripting languagesCompilers, interpreters & program analysistutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering slow NumPy or loop performance questions, as a practical introduction to speeding code up with Numba's @njit.

Recommended by 3 people across 3 different months, 2021 to 2022.

What it is

A 20-minute tutorial by Jack of Some introducing Numba, a just-in-time compiler for a subset of Python and NumPy. It covers common mistakes, then walks through a spring-mass simulation that runs about 1000x faster with Numba in single-threaded and multithreaded (nogil) form, ending with a reading list.

What people said

Recommends figs_and_nuts · 2021-12 · Stack Overflow · on “Why doesn't the following code need a python interpreter?”

I was watching a video [this video] to learn Numba. At 17:00 the presenter has the following code on screen: @njit def simulate_spring_mass_funky_damper(x0, T=10, dt=0.0001, vt=1.0): times = np.arange(0, T, dt) positions = np.zeros_like(times) v = 0 a = 0 x = x0 positions[0] = x0/x0 for ii in range(len(times)): if ii == 0: continue t = times[ii] a = friction_fn(v, vt) - 100*x v = v + a*dt x = x + v*dt positions[ii] = x/x0 return times, positions The presenter then proceeds to instruct numba to release the GIL using jnit(nogil=True) . His argument is This function does not need to access the python interpreter while it is running. In fact, we made sure of that. So we can additionally tell it to re

Recommends pugi · 2022-01 · Stack Overflow · on “In numpy, how to efficiently build a mapping from each unique value to its indices, without using a for loop”

I advise you to check out numba which can speed up numpy code on python significantly - it supports numpy.invert() and numpy.unique() - documentation [LINK] Here is a good video explaining how to use numba from youtube - Make Python code 1000x Faster with Numba [this video]

Recommends Jakub · 2022-07 · Stack Overflow · on “Improve performance of combinations”

if you need futher speed up, the vectorized option mr.Kovalski provided, try us Numba on top of that. Note that not all numpy function are supported inside Numba. More here: [this video]

Recommends Jakub · 2022-07 · Stack Overflow · on “How do I optimize this integration loop?”

Welcome to the community, on top of the previous comments, if you convert it into numpy, the numba would have great potential to speed up your function, it basically convert Python into "lower" language, and the result sometimes greatly speed up the things up (potentinaly at same rate, that whole conversion into c++ would be) more here: [this video]

Cites Suzuna Minami · 2021-05 · Stack Overflow · on “Numpy 3D array max and min value”

Make sure that everything you use is a numpy array and NOT Python list and make sure that all of the members have the same data type. In your case this is true. Now you can make use of a library called numba . It uses JIT. A video demonstrating it can be seen here [this video] . The documentation for numba can be seen here [LINK]