Cython makes Python INSANELY FAST

preview_player
Показать описание
Python has a bit of a reputation for being slow, but with just a little bit of effort, it can approach the sort of speeds you could expect from C. Wowsers.



If you enjoy my content, consider supporting me on Patreon (or hit the JOIN button below to become a member)!

If you need help with anything, feel free to join the Discord server:

I get a lot of people asking, so here's my Visual Studio Code setup!



If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.

#python #coding #howto
Рекомендации по теме
Комментарии
Автор

Thanks to Mitko for pointing this out — due to an oversight in my implementation, the benchmarks for the factorial function are actually incorrect. This doesn't degrade the educational value with regard to the syntax, which is all fine, but I thought I'd just mention it. Moral of the story: be careful around big numbers!

Carberra
Автор

Wait! You don't check if the answers are the same! I suspect if the code is converted to C/C++ without using Python's int object, it overflows and that's why it's way faster. It should be way faster eitherway, but does it properly use the python int object or just uses the primitive 4 bytes integer in C/C++?

MitkoNikov
Автор

Yo this is awesome bro, I run this code and it was actually 1867.571 times faster using Python 3.10.7 and C++ 14.0 (for the factorial() example)

shner
Автор

You might write some Common Lisp params to benchmark them against Python programs solving the same problem. Common Lisp types dynamically. But some programmers wrote operating systems in it to run them on computers designed to run Lisp programs. Common Lisp lets you speed up your Lisp by statically typing some variables.

williammcenaney
Автор

"so" in "*.so" files stands for Shared Object.

divingradish
Автор

Excellent video! New to Cython, but when I tried this without importing the cpython array it worked with similar speedups. Maybe they changed something in the last 6 months?

traversis
Автор

Use deques (double-ended queues) instaed of lists in python if one wants fast execution times. It's why they are a part of python.

richardbennett
Автор

i'd assume the .so file is the same as every other .so file: a shared object. basically, a dynamically linked library.

colly
Автор

I wonder if PyPy could use some of its thorough data type analysis of Python code to convert Python to Cython. PyPy does JIT compilation but I wonder if mixing PyPy with Cython tools could lead to better performance.

IARRCSim
Автор

Great video!
More videos on Cython please!

In the video you mentioned the pure python mode didn't give the level of speedup of full Cython syntax...I experienced the same. Is this behaviour to be expected? I would rather write full Cython syntax if that's the case.

pietraderdetective
Автор

Step 1: ask ChatGPT generate simple python program for hard task
Step 2: Cython generate C code
Step 3: Ask ChatGPT to simplify each Cython code files
Step 4: Go back to step 1

tristotech
Автор

so, is a shared object, something like *.dll in Windows platforms...

grimonce
Автор

Use numba in oython. Vastly simpler, and IT DOES GET ONE TO THE SPEEDS OF C.

Also, there's pypy3 and codon, a language tgat uses python syntax, but is compiled, so wicked fast. 😮

richardbennett
Автор

You can't get 1000! fit in int64. Python's `int` contains all the digits, while C's `int` overflows silently.

RuslanKovtun
Автор

Can you call existing c math functions like matrix multiplication?

wryltxw
Автор

Can you call numpy functions from within a cython kernel?

ripsirwin
Автор

100, 000! doesn’t blow up the max int value for C? That seems suspicious. Might want to verify that the outputs are the same.

abschmit
Автор

So confused. In the second example. Why did you have to make a separate cpdef list function ? I don’t see how it is even referenced from the first routine? I thought the whole idea was just to Cython existing code. This is very odd as it looks like you have to add a bunch of anscillary code to make it work and how would you even know what to put? My brain hurts now.

PeterRichardsandYoureNot
Автор

Factorial 1000 will overflow your int. C doesn't care about the overflow, python does.

AlexeiFarWest
Автор

Could you please make a series of Cython? It's really amazing. 💯
Thanksgiving.

dipeshsamrawat