@@shreehari2589I think they mean pass the global_var as a parameter like def function(local_var): and then pass global_var when you call the function
No, Python does not check locally then globally. Python knows what is local and what is global at compile time. The difference is that globals is a dict, locals is an array, so local access is direct indexing vs dict look up. You can have a look at disassembly, when python determines the variable is local it uses LOAD/STORE_FAST, vs LOAD/STORE_NAME. The name is quite explicit which is more efficient.
May I know how you have produced the graph of performance. I would highly appreciate if you can create shorts or a detailed video on this. I would like to test few other functions and Optimizations for personal use. Thanks in advance.
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 )
If you want fast code, don't use python. No, don't optimize your python code, write anything that has to be fast in C and use pyhton for things that don't have to be fast.
It would be more optimal to calculate the answer rather than iterate. The sum from 1 to N is N*(N+1)/2. So your code could be changed to global_var * N * (N+1)/2.
If you try writing to global_var it will create a local instance precisely to avoid corrupting it. You have to use the global keyword to specify global_var is global and be able to write to it. But really in the example in the video global_var should just be a parameter that is passed in. 99% of the time globals issues can be solved by just not using them and refactoring your code appropriately or starting with best practices from the beginning.
Man being new to Python and new to coding just sounds like an awful time. Coming into python with years of statically typed OOP these are just things I do automatically lol
Putting aside the fact that the use of global variables sticks out like a sore thumb, this looks like an optimization that the runtime should do for me, and not have me jumping hoops to skirt around the runtime's lousy performance.
I think it is much better to make function with this 'global varible' as argument. Using global variables in functions is bad practise, because it makes functions less portable (you can't just take this function and place it in another program or file, because it may doesn't have this variable in it)
Better - when writing non trivial programs, it's often hard to avoid globals - unless you want to make the call graph a complete mess. If you're determined to kill all globals, you might want to malloc() a singleton (a struct) containing all of them (so you can re-enter the function without side effects) and pass it down all the way - even if you don't use it. That way if you change a function so it requires the use of such singleton, it's there. You might get a few warnings, but it works - and it makes maintenance less of a pain in the neck. BTW, always initialize your global vars in a separate function. You might be in for a surprise if you call it multiple times.
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.
Don't do that. First, you should avoid using a global variable. But let's assume you really need to. Then use the "global" keyword inside of you scope to indicate the interpreter to look directly into the global scope ti retireve the variable. You should also checkout the "lonlocal" keyword, having the same effect but instead of targeting the global scope, it targets the direct upper one.
Could you make a video how I could make a text document where there is a list and in my python program I want a list that is the list of the text document. I would really appreciate it. Thanks for the helpful videos.
Well , this technique won't help when we actually need to update the value of the global variable or it should be mentioned explicitly before return to reassign the local value to global variable. Nice trick btw
I got a question, how would you switch an item between two dictionaries or lisits? Lets say an person between "working" and "on break" lists? One idea i had was this. People = [people names here] Working = [people:] On break=[:people]
This is not true. Bariable scope bindings occur at compile time for python (aka before the interpreter runs the byte code). By default, python does not perform those successive lookups you describe. Rather, if a global variable or a variable in the closure of a function is referenced, the interpreter will produce byte code referencing the variable in the correct location. So this ultimately does not matter.
@@Nip403 advising the use of regex for someone new to programming is mean. Python definitely has a built-in function to check if a str is a valid number before converting.
@@drewsarkisian9375 There's nothing wrong with using Python - but accept that "optimizing" it is useless. If you want raw performance, accept you have to use another language.
You guys know he's probably giving out bad advice so everyone complains in the comment and gets him more engagement rating on youtube right? (you're welcome guy)
@@ZeroSpawn what are you saying? if you want a function to access a variable declared at a global scope you can add the line "global variable_name" below the function definition to make it accessible without redefinition
Adding the global keyword specifically tells Python to WRITE to that global variable if you try to assign to that name instead of the local namespace, so if you want to ensure that your function code can't modify the global variable, that's a bad way to go.
Right. If you want to make changes to the global_var using the for-loop, you could still use this method and after the for-loop is finished, re-assign the global_var with the new local_var. This would prevent searching globally each for-loop iteration.
@@peter9477 Python DOES have a cache! Otherwise how will it find the reference to the functions? It does cache variables, how do you think it invokes and references them? Why bother using "global" then? Well, to know that it's the global variable that you're using, i.e., set a reference to it when it appears -> a cache a reference, a memory slot that points towards the specific variable....
@@Andrumen01 I've been using Python since 1999 and I've never seen any Python expert refer to any of the name resolution stuff as a "cache". Yes, it has namespaces, which you might think of that way, but those aren't caches per any conventional definition of the word. They're just dictionaries. As for how it finds stuff: 1. Locals (which aren't "cached" but compile-time indexed), 2. Module-level globals (a dictionary), and 3. Builtins (a true global namesake, also a dictionary). To the extent that you think of a cache as being any place where things are put and later found... okay, go ahead and call it a cache. I don't think anyone else does though.
By the way, you referred specifically to "the functions cache" yet you were talking about globals, which are absolutely not in a function. The fuzziness in your terminology makes me suspect you're using these words imprecisely, without strong meanings, in which case please just trust me: nobody calls these things "caches" in Python.
Sir can you please explain.which one is better make a program normally or using def() function to create a program.which one is more optimized😊.so plz reply 🙏
Another alternative is to create a default parameter for the function.
I was just about to ask this. Thanks for pointing this out.
Can you give an example
@@shreehari2589def func_name(local_var):
when using function in main code
func_name(global_var)
@@shreehari2589def func(local_var = 10) You can call the function with a custom local var or use 10 as default if none specified
@@shreehari2589I think they mean pass the global_var as a parameter like def function(local_var): and then pass global_var when you call the function
... or just pass it to the function as an argument, not only more efficient but also way easier to read
This doesn't have to semantically make sense at all
kinda annoying to keep track of the variables and has to unnecessarily place arguments everytime u call the function
This is common in Pure Functional Programming
@@vulpritproozeSo, you can set default parameters
best python optimization trick: switch to another language
thanks to this trick I managed to get a 50000% performance increase!
I switched to NodeJS and then bun.sh!
@@sanchogodinho lmaooo, yeah great... optimization I guess
Thank you❤I started learning binary code😍😍😍🥰🥰😍
@@firstnamelastname2775 nice, congratulations :)
def func(size, mult):
return mult * ((size**2 - size) / 2)
print(func(1000, 10))
then add some math :p
No, Python does not check locally then globally. Python knows what is local and what is global at compile time. The difference is that globals is a dict, locals is an array, so local access is direct indexing vs dict look up. You can have a look at disassembly, when python determines the variable is local it uses LOAD/STORE_FAST, vs LOAD/STORE_NAME. The name is quite explicit which is more efficient.
May I know how you have produced the graph of performance.
I would highly appreciate if you can create shorts or a detailed video on this.
I would like to test few other functions and Optimizations for personal use.
Thanks in advance.
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 )
If you want fast code, don't use python. No, don't optimize your python code, write anything that has to be fast in C and use pyhton for things that don't have to be fast.
also lookup the phrase 'premature optimization'
make code harder to read for dubious gains
well but actually you don't use the global variable in the first place
Insted of doing that just assign a variable inside the loop.😊
Thanks for that, im learning Python coding, so it will help me in improving coding skills
If you had an (inexplicable) reason to make the variable global, you wanted it updated within the function.
Interesting. Didn't know it impacts performance that much.
It would be more optimal to calculate the answer rather than iterate. The sum from 1 to N is N*(N+1)/2. So your code could be changed to
global_var * N * (N+1)/2.
This is actually useful, I'll try to keep it in mind.
Your first mistake was using python if you're worried about performance
This is the exact type of thing a compiler would optimize for.... oh wait
I do this a lot when building a data-cleaning function. It also avoids mistakingly corrupting the global variable.
If you try writing to global_var it will create a local instance precisely to avoid corrupting it. You have to use the global keyword to specify global_var is global and be able to write to it. But really in the example in the video global_var should just be a parameter that is passed in. 99% of the time globals issues can be solved by just not using them and refactoring your code appropriately or starting with best practices from the beginning.
Where’d this guy go I like these videos
If you’re good at python, you should do the multiplication after the summation, since they’re separable.
The synthwave theme extension though. My man 👌
Can you do a video on how you test performance like that?
Man being new to Python and new to coding just sounds like an awful time. Coming into python with years of statically typed OOP these are just things I do automatically lol
The best optimization trick for Python is to write the performance-heavy bits in C++ and use Python as a frontend
Putting aside the fact that the use of global variables sticks out like a sore thumb, this looks like an optimization that the runtime should do for me, and not have me jumping hoops to skirt around the runtime's lousy performance.
I think it is much better to make function with this 'global varible' as argument. Using global variables in functions is bad practise, because it makes functions less portable (you can't just take this function and place it in another program or file, because it may doesn't have this variable in it)
Better - when writing non trivial programs, it's often hard to avoid globals - unless you want to make the call graph a complete mess. If you're determined to kill all globals, you might want to malloc() a singleton (a struct) containing all of them (so you can re-enter the function without side effects) and pass it down all the way - even if you don't use it. That way if you change a function so it requires the use of such singleton, it's there. You might get a few warnings, but it works - and it makes maintenance less of a pain in the neck. BTW, always initialize your global vars in a separate function. You might be in for a surprise if you call it multiple times.
When nanoseconds make a difference.
just write "global global_var" before using the global_var in function
Alternative: use C
This is the why you must use memory managed languages such as c/cpp or go
wow didn't know the python interpreter was so bad, this should be optimized automatically
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.
Right? I thought he was going to say to pass it into the function as a parameter, since he said “new to python” but I guess not lol
look it's bad practice but people use globals and sometimes they provide value too
Especially his changes also changed the whole program's behaviour as Global_var is no longer updated.
Question, how is he even getting the global variable to work in his function without write
global (var_name)
at the top of the function?
so you never use a global variable / constant in your code?
btw for that function couldn't you do some math trickery
Thanks!
Which program you used to see performance difference?
Came here to ask the same thing 🤔
looks like seaborn
Likely a python library like seaborn or matplotlib
See module timeit
Thanks for the tip
I taught im the only one who using synthwawe 84 material theme
Don't do that. First, you should avoid using a global variable.
But let's assume you really need to. Then use the "global" keyword inside of you scope to indicate the interpreter to look directly into the global scope ti retireve the variable. You should also checkout the "lonlocal" keyword, having the same effect but instead of targeting the global scope, it targets the direct upper one.
1. You shouldn't be using globals anyway.
2. If you need performance, you shouldn't use Python.
And that's exactly why interpreted languages suck. The compiler would do that optimization for you in any decent compiled language.
Could you make a video how I could make a text document where there is a list and in my python program I want a list that is the list of the text document. I would really appreciate it. Thanks for the helpful videos.
Well , this technique won't help when we actually need to update the value of the global variable or it should be mentioned explicitly before return to reassign the local value to global variable.
Nice trick btw
Or just don’t use globals, only if they’re constant. Python doesn’t specifically implement constants but you can denote them with capitalization.
I got a question, how would you switch an item between two dictionaries or lisits? Lets say an person between "working" and "on break" lists?
One idea i had was this.
People = [people names here]
Working = [people:]
On break=[:people]
another alternative is to not use python in iterative tasks.
Remember friends: Premature optimization is the root of all evil. Benchmark before you optimize.
Best python optimization is switching to Nim
There's no way the interpreter doesn't catch that, or maybe I'm just too used to real compilers. I'll test it
Python for CPU intensive tasks...
Another W for compilers
Alias everything… unless you have to update. 😉
What is your theme it is veryy veryyy beautiful
There's no way this works. Scopes of variables are determined on compile time?
well actually, you should pass it as an argument
is this new? i thought global variables were limited to the outer scope unless you used the global key.
cant wait for the livestream
But don't it does the same when you define it ? Search locally, then outside ?
Why is it searching like that? Why doesn't Python know beforehand the function's local variables?
doesnt that also make a copy of the global var? like any changes to the global var wont be kept outside of the function's scope
Is it just me feel anxiety when there isn't an empty line at the end of file?
It's mind boggling if this is not optimized automatically 😂😢😢😢
function parameters: exists
I was using this for convenience but it is optimization aswell 😅
What font and theme are you using?
How did you make performance graph?
This is not true. Bariable scope bindings occur at compile time for python (aka before the interpreter runs the byte code). By default, python does not perform those successive lookups you describe. Rather, if a global variable or a variable in the closure of a function is referenced, the interpreter will produce byte code referencing the variable in the correct location. So this ultimately does not matter.
Solution: don’t use for loops in python
Bro , I want to take input from the user . But the input should be a number b/w 1 and 62 , what should I do to remove errors and take the input
Either regex or try-except int(input()) or .isdigit string method, then if/assert converted_to_int in range(1, 63)
@@Nip403 will definitely try
@@Nip403 advising the use of regex for someone new to programming is mean.
Python definitely has a built-in function to check if a str is a valid number before converting.
I need this theme
It still has to find the global variable information tho.. so it still goes up to look for it right?
Optimizing python is like polishing a turd
Yeah but I guess no fucking one use it for performance anyway 😂
As usual, can't let folks alone who like Python (for all its quirks) and actually add some useful information. "Thanks" for sharing your "wisdom".
@@drewsarkisian9375 Its the Duplo of programming
@@drewsarkisian9375 There's nothing wrong with using Python - but accept that "optimizing" it is useless. If you want raw performance, accept you have to use another language.
how did u do the graph? just used data to create it or some website or smth
Use numpy or some library written in C instead of native Python
If that's not enough then ditch Python; you're using the wrong tool for the job
Please can you show us how you check the performance fir both options
you can run the function 10k times and store the start and finish time with time.perf_counter()
You guys know he's probably giving out bad advice so everyone complains in the comment and gets him more engagement rating on youtube right? (you're welcome guy)
Those two runs with 30% increase:🗿
It’s because of background noise
Right that way it already has the variable to access
or you can just say global var_name above the for loop
Well if that Global variable is needed somewhere else one would have to dive into the for Loop to get the value.
@@ZeroSpawn what are you saying? if you want a function to access a variable declared at a global scope you can add the line "global variable_name" below the function definition to make it accessible without redefinition
Adding the global keyword specifically tells Python to WRITE to that global variable if you try to assign to that name instead of the local namespace, so if you want to ensure that your function code can't modify the global variable, that's a bad way to go.
yk python is slow when it takes 7% of the performance to access a variable out of scope
This is like putting lipstick on a pig
How to show that result test?
.. WRITE PURE FUNCTIONS! :')
Does anyone know this theme? I really like it.
First optimization, move to C or C++ or Go.
Rumors say function parameters exist.
One alternative I really like, is just using C.
Good one.
What if I want to make changes to the global variable? I'll have to use global or globals() right?
Right. If you want to make changes to the global_var using the for-loop, you could still use this method and after the for-loop is finished, re-assign the global_var with the new local_var. This would prevent searching globally each for-loop iteration.
just use rust if you need speed😊
Or just use the "global" keyword inside and it will create a reference in the function's cache...
No it will not. There is no such "cache" and the global keyword merely prevents the compiler from creating a local variable if you re-assign the name.
@@peter9477 Python DOES have a cache! Otherwise how will it find the reference to the functions? It does cache variables, how do you think it invokes and references them? Why bother using "global" then? Well, to know that it's the global variable that you're using, i.e., set a reference to it when it appears -> a cache a reference, a memory slot that points towards the specific variable....
@@Andrumen01 I've been using Python since 1999 and I've never seen any Python expert refer to any of the name resolution stuff as a "cache". Yes, it has namespaces, which you might think of that way, but those aren't caches per any conventional definition of the word. They're just dictionaries. As for how it finds stuff: 1. Locals (which aren't "cached" but compile-time indexed), 2. Module-level globals (a dictionary), and 3. Builtins (a true global namesake, also a dictionary). To the extent that you think of a cache as being any place where things are put and later found... okay, go ahead and call it a cache. I don't think anyone else does though.
By the way, you referred specifically to "the functions cache" yet you were talking about globals, which are absolutely not in a function. The fuzziness in your terminology makes me suspect you're using these words imprecisely, without strong meanings, in which case please just trust me: nobody calls these things "caches" in Python.
@@peter9477 I do...so....there is somebody who uses it. Imprecise, in your opinion...or somebody elses...not mine!
hi, what font did u use? ty
How to check performance results can u tell me ?
Best perfomance boost - use another language.
I stopped at GLOBAL VARIABLE
helpful 💯😌
Another way is to stop using global variables
Nice one
Sir can you please explain.which one is better make a program normally or using def() function to create a program.which one is more optimized😊.so plz reply 🙏
what's the name of the application? newbie here