Speed Up Your Code With Cython
Vložit
- čas přidán 28. 07. 2024
- Today we learn how to speed up Python code, using Cython.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: www.neuralnine.com/books/
🐍 The Python Bible Book: www.neuralnine.com/books/
👕 Programming Merch: www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: www.neuralnine.com/
📷 Instagram: / neuralnine
🐦 Twitter: / neuralnine
🤵 LinkedIn: / neuralnine
📁 GitHub: github.com/NeuralNine
🎵 Outro Music From: www.bensound.com/ - Věda a technologie
When using the time module to benchmark something use time.perf_counter() rather than time.time() as it provides more precision and isn't the time since the epoch!
thanks!
I thought it was just a much more accurate time to the nanosecond or something?
@@calvindibartolo2686 I'm late to this but for everyone else, perf_counter_ns() does nanoseconds and perf_counter() does seconds
@@edwardb05 similarly time.time_ns() does nanosecodns
Anyhow, cython is much faster than normal python, we don't even need to talk in nano seconds.
Here I was trying to attach Python to my C, when really I should try attaching C style to Python. 👍 Great vid. You've given me something to consider.
This is actually pretty useful
I wasn't aware that cython was a thing until now
Thanks for this!
Thanks again for a great video. I like that you use Windows as well.
It was cool to see the speed of : flexibility of Python vs rigidness of cython
Great video! I would love to watch more of those Cython vs Python
It's not only about static typing, but also probably about predefined vs dynamic sized array. Dynamical array size definitely comes at a cost.
If both functions are in the same source file, both will be compiled I assume? Have you tried putting the optimised function in a separate pyx file and only compiling that?
Finally you might be able to do number += 2 because prime numbers cannot be even anyway.
This is an example of Cython working better, but how about a comprehensive tutorial on how to use it? Does it work when you're using machine learning libraries?
well if you use it with something like NumPy which is already fast you will achive no speedup at all, if it is even possible
Actually Cython is used most commonly (in my experience) in data science to speed up processor intensive tasks
Most/all machine learning libraries already use well-optimized Assembly/C++/Fortran Code under the hood, so you won`t get any speed improvements.
@@lbgstzockt8493 You will get improvements for the python code you actually use. Especially for expensive tasks.
All respect for you bro 🙏
Will there be a follow up with C-Extension and a comparison with cython?
Very well explained.
There is a way to make your prime detecting more efficient. The only even prime number is 2 so there is no need to test the remaining even numbers. It would be more efficient to insert 2 in the list at the beginning of the program, then start testing at 3 and increment the number to be tested by 2 each time instead of by 1 as you did. This would double the speed of the program since there would be only half as many numbers to be tested. Another thing to consider is that when testing a number for primality it is not necessary to check if any prime less than the number divides it. It is only necessary to check if any prime less than its square root divides it. But I'm not sure how much this would speed up the program (if at all) due to the overhead of calculating the square root of each number to be tested (unless you used a relatively simple formula to approximate the square root instead of calculating a precise value).
The sqrt n method will work faster always as the code will iterate sqrt n times. Calculating the sqrt n won't be affecting much as compared to running code for n/2 or n/4 times. For 1st few numbers, n/2 or n/4 method will dominate and that too even before the number 20. I used the sqrt n method in the super slow bash script and it accurately found that 10247693 is a prime within a second, while Python was even faster than that. Even if you scale it to find all primes over a range, the sqrt method is simplest AND fastest method to find primes
I think the point here was not how to make that particular algorithm faster, but to show the difference between regular python and cython
@jesussevillaperez3639 I am well aware of that. I just wanted to point out an improvement to the algorithm. If my suggestion was incorporated it would still show the increase in speed provided by Cython since both the Python and Cython versions would be faster.
@brucea9871 you suggestion has no value here, he could do anything just to show the difference, it doesn't matter.....
How does Cython compare vs PyPy in terms of speed ?
Woah! What's this for-else code? Can you you use else with for loops? I'll have to look that up. Didn't know about that. Learn something every day.
ty, exactly what I wanted to know.
Thanks u saved my day
I love this video!!!
That's Great👍🏻
Thanks dude 👏👏✌️✌️👍
How does he get the pyx to work. I get a message saying I can’t do it on the community version.
so whats the advantage of using cython instead of ctypes?
it didn't work .when i did the command 'py .\setup.py build_ext --inplace' created a new file with title 'main.c' and written "#error Do not use this file, it is the result of a failed Cython compilation.
" init. What i need to do?
Bro how can i run my python file on android like Suppose i've created a clock.py file and it can run easily in computer but not in android :( please help me.
Sir, can we make exe files of kivy program by simply naming it .pyx and compiling it?
i tried this but cython only created a .c file, no pyd. i cant import the compiled script
Please create a video on python user input in nested autocomplet
When I do "import main" in VSCode, it says "import main could not be resolved"
is there an answer to this question
NameError: name 'exit' is not defined
my exit command gives me this error. How can I fix it?
Thanks for the 'starter'. :-)
Is there a Cuby?
First I Love C# and Python. Now I Love Cython too
The question is, do built-in python functions ;like count method for lists; use Cython?
and if the answer is yes, is it much faster to use count method instead of using for loop in a list?
My general understanding is that built in methods would be faster. Not sure about the Cython though
A lot of built in stuff is optimised, not sure about count though
I mean, you could just test it out ;)
But generally: Yes, built-in functions and functionality is at least optimized but often written in C. So it will easily outperform anything you write in Python yourself.
Why [p for p in primes[:found]] instead of just primes[:found] you're literally iterating through each number of a list, putting it in another list and returning the new list. (?!?!?!?!)
shouldnt you initialise the primes array to size amount
no, u cant initialize the size of an array in runtime, it needs to be clear what size it is in compile time. If u do it in runtime it could give you an stack overflow
what the name of your outro song
me: wants to know about cython because python is soo slow
NeuralNine: I have read your mind, here you go.
Thanks!
I am faster than Cython😅
@@sofianbar.598 😄
😂
Cython app running on vaccum tubes
Hey, what C compiler are you using for windows?
msvc (from Visual Studio Build Tools)
How to decompile cython to python ??
how to make simple program for editing photos like upscaling its resolution
Bro there’s probably quite a bit of fundamentals that go into that. Lol
You might be able to find something online but that’s a project probably for more advanced programmers...not beginners.
Why in prime_finder_optimized you didn't use the append method?
because "primes" here it is a C-array, not a python list.
It's kinda dumb to ask but does cython have use cases ? Python isn't know to be optimised but then instead of using cython why not coding directly in C/C++ ?
Cython definitely helps when doing high priority code. For example, machine learning models can benefit from cython if it's a massive model with massive data sets.
Moreover, Django also uses cython in the background to speed up web development, as you want your server to be fast.
Cython pretty much compiles down to well formatted C code, almost as if a professional C developer wrote it. It does this without you having to even know C at all.
can someone please explain why we used "100000" when defining the "primes"? Why not use something like "primes[amount]"?
In C and C++ array size must be known at compile time. amount is known only at runtime(even if we use constant in parameter when we call the function).
Thx.
How is it compared to vanilla c ?
Because Cython is compiled into C code, I’d imagine they’re equivalent in speed, but I’m also curious about the answer to this question.
I Love Cython too Now
it's not working on big python script's
Hi NeuralNine, great Video, can ask this question : why using Python instead of C++?
because python is cool to write and readable and I hate squiggly brackets.
make a tutorial on compiling .pyx to .c to .exe. The executable should run without any dependencies
Even with imported external pip modules?
Import turtle 🐢 small tutorial video bro 👍👍👍👍
nice video
Subbed
Thanksgiving 💯
What IDE are you using?
PyCharm
Nice
Very nice tutorial.
please also make an Iron python tutorial.
thanks.
When the language is so slow you have to use c code to make it bearable
I learnt many things
But I had a request to plz make a short playlist for these types of all modules which can useable for python
Plz teaches us cython syntax and all stuff in detail as well as pandas and numpy
I want to learn those thing
Plz sir
Can opencv and cython combined?
the thing is most computational libraries already call their own c/c++ code, things like numpy / opencv / tensorflow etc all are already optimized internally
Yeah, you can also work with numpy arrays instead of regular lists and it will work much faster without all the need of that c complexity sintax, but great video anyway, I'll search about this way of programming
Numpy is a C library, certainly knowing cython and C can help you extend numpy and heck even make it faster if you're running into problems you can program it at the C/Cython level to make optimizations.
Wow!!!
nice videööööö :) grüße ;)
Soooooooooo gret video pls make a lot of video about discord boy tnx
Pyd is for Windows. How do we do for Linux ?
Compile it on Linux system or use WSL (Windows Subsystem for Linux)
11:24 break my focus hahaah
cool
Which code editor are you using ?
Pycharm
Looks different to my pycharm…
@@Dbdlkxbdbwk pycharm with vim extension
The one complaint I had with Python was the type ambiguity.
You can provide your type annotations in your python code, mypy can perform the linting, and your IDE the code analysis.
Like!
Personally I don't like Cython, If you want to speed up your python just use numba and with just one decorator you are set to go, and I would rather use Rust and bind it with python with pyo3 rather than using Cython
How To decoded Cython
So it's just Rust?😅
Python cython 😅😂😂😂😂
You made the optimized takes min of 1000 and amount
So in case you pass amount higher than 1000 it will always take the 1000
So all your work is like nothing and not practical at all
why don't my pycharm accept .pyx ? it is just reading it as a text file
mach mal videos auf deutsch auch
Bro telegram bot
Just use cpp lol
second
^^
Me #1k likes
Take regain with cold drink CO2 mix inject method in detal arm be young again
Horrible program
Great demonstration of Cython
Omg, why don't you just program in language that is appropriate to the application.
If you really need so fast module, make a library in c or even assembler if necessary.
It IS a C module, just using Python syntax.
I can't get this to work. Says there's a syntax error in _distutils_hack\__init__.py