Cython makes Python INSANELY FAST

  Рет қаралды 32,721

Carberra

Carberra

2 жыл бұрын

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)!
patreon.carberra.xyz
If you need help with anything, feel free to join the Discord server:
discord.carberra.xyz
I get a lot of people asking, so here's my Visual Studio Code setup!
• My Visual Studio Code ...
-
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

Пікірлер: 52
@Carberra
@Carberra 2 жыл бұрын
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!
@MitkoNikov
@MitkoNikov 2 жыл бұрын
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++?
@Carberra
@Carberra 2 жыл бұрын
You are...right. Fuck, how did I not notice that? Thankfully I don't think it kills the educational value with regard to the syntax, but I will update the thumbnail to reflect the speedup from the sorting, which I've confirmed as legit.
@shner6742
@shner6742 Жыл бұрын
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)
@williammcenaney1331
@williammcenaney1331 3 ай бұрын
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.
@traversis
@traversis Жыл бұрын
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?
@Carberra
@Carberra Жыл бұрын
Entirely possible. I haven't really gone back to this since the video so I couldn't tell you. It's possible the speed was roughly the same anyways.
@divingradish
@divingradish 2 жыл бұрын
"so" in "*.so" files stands for Shared Object.
@Carberra
@Carberra 2 жыл бұрын
I see -- I had a hunch but wasn't 100% sure. Thanks for confirming (:
@colly6022
@colly6022 10 ай бұрын
i'd assume the .so file is the same as every other .so file: a shared object. basically, a dynamically linked library.
@pietraderdetective8953
@pietraderdetective8953 8 ай бұрын
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.
@Carberra
@Carberra 7 ай бұрын
Thank you! I believe so, as using Pure Python Mode requires the use of cpdef where cdef could be used in pure Cython. That's my understanding of it anyways, if I'm honest I haven't really done much with Cython since making this video!
@IARRCSim
@IARRCSim 23 күн бұрын
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.
@tristotech
@tristotech 6 ай бұрын
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
@hammerheadcorvette4
@hammerheadcorvette4 2 ай бұрын
ChatGPT is not the end all be all. It's cool sure, it's not infallible.
@ripsirwin1
@ripsirwin1 6 ай бұрын
Can you call numpy functions from within a cython kernel?
@AlexeiFarWest
@AlexeiFarWest 7 ай бұрын
Factorial 1000 will overflow your int. C doesn't care about the overflow, python does.
@furkanesen
@furkanesen 2 жыл бұрын
Thx you.
@Carberra
@Carberra 2 жыл бұрын
You're welcome (:
@richardbennett4365
@richardbennett4365 Ай бұрын
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.
@Carberra
@Carberra Ай бұрын
I recently made a video talking about this, if you're interested.
@grimonce
@grimonce Жыл бұрын
so, is a shared object, something like *.dll in Windows platforms...
@ritikranjan7192
@ritikranjan7192 2 жыл бұрын
naise
@abschmit
@abschmit 7 ай бұрын
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.
@RuslanKovtun
@RuslanKovtun Жыл бұрын
You can't get 1000! fit in int64. Python's `int` contains all the digits, while C's `int` overflows silently.
@birdbeakbeardneck3617
@birdbeakbeardneck3617 10 ай бұрын
I thought all the cython stuff in the .so file makes it so the int allocate more space
@richardbennett4365
@richardbennett4365 Ай бұрын
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. 😮
@Carberra
@Carberra Ай бұрын
I've made videos on both Numba and PyPy, if you're interested.
@user-wr4yl7tx3w
@user-wr4yl7tx3w Жыл бұрын
Can you call existing c math functions like matrix multiplication?
@Carberra
@Carberra Жыл бұрын
Yep. The documentation shows how, but I've never really tried it.
@asianman1329
@asianman1329 2 жыл бұрын
Could you do a video on mypyc?
@Carberra
@Carberra 2 жыл бұрын
I certainly can! Planning on doing a video on that for next week (:
@asianman1329
@asianman1329 2 жыл бұрын
@@Carberra sweet!
@rolfjohansen5376
@rolfjohansen5376 10 ай бұрын
how are classes with user functions represented in C?
@Apuryo
@Apuryo 4 ай бұрын
no classes in c. use c++
@2sourcerer
@2sourcerer 9 ай бұрын
Javascript also has to check types. Why is it faster?
@PeterRichardsandYoureNot
@PeterRichardsandYoureNot Ай бұрын
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.
@dipeshsamrawat7957
@dipeshsamrawat7957 2 ай бұрын
Could you please make a series of Cython? It's really amazing. 💯 Thanksgiving.
@Carberra
@Carberra 2 ай бұрын
If only I knew enough about it to say I was confident making a series on it 😅 I do keep meaning to go back to it and learn it more, just finding the time to do so atm is tough.
@dipeshsamrawat7957
@dipeshsamrawat7957 2 ай бұрын
@@Carberra I can understand your hard work. Please proceed with the action in the future if feasible. Thank you.
@jfk1337
@jfk1337 Жыл бұрын
how can cython modules be pip installed?
@Carberra
@Carberra Жыл бұрын
Same as any other module.
@jfk1337
@jfk1337 Жыл бұрын
@@Carberra And pip automatically builds the .so?
@Carberra
@Carberra Жыл бұрын
Cython builds the .so files. You then include the .so files in your distributed package, and pip will treat it like any other file.
@desertfish74
@desertfish74 Жыл бұрын
@@Carberra and this makes your code no longer portable
@richardbennett4365
@richardbennett4365 Ай бұрын
😮😮😮 Oh, no. The narrator said, "can be RAN." NO. CAN BE RUN. The word be is right there, so the past participle form must be used of the verb to run, not the past tense form of run. Run, ran, run. It's not run, ran, ran. That woukd be incorrect.
@johnlimusicofficial220
@johnlimusicofficial220 8 күн бұрын
Hell yeah!! Grammar Nazi, adore your
@cachazurdos
@cachazurdos Жыл бұрын
Spanish pliss
@JOHNSMITH-ve3rq
@JOHNSMITH-ve3rq Жыл бұрын
Bruh just paste the python code into gpt and ask it to turn it into pure c. Problem solved m
@hammerheadcorvette4
@hammerheadcorvette4 2 ай бұрын
Learn to code. You can't rely on ChatGPT too much. Gemini is even worse.
@richardbennett4365
@richardbennett4365 Ай бұрын
Its wuick, because it's compiled into machine code. Duh.
Compiled Python is FAST
12:57
Doug Mercer
Рет қаралды 97 М.
5 Good Python Habits
17:35
Indently
Рет қаралды 370 М.
Китайка и Пчелка 10 серия😂😆
00:19
KITAYKA
Рет қаралды 2,1 МЛН
🌊Насколько Глубокий Океан ? #shorts
00:42
King jr
Рет қаралды 1,7 МЛН
🍕Пиццерия FNAF в реальной жизни #shorts
00:41
Unlocking your CPU cores in Python (multiprocessing)
12:16
mCoding
Рет қаралды 293 М.
Cython 3 - Python at the speed of C - Stefan Behnel
31:15
EuroPython Conference
Рет қаралды 6 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 364 М.
PyPy: The FASTEST Python implementation
9:52
Carberra
Рет қаралды 11 М.
Turn Python BLAZING FAST with these 6 secrets
5:01
Dreams of Code
Рет қаралды 34 М.
10 Nooby Mistakes Devs Often Make In Python
24:31
Indently
Рет қаралды 29 М.
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1 МЛН
5 Really Cool Python Functions
19:58
Indently
Рет қаралды 43 М.
Faster than Rust and C++: the PERFECT hash table
33:52
strager
Рет қаралды 516 М.
Modern Python logging
21:32
mCoding
Рет қаралды 152 М.
Китайка и Пчелка 10 серия😂😆
00:19
KITAYKA
Рет қаралды 2,1 МЛН