Can Python Approach the Speeds of C?

preview_player
Показать описание
In this video, we explore a HUGE gamechanger for Python speed and optimization - Just In Time compilation (JIT) with Numba.

0:00 - Intro
0:52 - Compilation vs interpretation
2:05 - Numba (JIT)
2:36 - Decarators
3:58 - NumPy
Рекомендации по теме
Комментарии
Автор

That decorator is amazing, but the speed up in this case happens because it compiles the code and knows the answer beforehand

If you will try the same example in c++ for example (you might need to use constexpr) you will see that the assembly code will be only a couple of lines because the compiler does all the work on the background

plwxzvm
Автор

I will share this with people just by virture. The meme you put at 1:05. Brilliant animations.

Proprogrammer
Автор

So cool that you can just slap such a short decorator on your functions to do it with Numba

sanderbos
Автор

Congratulations Daniel! You managed to lose me the first 10 seconds. I have to come back to this video after CS50 class 😅 Amazing Video!

TheControlMastr
Автор

a few months late but I implemented the same simple function and ran it on my machine and used 10, 000 for the parameter value. ~3 trillion ns for pure python vs 1.4, 1100, 370 and 250ns for four consequitive runs for the jit version (speed didn't change much after that and hovered around 250).

Running the same algorithm in c++ with -O2 it seemingly just optimized it to constant-time calculation as it ran in 10-20ns both when using 10, 000 and 100, 000 as the parameter.

I wonder what could be a better microbenchmark that shows how well the jiting works for python and how close to c/c++ it can get.

KalleLast
Автор

I knew it would be numba. Cool thing is if you have some complicated shit to do you can use cmake to create your own package.

brockobama
Автор

Very interesting; I personally use Cython's Pure Python mode to compile time-intensive Python functions, and I wonder how it compared to Numba

Darkev
Автор

This is simply not possible, a script running under a VM will never be as fast as a native executable with the same algorithm and task...

erikkonstas
Автор

this taught me how little chicken wings float in the equator of the earth

ImperialRoads
Автор

Thing is, numba isn't 100% compatible with numpy. Try using argmin or argmax. It simply doesn't work in nopython mode. It's really a shame.

josebr
Автор

hacking tools are about to get much faster

myname-mzlo
Автор

Why Python is faster?




Because is using c

manupro
Автор

That is cheating because it isn't Python which is doing the work, you are not using Python, just Python-syntax. It is like driving a 90 year old woman in a car and then having her being faster than Usain Bolt. Sure, because the car-engine is doing all the work. Obviously I am not against making the code run faster but it doesn't solve the problem that too many people who learned programming are too comfortable with using Python and are using it for things which it just isn't suitable for, unless you do tricks like this. I can easily tell when a program uses Python or another interpreter-language (but usually it is Python), you can easily feel that the program starts slower, is slower with the GUI (when you open a window from a context-menu or whatever) and all that. Examples are Thunderbird and Lutris. I still use it but it is noticeable and if I would know of software with equivalent features but which uses a compiled language then I would switch.

peterjansen