Python OPTIMIZATION Trick!!

  Рет қаралды 767,889

b001

b001

Күн бұрын

⭐ Join the Byte Club to practice your Python skills! ($2.99/mo): / @b001
🐦 Follow me on Twitter: / b001io
Background Music:
Rain, Book And Cup Of Tea by | e s c p | escp-music.bandcamp.com
Music promoted by www.free-stock-music.com
Creative Commons / Attribution 4.0 International (CC BY 4.0)
creativecommons.org/licenses/...

Пікірлер: 833
@TheCircuitProject
@TheCircuitProject 8 ай бұрын
Another alternative is to create a default parameter for the function.
@Schecterbaby
@Schecterbaby 8 ай бұрын
I was just about to ask this. Thanks for pointing this out.
@shreehari2589
@shreehari2589 8 ай бұрын
Can you give an example
@beepbeepgamer1305
@beepbeepgamer1305 8 ай бұрын
​@@shreehari2589def func_name(local_var): when using function in main code func_name(global_var)
@fres621
@fres621 8 ай бұрын
​@@shreehari2589def func(local_var = 10) You can call the function with a custom local var or use 10 as default if none specified
@zimzimph
@zimzimph 8 ай бұрын
​​@@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
@ItzJustJohn360
@ItzJustJohn360 8 ай бұрын
It’s common practice to also avoid using global variables as much as possible. Only use them as constants.
@novianandrian
@novianandrian 8 ай бұрын
can you explain why we should avoid global variable? I am new to python and been seeing alot of this information, but quite unsure how to workaround on that.
@badlydrawnjolteon646
@badlydrawnjolteon646 8 ай бұрын
​@@novianandrian global variables sow the seeds of terribly bad practices in learners, and experts never use them anyway. the reason why is because new learners dont fully grasp what a function is. it isnt code you just put under a `def` block because thats what you think you are supposed to do. its not even just "repeatable" code. a function is essentially a "sub"program. suppose you are making a library, and someone imports your functions. that function is a black box to them, and they cant see that it wants to use "x" from the global scope. if the function tried to, and they actually did have an "x" in their global scope, even though they didnt know that was part of the use case, they would almost certainly run into inaccurate outputs with absolutely zero clue as to where said output was coming from, and literally no possible way to debug that. but thats not just to say that globals are bad only in the context of libraries, using global variables like this is genuinely harmful no matter what. they direct your code into a very specific architecture which is completely unmaintainable, unreadable and harder to debug. they promote misconceptions on what functions are and how they are meant to interact with a program. and you will never, ever be able to advance beyond basic scripting while you still maintain this habit, because projects that are better formatted in multiple files are practically a nightmare to debug or expand when even a few globals are being thrown around between scopes and files. in short, one of the biggest things i would tell anyone at this point in their journey to learn programming is to make sure that in every circumstance your functions are able to be run on their own. all of the information they need to function fully should be passed in through parameters. if you feel as though you cant do this for your specific case, it may be a sign that your code architecture is wrong, as I have never had that actually be the case in over 6 years, nor have I heard of anyone who has.
@the_cheese_cultist
@the_cheese_cultist 8 ай бұрын
​@@novianandrianbecause it creates a lot of dependencies for your function, and makes the code less organized either pass it as a paramater, or create a class and make the function a member of the class using it for constants is fine not using global variables is a good practice for all programming languages
@stilyandimitrov39
@stilyandimitrov39 8 ай бұрын
​@@the_cheese_cultist when you have code that might need constant change or update don't global variables make everything faster? Im doing my capstone college class and im working on a website that deals with different aspects of CSV data surveys. and i approached it with the mindset of "hey making everything global makes it easier to edit later. (side not it was inherited from last year and the whole approach the other team started with was a mess so we could only focus on refactoring.)(we worked with ruby on rails )
@the_cheese_cultist
@the_cheese_cultist 8 ай бұрын
@@stilyandimitrov39 I'm gonna need more info to give you an opinion, but there are many sources online about good coding practices, avoiding globals included, that can explain everything better than a KZbin comment.
@ElectrostatiCrow
@ElectrostatiCrow 19 күн бұрын
"All that for a drop of blood?" - Thanos.
@nikkehtine
@nikkehtine 8 ай бұрын
... or just pass it to the function as an argument, not only more efficient but also way easier to read
@seriouslyWeird
@seriouslyWeird 7 ай бұрын
This doesn't have to semantically make sense at all
@vulpritprooze
@vulpritprooze 7 ай бұрын
kinda annoying to keep track of the variables and has to unnecessarily place arguments everytime u call the function
@Rudxain
@Rudxain 6 ай бұрын
This is common in Pure Functional Programming
@thomquiri9860
@thomquiri9860 8 ай бұрын
best python optimization trick: switch to another language
@lobotomy-victim
@lobotomy-victim 8 ай бұрын
thanks to this trick I managed to get a 50000% performance increase!
@sanchogodinho
@sanchogodinho 8 ай бұрын
I switched to NodeJS and then bun.sh!
@thomquiri9860
@thomquiri9860 8 ай бұрын
@@sanchogodinho lmaooo, yeah great... optimization I guess
@firstnamelastname2775
@firstnamelastname2775 8 ай бұрын
Thank you❤I started learning binary code😍😍😍🥰🥰😍
@thomquiri9860
@thomquiri9860 8 ай бұрын
@@firstnamelastname2775 nice, congratulations :)
@sami9323
@sami9323 8 ай бұрын
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
@PracticalAI_
@PracticalAI_ 7 ай бұрын
exactly this video is super wrong
@darkusboy1
@darkusboy1 7 ай бұрын
At least someone knows!
@LiamInviteMelonTeee
@LiamInviteMelonTeee 7 ай бұрын
This can make the code very complex when using nested function, see mcoding's video
@joergsonnenberger6836
@joergsonnenberger6836 7 ай бұрын
You have completely missed the point of the video. Hint: adding "global global_variable" at the top of the function doesn't change the bytecode at all. The whole (bad as it might be) point of the video is that accessing a global variable from the inner loop takes more time than loading the global variable into a local variable first.
@jaiganeshk4069
@jaiganeshk4069 5 ай бұрын
​@@joergsonnenberger6836 we can declare the variable as global inside the function before the for loop
@Martmists
@Martmists 6 ай бұрын
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.
@rnseby
@rnseby 8 ай бұрын
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.
@glass1098
@glass1098 8 ай бұрын
That's called dependency injection
@HirschyKiss
@HirschyKiss 8 ай бұрын
Yep, DI is the way to go. Reuse your code and it makes it easier to read.
@l3gacyb3ta21
@l3gacyb3ta21 6 ай бұрын
oh my god that is not dependency injection. that's just having a pure function
@glass1098
@glass1098 6 ай бұрын
@@l3gacyb3ta21 Realized after two weeks but who cares i got two likes thanks to misinformation
@pixelprizm
@pixelprizm 7 ай бұрын
If you need enough performance that something like this becomes worth it, you now are in need of a higher performance language.
@LovelyJacob
@LovelyJacob 7 ай бұрын
best reply imo
@l3gacyb3ta21
@l3gacyb3ta21 6 ай бұрын
I mean having faster python is still better than slow python.
@pixelprizm
@pixelprizm 6 ай бұрын
@@l3gacyb3ta21 I wouldn't say it's strictly better, it's a tradeoff - because to get faster python you have to decrease the readability by making your code appear a little more complex. You should choose Python when code readability/understandability is important and performance isn't important, because that is what the language is designed for. That's why it's a really good language for one-off quick scripts. But if you are in a problem space where performance is valuable, it's nice to use a language that's really designed for that from the get-go. Where you can actually use the language's nice features without surprise performance problems. That being said, sometimes you inherit a codebase that's already in Python and performance may become an issue so sometimes you have to choose lower code readability for better performance.
@pantazhs.94
@pantazhs.94 6 ай бұрын
Right
@matejnovosad9152
@matejnovosad9152 6 ай бұрын
If you care about improvement to 0.1 miliseconds from 0.11 miliseconds you should probably not be programming in python ...
@tahaahmedmallick2008
@tahaahmedmallick2008 2 ай бұрын
Definitely use this trick to run out of variable names
@serggie3
@serggie3 8 ай бұрын
That's what we call a premature optimization.
@revimfadli4666
@revimfadli4666 8 ай бұрын
Except its done after the mvp and before it's too big to optimise?
@creeperhostanimations1160
@creeperhostanimations1160 7 ай бұрын
⁠@@revimfadli4666yeah, it’s probably a fine optimization if your program is just a counter multiplying by a global, but it is woefully insignificant in the scale of any program larger. Or, if your program is just a big loop, use PyPy. I remember hearing that it’s better at this stuff.
@user-xx5pv6wv5w
@user-xx5pv6wv5w 7 ай бұрын
no, that's not
@NithinJune
@NithinJune 7 ай бұрын
it’s just an example of
@Rudxain
@Rudxain 6 ай бұрын
He literally profiled it. How's that premature??
@richardmelendez1626
@richardmelendez1626 8 ай бұрын
Bro I can't even here you over this beat. It's slaps more than I could have imagined 😂
@__mrmino__
@__mrmino__ 8 ай бұрын
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.
@viCoN24
@viCoN24 8 ай бұрын
It's predetermined but it doesn't mean it has the same cost. Lookup in local scope is faster for many reasons - it can be stored more efficiently, you can have more faith in what you are dealing with etc. For example, if you are dealing with something in outer scope, there are no guarantees about the value assigned to the variable as it may be changed in some other part of the code and global lookup has to be used to ensure that you are using proper value at the time you want to access it. If you copy the global value to a locally scope one, you can avoid that lookup as it is "detached" from its old variable. I don't know how lookups are managed for collections but if they are not explicitly copied, then I would assume they are stilled handled through global lookup rather than a local one based on the same problems with it being stored at a global level.
@__mrmino__
@__mrmino__ 8 ай бұрын
@@viCoN24 Your reasons are also wrong. The object under reference is stored the same way no matter if it's local or global. "Outer scope" and "global" are two different things, but whether you have "guarantees" about the referenced value or not is irrelevant - cPython has no JIT. It has global interpreter lock, so the dereference doesn't have to do nich Apart from finding the actual memory address. I would invite you to profile this in an actual code, where the local scope has just as many items as the global one.
@viCoN24
@viCoN24 8 ай бұрын
I think I missed your point. Thanks for the explanation! You focused on the underlying mechanism where it's still one operation rather than two lookups. I don't know how these operations are performed by CPython but one is clearly faster than the other so I tried to provide some reasoning behind it but it's true that GIL simplifies the situation. In principle the lookup resolution behaves as author explained even though the interpreter is able to scope it internally to decide on the proper operation rather performing dynamic lookup and failing at runtime when variable is not found. You are right that it's only one operation but whether it's assigned that one operation or the other is decided in the fashion described by the video maker.
@viCoN24
@viCoN24 8 ай бұрын
@@KennyWlr Wow! Thank you so much for this detailed explanation. It's good to be wrong sometimes to learn this much. Cheers! :)
@xsamueljr
@xsamueljr 8 ай бұрын
Are you sure? I've done a "benchmark" in Google Colab trying it, and the second case gains some performance indeed (Not too much)
@TheKilledDeath
@TheKilledDeath 7 ай бұрын
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.
@Suekru3
@Suekru3 7 ай бұрын
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
@bitzero000
@bitzero000 5 ай бұрын
look it's bad practice but people use globals and sometimes they provide value too
@olivierdulac
@olivierdulac 5 ай бұрын
Especially his changes also changed the whole program's behaviour as Global_var is no longer updated.
@user-wr2cd1wy3b
@user-wr2cd1wy3b 4 ай бұрын
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?
@JackySupit
@JackySupit 4 ай бұрын
so you never use a global variable / constant in your code?
@MortonMcCastle
@MortonMcCastle 2 ай бұрын
Is this why pointers are used in C and C++?
@whirvis
@whirvis 2 ай бұрын
This is one of the reasons, yes. C and C++ are, by default, pass by value. That means when you pass arguments to a function the values are copied for the function to use. Passing a pointer is still technically pass by value, but the value being copied is the *address* of the data; rather than the data itself. This allows you to save memory for larger variables (e.g., a big struct or a class). It also allows you to modify the original data, since you're being given a pointer to it rather than a copy of it. Note that C++ has references, which are often used over pointers because they are safer (but are very very similar). If you're in C++, default to using references unless you have a reason to use a pointer (e.g., so you can call C functions). For functions which are taking a pointer/reference to save time on copying data, you'll often see `const MyLargeDataType *mldt` or `const std:: string &str`. The `const` part signifies that the data being pointed to will never be modified by the function. Something to keep in mind also is that, for pointers, the following two are not the same: `const int *a`, `int *const b`. Here, `a` can be re-assigned to a new address of another `const int`. However, the contents at the address cannot be changed. For `b`, the contents at the address can be changed, but the address it actually points to cannot be changed. Here are some examples: ``` int c = 123, d = 456; const int *a = &c; int *const b = &c; a = &d; /* okay, we can change where it points */ *a = 789; /* not okay, we can't change the data it ponts to */ b = &d; /* not okay, we can't change where it points */ *b = 1011; /* okay, we change the data it points to */ ``` Note that in C++, all references are `const` (the address they point to cannot be changed), but you can still modify the data they point to so long as the type is not marked as `const`.
@MortonMcCastle
@MortonMcCastle 2 ай бұрын
@@whirvis Thanks for the explanation. I'm trying to learn C, and pointers are a bit confusing to me, particularly when and why to use them. As I understand it: int a; //declares a variable containing a value. int *b; //essentially declares two variables, one contains a value, the other, an address. *b = 123; //contains a value b = &a; //contains an address void Function(int *c){} //expects an address. Call like this: Function(&a);
@samcates435
@samcates435 Ай бұрын
@@MortonMcCastle int a; // designates enough memory to store an int but doesn’t say what int to store int *b; // designates enough memory to store an address which, when dereferenced, should point to memory that stores an int (should not be thought of as declaring two variables because it’s only declaring memory for the pointer, not for any actual int value) *b = 123; // is a very insidious bug because you’re modifying data at a random memory location because you never initialized b with a valid pointer so it’s interpreting whatever bits happened to be at that spot in memory before as a memory address. This is the kind of bug that hackers exploit to gain escalated privileges to systems. b = &a; // b is now a pointer to a, so now it is safe to do *b = 123 which is equivalent to a = 123
@viCoN24
@viCoN24 8 ай бұрын
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.
@Alternatywny_1
@Alternatywny_1 23 күн бұрын
Thank you for this short ^^
@lolcat69
@lolcat69 2 ай бұрын
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 )
@Jackson141vja
@Jackson141vja 8 ай бұрын
Can you make a video on how you did the performance test at the end?
@tormodhag6824
@tormodhag6824 8 ай бұрын
I dont know which he used, but he most likely used a profiler
@actiongammer668
@actiongammer668 6 ай бұрын
Maybe cprofile or mprof or else lineprofiler I guess.
@apalsnerg
@apalsnerg 8 ай бұрын
That is literally what I needed for a school project I'm working on. How unimaginably lucky. Thank you!
@ahmedkhedr2631
@ahmedkhedr2631 3 ай бұрын
Were yall doing AST analysis for a school project 0_0
@a.j.outlaster1222
@a.j.outlaster1222 2 ай бұрын
This is actually useful, I'll try to keep it in mind.
@sirbobbyuk
@sirbobbyuk Ай бұрын
Thanks for that, im learning Python coding, so it will help me in improving coding skills
@amritsubramanian8384
@amritsubramanian8384 8 ай бұрын
That was super insightful
@linuxguy1199
@linuxguy1199 7 ай бұрын
One alternative I really like, is just using C.
@click9000
@click9000 7 ай бұрын
Interesting. Didn't know it impacts performance that much.
@thedapperfoxtrot
@thedapperfoxtrot 5 ай бұрын
The synthwave theme extension though. My man 👌
@kobekapoor
@kobekapoor 2 ай бұрын
Can you do a video on how you test performance like that?
@macchiato_1881
@macchiato_1881 15 сағат бұрын
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.
@geoafrikana
@geoafrikana 8 ай бұрын
I do this a lot when building a data-cleaning function. It also avoids mistakingly corrupting the global variable.
@sarsoar
@sarsoar 8 ай бұрын
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.
@SilverSuperGamer
@SilverSuperGamer Ай бұрын
Fun fact: a new scope is not created in a for loop
@danix30001
@danix30001 6 ай бұрын
Another optimization trick is not to use global variables
@Rudxain
@Rudxain Ай бұрын
In other langs, this fixes race conditions, as the code no longer assumes the variable won't be mutated. The program no longer has to load from heap every iteration, since the value is already on the stack
@vanlepthien6768
@vanlepthien6768 2 ай бұрын
If you had an (inexplicable) reason to make the variable global, you wanted it updated within the function.
@mickaeldelattre7609
@mickaeldelattre7609 3 ай бұрын
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.
@vitoriio_santos4171
@vitoriio_santos4171 8 ай бұрын
here and from Brazil a question what is your theme I thought it was very good for viewing the code
@mistkeyblade
@mistkeyblade 5 ай бұрын
I've been playing around with global variables lately and your tip is really helpful!
@olivierdulac
@olivierdulac 5 ай бұрын
Using Global variables is not good, unless they are constants. If you need a value [especially a variable one] it should be passed as a parameter to the function, so that the function always have the same exact behavior when it is passed the exact same parameters. makes them predictable and individually testable.
@feelsxaadman9559
@feelsxaadman9559 3 ай бұрын
@@olivierdulac Use of global variables is fine in some contexts esp when writing smaller scripts or module level variables
@feelsxaadman9559
@feelsxaadman9559 3 ай бұрын
You're better off using the global keyword if worrying about scope
@molohktegg2462
@molohktegg2462 7 ай бұрын
Use parameters or the global Keyword. This solution is questionable at best, and also difficult to read since it’s not clear that you’re referencing a global variable.
@joergsonnenberger6836
@joergsonnenberger6836 7 ай бұрын
Using the global keyword doesn't change the generated bytecode at all. For good or for bad, it doesn't avoid the problem this video is supposedly about.
@molohktegg2462
@molohktegg2462 7 ай бұрын
@@joergsonnenberger6836 there’s no problem, the video pretends to do an optimization which, besides potentially not working, makes a bad practice worse and hard to understand. Adding the global variable makes the code more readable. And I doubt it doesn’t change the bytecode, since it literally allows you to edit global variables inside a function.
@joergsonnenberger6836
@joergsonnenberger6836 7 ай бұрын
@@molohktegg2462 You are completely missing the actual point of the video: local object references are faster than non-local object references. Modify the example slightly and it becomes a lot more practical: create an empty function f in a subpackage a.b.c.d. Now compare the time of a module-level "import a.b.c.d" and calling "a.b.c.d.f" in a loop in the test function with doing a "from a.b.c.d import f" in that test function. You will notice that the second version takes about half the time. It's also really annoying to see comment likes "I doubt it doesn't change the bytecode" since it is very easy to test that statement beforehand.
@molohktegg2462
@molohktegg2462 7 ай бұрын
@@joergsonnenberger6836 I’m not missing the point, I’m disagreeing with the recommendation. First, if you need write access to the global variable, the solution won allow that, you should use the global keyword instead; and if you only need read access, then there are much cleaner ways to do it, like passing a parameter, which would eliminate the need to look for global scope variables, and the performance cost, while also eliminating the risk of secondary effects. Also if you’re using Python, most likely performance is not your first priority, and readability/maintainability are just as important if not more; and this recommendation is exactly the reason why global variables are discouraged, poor readability and leads to unpredictable code.
@gurupartapkhalsa6565
@gurupartapkhalsa6565 9 күн бұрын
better optimization: redefine your function using integration as the increase is constant and determinant
@sirheavenlyy
@sirheavenlyy 2 ай бұрын
What theme do u use?
@3a8o
@3a8o 7 ай бұрын
How did you build that graphic of the performance?
@alefpontessilva1093
@alefpontessilva1093 3 ай бұрын
Could you give details about how to test the performance of a code?
@nagatosmith1158
@nagatosmith1158 Ай бұрын
Thanks!
@LOBOTOMYtv
@LOBOTOMYtv 2 ай бұрын
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
@Thekingslayer-ig5se
@Thekingslayer-ig5se 8 ай бұрын
What I understood is to make that variable in local scope so as to prevent checking for the golabal variable each and every time. This reduces the time complexity
@topzozzle6319
@topzozzle6319 8 ай бұрын
this optimization doesn't change time complexity, for that function it is O(n). time complexity refers to how well an algorithm scales when the amount of input data is increased. this optimization just reduces cpu cycles required to access the variable.
@Thekingslayer-ig5se
@Thekingslayer-ig5se 8 ай бұрын
@@topzozzle6319 Thanks for the info sir
@luis96xd
@luis96xd 5 ай бұрын
Thanks for the tip
@videos4mydad
@videos4mydad 28 күн бұрын
also lookup the phrase 'premature optimization' make code harder to read for dubious gains
@hamkaasburger6142
@hamkaasburger6142 2 ай бұрын
Never mention "performance" and "Python" in one item!
@hd1percent
@hd1percent 8 ай бұрын
How do you check the performance. Do you use matplotlib or no?
@bcn_clips
@bcn_clips 8 ай бұрын
Hey, how can I make my Terminal look like yours? Or can you someday do an general showcase of your visual stuff? Love your videos.
@hyprmynd
@hyprmynd 7 ай бұрын
This looks like VScode with maybe the Dracula theme?
@NuclearShadowYT
@NuclearShadowYT 18 күн бұрын
Your first mistake was using python if you're worried about performance
@nicolas.predella
@nicolas.predella 7 ай бұрын
the main i see here is the for loop, when at that scale of iterations i'd look at a different way to implement that section of code first, for instance doing that specific part in a more optimized language
@arunsagarsa3613
@arunsagarsa3613 8 ай бұрын
How did you test it. Like how are you getting this graph?
@PedroHenrique-qh3bl
@PedroHenrique-qh3bl 5 ай бұрын
def func(size, mult): return mult * ((size**2 - size) / 2) print(func(1000, 10)) then add some math :p
@aryanmithbawkar4309
@aryanmithbawkar4309 3 ай бұрын
What is your vs code theme?
@michaelxia99
@michaelxia99 8 ай бұрын
what theme are you using?
@1Eagler
@1Eagler 16 күн бұрын
I thought that after first iteration, python would be clever enough to figure it out fir the next iteration.
@mc-ciro
@mc-ciro 3 ай бұрын
What font do you use?
@Hallilo
@Hallilo 5 ай бұрын
Alternative: use C
@JohnFerrier
@JohnFerrier 7 ай бұрын
If you’re good at python, you should do the multiplication after the summation, since they’re separable.
@lOmaine777
@lOmaine777 8 ай бұрын
What if I want to make changes to the global variable? I'll have to use global or globals() right?
@b001
@b001 8 ай бұрын
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.
@Alex-fk8dk
@Alex-fk8dk 3 ай бұрын
This increase your program's speed while accupying more space for another variable.
@meatlover419
@meatlover419 8 ай бұрын
what theme are you using, it looks comfy for the eyes
@popcultureprogrammer2171
@popcultureprogrammer2171 Ай бұрын
The best optimization trick for Python is to write the performance-heavy bits in C++ and use Python as a frontend
@rafaelmesaglio1399
@rafaelmesaglio1399 11 күн бұрын
This is the exact type of thing a compiler would optimize for.... oh wait
@user-hn5iq1oh2t
@user-hn5iq1oh2t 7 ай бұрын
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 🙏
@aleemmett1734
@aleemmett1734 3 ай бұрын
How did you perfectly fit the IDE resolution to the phone screen?
@CaptMirage
@CaptMirage Ай бұрын
how did u do the graph? just used data to create it or some website or smth
@awuahjimisaac5166
@awuahjimisaac5166 5 ай бұрын
Can you create a playlist on python
@Sdwj91
@Sdwj91 8 ай бұрын
But in order to assign the new local variable with the value of the global variable, wouldn’t the function still look within itself for local first, then global?
@b001
@b001 8 ай бұрын
Yes, but this only happens once before the for-loop. It prevents it from searching globally in each of the 1000 for-loop iterations.
@thesleepingforest8929
@thesleepingforest8929 8 ай бұрын
This does not reassign the local variable to the global correct, like it doesn’t change the global from what it was originally to what it is after the local function is done? Sorry super new here to python and coding.
@nyther
@nyther 8 ай бұрын
@@thesleepingforest8929 It does, assigning the global_var to local_var creates kinda like a pointer so every time you reference the local_var it points to the global one and edits the global_var Unless I'm wrong.
@JustinUnruh-xz5rz
@JustinUnruh-xz5rz 8 ай бұрын
​@@nyther❤
@kamdenbrothers8089
@kamdenbrothers8089 8 ай бұрын
​@@nytherit depends on the data type. If it is a string, int, double it creates a copy. If it's a list, set, ect then changing the local will affect the global.
@user-xy6tv9dq9c
@user-xy6tv9dq9c 2 ай бұрын
amazing trick
@FundamSrijan
@FundamSrijan 6 ай бұрын
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
@Nip403
@Nip403 3 ай бұрын
Either regex or try-except int(input()) or .isdigit string method, then if/assert converted_to_int in range(1, 63)
@FundamSrijan
@FundamSrijan 3 ай бұрын
@@Nip403 will definitely try
@briannormant3622
@briannormant3622 3 ай бұрын
​@@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.
@fedyfausto
@fedyfausto 7 ай бұрын
This is the why you must use memory managed languages such as c/cpp or go
@wiono
@wiono Ай бұрын
the best optimization is: for i in range(1000): ans += 10 * i
@TannerJ07
@TannerJ07 3 ай бұрын
How did you get the optimization graph?
@reactoromg3906
@reactoromg3906 5 ай бұрын
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.
@viki_sky007
@viki_sky007 8 ай бұрын
How did u tested performance with that graph ?
@fishygd4723
@fishygd4723 5 ай бұрын
What is the software that you use?
@bobmike2373
@bobmike2373 3 ай бұрын
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
@chakhovbilly
@chakhovbilly 7 ай бұрын
What’s the font you’re using?
@maruftim
@maruftim 6 ай бұрын
cant wait for the livestream
@ankushbhagatofficial
@ankushbhagatofficial 8 ай бұрын
Which program you used to see performance difference?
@santiagoavalos3637
@santiagoavalos3637 8 ай бұрын
Came here to ask the same thing 🤔
@andgnd3674
@andgnd3674 8 ай бұрын
looks like seaborn
@smaaack
@smaaack 8 ай бұрын
Likely a python library like seaborn or matplotlib
@peter9477
@peter9477 7 ай бұрын
See module timeit
@crispy.caesus
@crispy.caesus 7 ай бұрын
well but actually you don't use the global variable in the first place
@aspoopypixel
@aspoopypixel Ай бұрын
My only issue is you didn't have your global variable in all caps to signalify it as a constant.
@rbaleksandar
@rbaleksandar 6 ай бұрын
Wait, what about the global declaration that you can add at the beginning of your function that tells Python it's a global variable?
@levimatheri7682
@levimatheri7682 2 ай бұрын
What font and theme are you using?
@user-ro3fr9qz2g
@user-ro3fr9qz2g 5 ай бұрын
How'd you customize your terminal to not show file path and so on?
@TESFan791
@TESFan791 Ай бұрын
It still has to find the global variable information tho.. so it still goes up to look for it right?
@prrithwirajbarman8389
@prrithwirajbarman8389 3 ай бұрын
hoping for more python optimization trick.
@Ryrzard
@Ryrzard 2 ай бұрын
Or, use a global keyword
@KonDima123
@KonDima123 7 ай бұрын
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)
@HansBezemer
@HansBezemer 3 ай бұрын
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.
@C_itsNemo
@C_itsNemo 5 ай бұрын
Where’d this guy go I like these videos
@efeme04
@efeme04 7 ай бұрын
What is the name of the vscode theme? Looks cool.
@ogamiitto5642
@ogamiitto5642 6 ай бұрын
Did you also test the effect of declaring global_var as as a “global variable” by placing “global global_var” at the beginning of the given function? Havenʼt done any tests, but I think this could also give a speedup (without introducing a second name for the same thing). Hmm :-\
@vaibhavjaiswal8034
@vaibhavjaiswal8034 6 ай бұрын
How do you test performance?
@dustinhess6798
@dustinhess6798 18 күн бұрын
What theme are you using?
@CattleRustlerOCN
@CattleRustlerOCN 5 ай бұрын
Could you define the local function var in the parentheses as an argument and just pass in the global value to the function? I code in other languages but just starting python. Just curious. Edit: according to the other comments here, you can pass it as an argument, and it seems to be the preferred way of doing it.
@kunalsoni7681
@kunalsoni7681 8 ай бұрын
helpful 💯😌
@nandurstudio
@nandurstudio 6 ай бұрын
hi, what font did u use? ty
@EmmettReaville
@EmmettReaville 2 ай бұрын
what website do you use?
@Ihat3my1if3
@Ihat3my1if3 2 ай бұрын
Video style reminds me of a certain ship that is on fire.
5 Good Python Habits
17:35
Indently
Рет қаралды 473 М.
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,1 МЛН
Comfortable 🤣 #comedy #funny
00:34
Micky Makeover
Рет қаралды 11 МЛН
Я не голоден
01:00
К-Media
Рет қаралды 8 МЛН
ТЫ С ДРУГОМ В ДЕТСТВЕ😂#shorts
01:00
BATEK_OFFICIAL
Рет қаралды 6 МЛН
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 115 М.
Pwnable.kr - Flag
8:07
COZT
Рет қаралды 565
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
D3 LiXiang L6 Машина Года 2025?
15:14
smotraTV
Рет қаралды 126 М.
Oh, wait, actually the best Wordle opener is not “crane”…
10:53
The moment we stopped understanding AI [AlexNet]
17:38
Welch Labs
Рет қаралды 863 М.
Dijkstra's Hidden Prime Finding Algorithm
15:48
b001
Рет қаралды 161 М.
*Args and **Kwargs in Python
3:49
b001
Рет қаралды 259 М.