Python OPTIMIZATION Trick!! #python #programming #coding

preview_player
Показать описание

Background Music:
Creative Commons / Attribution 4.0 International (CC BY 4.0)
Рекомендации по теме
Комментарии
Автор

Another alternative is to create a default parameter for the function.

TheCircuitProject
Автор

It’s common practice to also avoid using global variables as much as possible. Only use them as constants.

ItzJustJohn
Автор

best python optimization trick: switch to another language

thomquiri
Автор

... or just pass it to the function as an argument, not only more efficient but also way easier to read

nikkehtine
Автор

"All that for a drop of blood?" - Thanos.

FictionHubZA
Автор

there's the `global` keyword, or `nonlocal` for nested functions

instead of creating local_variable, you can just declare `global global_variable` at the top of the function

sami
Автор

I prefer passing the required values into the function as arguments. If I need to reuse a function in a different script, I know by the function signature exactly the function requires to run.

rnseby
Автор

I believe this is incorrect, any recent (python 3 and up) will emit a LOAD_FAST for locals (like ans and i) but a LOAD_GLOBAL for nonlocal, non-cell variables (like global_var). The speed difference between LOAD_FAST and LOAD_GLOBAL is pretty much negligible, especially with the adaptive opcodes in newer versions where most of these have faster, adaptive versions.

Martmists
Автор

If you need enough performance that something like this becomes worth it, you now are in need of a higher performance language.

pixelprizm
Автор

The best optimization trick in this case is not bringing the global_var into the local scope as local_var. It is going back to 5th grade and learning that you can simplify this function to
def func():
return global_var * 1000 * 1001 / 2

(**exceptionally hard to do**)

AndrasBalintBoroczky
Автор

That's what we call a premature optimization.

serggie
Автор

This is not true. Variable scoping is done at compile time (yes, Python has one), not at runtime. It's actually the only complex thing Python compiler does.

You can look through the related data structures using the symtable module.

It's also visible if you use dis.dis on each version of the function - one will have LOAD_FAST op, the other will use LOAD_GLOBAL. It's predetermined.

__mrmino__
Автор

Sorry this is just very bad. Really, really, really bad. NEVER teach people how to optimize the use of global variables. Teach them how to avoid them alltogether.

TheKilledDeath
Автор

Definitely use this trick to run out of variable names

tahaahmedmallick
Автор

The same point applies to attribute lookup on imported modules. For example, you can assign imported function to a local variable to avoid an expensive lookup.

Let's say you use "math.sin" in a loop. Every time you call it, you perform a lookup on "math" module for "sin" attribute before calling that function. If you assign "sin = math.sin", you get rid of that unwanted lookup from "math" module and your loop with local "sin" will be faster.

If you have a function that creates a lot of objects, you might also want to assign the class locally to also avoid global lookup for the same reason.

If you find it interesting, you are dealing with some problem that Python might not be the best language to solve it in. Still, if you have to resort to those optimizations, it's at least nice to know about them.

viCoN
Автор

A rule of thumb I have is to maximize clean encapsulation like in pure functional programming. Everytime I want to use a global state, I pass that state by reference.

This is so that you know which function is modifying what at any given time via the function calls.

It's really hard to spot any modification or references to global state if you directly refer to it within a method.

macchiato_
Автор

Bro I can't even here you over this beat. It's slaps more than I could have imagined 😂

richardmelendez
Автор

Is this why pointers are used in C and C++?

MortonMcCastle
Автор

One alternative I really like, is just using C.

linuxguy
Автор

This problem is the same in every interpreted programming language ( it doesn't affect performance on compiled langs cuz this only happends while compiling, not when running )

lolcat