“NEVER Worry About Performance In Python” #python #programming #coding

preview_player
Показать описание
Idiotic things that lazy Python devs say 😂 #python #code #programming
Рекомендации по теме
Комментарии
Автор

Dynamic programming :
I'm a joke for you?

LuizGustavo-dzwo
Автор

You should always make major optimizations, but if you're doing micro optimizations in Python, you're doing it wrong.

LaughingOrange
Автор

2 things
-You can just use @cache instead in 3.9+
-careful using this inside of a class, as it can prevent the class from being garbage collected when deleted, causing big memory leaks

stinkykyle
Автор

"never be worried about performance in python" - because if you're worried about performance, you shouldn't be using python

ThatJay
Автор

Just because you're using Python, doesn't mean you should just accept the fate of the slow speed of your script, what's important is that you follow the best practises for making it perform to the best of its ability (I'm NOT talking about micro-optimisations).

There are plenty of neat tricks out there for making your script perform much faster. What I showed you in the video is just one of them.

NOTE: @lru_cache isn't a completely free life hack in Python, it does start to eat memory which is something you should factor in!

Indently
Автор

Hey, from C++ here; I am loving python's version of format strings, it looks super clean and unambiguous to work with, C++ has it too though(the version implemented was adapted from the python standard even).

cyrilemeka
Автор

Literally writes the worst algorithm out there for Fibonacci numbers. Imports a library to fix the issue.
Typical pythoner behavior

tremon
Автор

for those who want to master this topic it's called Dynamic programming but I should warn you it will seem hard but it's really amazing and it will enhance your programming skills and take it to another level

osamaxz
Автор

Or you can implement your own dp array. For factorial you can also calculate it in place without recursion .

abdulrahmanabunabhan
Автор

Why not just use an array of 3 elements? The first 2 will be the numbers you sum, and the third is the result. Then you shift the numbers back dropping the front, and keep going. It's more space efficient and just as fast if not faster since you don't have to look through a cache and the answer is predictable every time

DaFrancc
Автор

For particularly math-based results, you can learn different ways to compute a certain result mathematically to heavily reduce complexity. For example, in the case of a fibonacci sequence, there is a formulaic and non-sequential method to generate the nth fibbonacci number, which would probably speed up the process really quickly.

jasonlu
Автор

it's neat, still not gonna use recursive functions though

FireSnake
Автор

Good to know! When I did something like this last time, for Stirling numbers I think, I coded a cache manually, just consulting a dictionary of previously used values. This seems faster to code and is probably more optimized.

Elyandarin
Автор

Not worrying about algorithmic complexity is obviously not what is meant.

elliuozaG
Автор

The best solution îs actually to write it in C, compile it, and execute it from the Python side

RomanianProductions
Автор

Well, regardless of language, speed can always be achieved by lowering the time complexity. However, If the constant factor is too large, then the language is important.

So in other words. If your program is slow, first blame the algorithm before you blame the language, cause the algorithm usually has a larger impact on performance than the language.

dominobuilder
Автор

Yeah, it's O(n) now, but you can do it O(log n) using formula

drexon
Автор

Would love some optimization tips for data analysis and machine learning.

maxrinehart
Автор

Or, you know. Use the non-recursive/non-iterative formula that given an n returns the nth Fibonacci number. Upon initializing the program, calculate sqrt(5), phi, and psi (since these are constants and don't need to be recalculated each time they're used). Then create your fib function to just return (phi**n-psi**n)/sqrt5.

Not only is this going to be more efficient than the recursive version for all but the smallest n, it generalizes not just to integers for inputs but all the way through to the complex numbers (though you'll need to pull in cmath or numpy or similar to pull off the complex expansion).

lrwerewolf
Автор

In Perl for this is the Memoize module. It has NORMALIZER, INSTALL options that can be used, making it even crazier.

vilijanac