Turn Python BLAZING FAST with these 6 secrets

  Рет қаралды 30,839

Dreams of Code

Dreams of Code

Күн бұрын

Don't assume Python is slow. These are some of the best ways to turn python into a language that is comparable for runtime performance to other known fast languages. Here, we look at six different approaches to turn your python code into some of the fastest code out there.
of these, my favorite is using pypy, which I think is one of the easiest ways to get more performance.
Source code: github.com/dreamsofcode-io/py...
#python #fast #coding #developer #programming #optimizing #concurrency #pypy
00:00 Intro
00:15 Built-in functions
00:52 Be Lazy with Generators
01:47 Use Concurrency
02:39 Cython
03:24 Using Compiled Frameworks
03:52 pypy
04:40 outro

Пікірлер: 51
@xhenryx14
@xhenryx14 Жыл бұрын
Numba is really fast! The problem is it doesn't support objects, but is really good for small numerical functions
@dreamsofcode
@dreamsofcode Жыл бұрын
I should look into Numba! That's a great tip, thank you!
@RameshBaburbabu
@RameshBaburbabu 6 ай бұрын
🎯 Key Takeaways for quick navigation: 00:00 🚀 Leveraging Built-in Functions for Speed - Using built-in functions from the standard library boosts Python's speed. - Comparison of a custom sorting algorithm against the built-in sorted function. - Built-in functions are often faster due to being written in C. 01:10 🔄 Laziness and Generators in Python - Embracing laziness as a virtue in coding for efficiency. - Utilizing the yield keyword to create generator functions. - Generators help with large data sets, avoiding expensive memory allocation. 02:32 ⚙️ Enhancing Performance with Concurrency - Introducing concurrency using the multi-processing library. - Exploring the concept of embarrassingly parallel problems. - Demonstrating the efficiency gain through concurrent image processing. 03:14 🛠️ Code Compilation with Cython for Optimization - Using Cython to compile Python-like code to C for performance improvement. - Optimizing specific parts of the codebase, not a full replacement for Python. - Significant performance improvement demonstrated with a factorial calculation. 03:42 📚 Harnessing Compiled Libraries and Frameworks - Leveraging compiled libraries and frameworks like NumPy, Pandas, and Pillow. - Exploring performance benefits by tapping into C implementation. - Enhancing code readability while maintaining performance through these frameworks. 04:10 🚀 Boosting Speed with PyPy Interpreter - Introducing PyPy as an alternative Python interpreter for improved speed. - Just-in-time compilation method for forward-looking code compilation. - Consider benchmarking code with both PyPy and CPython for optimal performance.
@jeffreyhymas6803
@jeffreyhymas6803 10 ай бұрын
This might be a bit nitpicky, but concurrency != parallelism. If you're using the multiprocessing library you're executing your code in a truly parallel fashion since it spawns new processes each with their own interpreter running, but you don't have to do that to run code concurrently. Asyncio and the threading library will both allow you to make your code concurrent, without needing new processes. If your tasks are largely io bound, asyncio or threads are usually a better choice, while multiprocessing is better for CPU bound processes (generalizing of course). Multiprocessing isn't always faster either. Depending on the number of threads and complexity of the problem, it might not be worth incurring the additional overhead to spawn new processes. And on top of all that you're adding complexity to your code. Concurrency/parallelism aren't easy. So all that is to say, it's a nuanced topic and might not be the best example of an easy or effective way to improve the performance of your code.
@electrolyteorb
@electrolyteorb 25 күн бұрын
"Concurrency is all about managing many tasks at once Parallelism is about doing many tasks at once"
@vincentbenet
@vincentbenet 9 ай бұрын
Once, one of my programm in python tooked 35min to process, a collegue ported this to a python rust-based librairy and the script was running in less than a second.
@dona_telo5378
@dona_telo5378 2 ай бұрын
Polars ? 😂
3 ай бұрын
Nice video. Nice Vim editor too. Thanks!
@user-pw5do6tu7i
@user-pw5do6tu7i 4 ай бұрын
if you have code generating code in a build step, using pickling is sometimes much faster when you use it at runtime. I think it skips having to serialize it all again.
@scotth8828
@scotth8828 Жыл бұрын
Great ideas! I got some code that runs multiple dd commands and is dog-faced slow. I'll try some of these like pypy and cython to see if it increases the speed One thing that I do: instead of adding characters to a string I use a list and append items to that list and use " ".join(list_in) to create the string at the end. Ex. if you're using st1 = st1 + new_char and you're creating an 80 character line you'll have 3200 immutable characters because of all the strings. by using lst_st1.append(new_char) with a " ".join(lst_st1) at the end you have 160 immutable characters.
@Kralnor
@Kralnor Күн бұрын
And even better if you can generate the list by comprehension instead of appending.
@storyxx
@storyxx Жыл бұрын
You have to be careful with multiprocessing though. Since Python is interpreted and e.g. CPython only offers a single Interpreter instance, multiprocessed code can actually be slower, because only a single Interpreter is performing the operations and context switches in between take time (see Global Interpreter Lock). It works well for IO bound operations though, like you showed in the video :)
@dreamsofcode
@dreamsofcode Жыл бұрын
I believe you're correct for Python threads, but multiprocessing actually forks new processes which have their own GIL (global interpretor lock). It can be slower however due to the overhead of creating more OS processes, and having to serialize and deserializd the data to those processes, so definitely worthwhile to check what is more performant!
@ewerybody
@ewerybody 9 ай бұрын
Make use of set and dict which use internal hashing to improve lookups insanely! (I used to amount stuff in lists and looped over them ... that was SO bad!)
@dreamsofcode
@dreamsofcode 9 ай бұрын
I actually have a video about this coming out!
@pingmetal
@pingmetal Ай бұрын
@@dreamsofcode Still planning to release it someday?
@dreamsofcode
@dreamsofcode Ай бұрын
@@pingmetal Haha I actually still have this video in my backlog. It's evolved somewhat since the original conception however.
@user-zi2zv1jo7g
@user-zi2zv1jo7g 26 күн бұрын
Not that I've looked too deeply into it, but the generators example seems wrong, it seems like the time save comes from not using .split in the generator version
@Luix
@Luix Жыл бұрын
How does QT generate the binaries?
@jameswalker199
@jameswalker199 4 ай бұрын
Possibly a dumb question, but is it not possible to just compile Python like you would any other language? Pre-interpret it, if you will? Speed isn't my biggest concern, as long as the machine does it faster than I can (I'm not a big, professional developer), so I've never really thought about it until now.
@CramBL
@CramBL 3 ай бұрын
"like you would any other language". The approach varies wildly between languages. The stages from the language we implement it in, to the machine code that the CPU (or GPU) executes is rarely the same between languages, even seemingly similar languages like C and C++. There's stage after stage after stage (especially for C++), until it reaches a point that it can be translated to some flavor of assembly and then to machine code. The "easiest" way to compile python would be to transpile it to a language that you can compile. And then Python is just a frontend to that language. The second easiest would probably be to implement a way to translate Python into some flavor of LLVM intermediate representation (IR) and then compile it to machine code from there. That is an approach many languages take, such as Rust, Zig and dozens of esoteric languages. Now you need to define a Python standard that describes how you go from arbitrary valid Python to LLVM IR. It would probably require Python4, otherwise I assume it would already exist.
@SharunKumar
@SharunKumar Жыл бұрын
0:30 - seems like you're creating new arrays for each partition (quick sort) but that defeats the purpose of quick sort which is supposed to be in place sorting algorithm 😢
@31redorange08
@31redorange08 3 ай бұрын
So he actually is bad at writing code. Welp.
@blankRiot96
@blankRiot96 Жыл бұрын
I like the representation
@robert_nissan
@robert_nissan 27 күн бұрын
poderoso
@profesionalfailer
@profesionalfailer Жыл бұрын
Which is the fastest among these?: - Yield generators - Inline generators - List comprehensions
@dreamsofcode
@dreamsofcode Жыл бұрын
Great question! - List comprehensions arent lazy evaluated to my knowledge so they'll likely not be as performant. - I'm unsure about inline generators so would need to look those up to give a concrete answer.
@almuaz
@almuaz Жыл бұрын
List comprehensions is the fastest in general. but it may vary based on your code and other workflow and objects.
@AxidoDE
@AxidoDE 13 күн бұрын
Remember, folks: The secret to making Python run fast is to use as little Python as possible.
@kovlabs
@kovlabs 10 ай бұрын
How about using maturin and Rust for those intense operations
@dreamsofcode
@dreamsofcode 10 ай бұрын
Don't tempt me. I've got a video on making python faster with rust 😅
@onogrirwin
@onogrirwin 2 күн бұрын
0:45
@musluy
@musluy 7 ай бұрын
Still waiting for PyPy
@deadeye1982a
@deadeye1982a 3 ай бұрын
Using mmap to create a hash from a file. This is not much faster than the approach with a buffer.
@igorlukyanov7434
@igorlukyanov7434 Жыл бұрын
The best method to improve speed of Python code is to use C++.
@dreamsofcode
@dreamsofcode Жыл бұрын
Hahah or C. There's also some other languages one can use 😉
@learning_rust
@learning_rust Ай бұрын
@@dreamsofcode eg Rust! 😉
@31redorange08
@31redorange08 3 ай бұрын
As expected: Turn Python fast by using other languages.
@OmarHashimOAD
@OmarHashimOAD 19 күн бұрын
which is one of the core concepts of the language
@spidertyler
@spidertyler Жыл бұрын
Pypy made my code slower. Although my code was running in only a few hundreths of a second so.... maybe that was my fault for expecting it to be faster...
@dreamsofcode
@dreamsofcode Жыл бұрын
Haha. I should do a video on when to use pypy. It's mainly best for when you have code that is called multiple times. Such as in a loop. Otherwise the JIT compilation is wasteful.
@spidertyler
@spidertyler Жыл бұрын
@@dreamsofcode it looped thru 1.5k iterations
@dreamsofcode
@dreamsofcode Жыл бұрын
Interesting, what was the code doing in those iterations?
@spidertyler
@spidertyler Жыл бұрын
@@dreamsofcode it was a parser. It would take a look at a list of tokens, compare it to some expected tokens, if the checks passed it would consume the tokens and replace them with a new one. That process would repeat until it got to the end. The input file was 200 lines, but i couldnt give you a number of tokens. It was probably a few hundred
@dreamsofcode
@dreamsofcode Жыл бұрын
Thank you for sharing. I've got a video planned with Pypy in the future so I'll add parsing + lexing as a benchmarking case!
@secretterminal2179
@secretterminal2179 10 ай бұрын
Secret number one: use c++
@dreamsofcode
@dreamsofcode 10 ай бұрын
Hahaha
@official_mosfet
@official_mosfet Ай бұрын
Python is just slow if you don't know how to use it.
@OmarHashimOAD
@OmarHashimOAD 19 күн бұрын
pure python is slow but you never meant to use "pure python" and that why the Standard library exist.
Compiled Python is FAST
12:57
Doug Mercer
Рет қаралды 84 М.
Make Python code 1000x Faster with Numba
20:33
Jack of Some
Рет қаралды 438 М.
СҰЛТАН СҮЛЕЙМАНДАР | bayGUYS
24:46
bayGUYS
Рет қаралды 465 М.
Teenagers Show Kindness by Repairing Grandmother's Old Fence #shorts
00:37
Fabiosa Best Lifehacks
Рет қаралды 44 МЛН
NO NO NO YES! (50 MLN SUBSCRIBERS CHALLENGE!) #shorts
00:26
PANDA BOI
Рет қаралды 98 МЛН
Using docker in unusual ways
12:58
Dreams of Code
Рет қаралды 384 М.
Diagnose slow Python code. (Feat. async/await)
9:57
mCoding
Рет қаралды 208 М.
How I accidentally learned Python in 6 months
4:57
Thaomaoh
Рет қаралды 7 М.
Python's 5 Worst Features
19:44
Indently
Рет қаралды 76 М.
How to use Cython to speed up Python
7:56
InfoWorld
Рет қаралды 5 М.
Cython makes Python INSANELY FAST
19:08
Carberra
Рет қаралды 31 М.
PLEASE Use These 5 Python Decorators
20:12
Tech With Tim
Рет қаралды 87 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 77 М.
How to actually make your Python code run faster?
12:02
Tech Raj
Рет қаралды 6 М.
Дени против умной колонки😁
0:40
Deni & Mani
Рет қаралды 7 МЛН
Samsung or iPhone
0:19
rishton vines😇
Рет қаралды 8 МЛН
Nokia 3310 versus Red Hot Ball
0:37
PressTube
Рет қаралды 2,6 МЛН
IPad Pro fix screen
1:01
Tamar DB (mt)
Рет қаралды 5 МЛН
How much charging is in your phone right now? 📱➡️ 🔋VS 🪫
0:11