Тёмный

Python OPTIMIZATION Trick!!  

b001
Подписаться 270 тыс.
Просмотров 913 тыс.
50% 1

Опубликовано:

 

27 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 947   
@TheCircuitProject
@TheCircuitProject 11 месяцев назад
Another alternative is to create a default parameter for the function.
@Schecterbaby
@Schecterbaby 11 месяцев назад
I was just about to ask this. Thanks for pointing this out.
@shreehari2589
@shreehari2589 11 месяцев назад
Can you give an example
@beepbeepgamer1305
@beepbeepgamer1305 11 месяцев назад
​@@shreehari2589def func_name(local_var): when using function in main code func_name(global_var)
@fres621
@fres621 11 месяцев назад
​@@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 11 месяцев назад
​​@@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 11 месяцев назад
It’s common practice to also avoid using global variables as much as possible. Only use them as constants.
@novianandrian
@novianandrian 11 месяцев назад
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 11 месяцев назад
​@@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 10 месяцев назад
​@@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 10 месяцев назад
​@@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 10 месяцев назад
@@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 RU-vid comment.
@nikkehtine
@nikkehtine 11 месяцев назад
... or just pass it to the function as an argument, not only more efficient but also way easier to read
@seriouslyWeird
@seriouslyWeird 10 месяцев назад
This doesn't have to semantically make sense at all
@vulpritprooze
@vulpritprooze 9 месяцев назад
kinda annoying to keep track of the variables and has to unnecessarily place arguments everytime u call the function
@Rudxain
@Rudxain 8 месяцев назад
This is common in Pure Functional Programming
@4ngelf
@4ngelf 2 месяца назад
​@@vulpritproozeSo, you can set default parameters
@ajflink
@ajflink 26 дней назад
I always get annoyed when looking at others' Python code and see something like: "object1 = object2". I've even seen inside a function something like: c_time = time.time() current_time = c_time Please explain to me why anyone would do something that seems so redundant?
@mickaeldelattre7609
@mickaeldelattre7609 5 месяцев назад
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.
@CamsCozyCorner
@CamsCozyCorner 13 дней назад
why is dict lookup slow? is it not using a hash map?
@ben-brady
@ben-brady 10 дней назад
It's just that compared to a direct array lookup, computing a hash lookup is slow
@tobias8951
@tobias8951 5 дней назад
You already lost me at "at compile time". What compile time? Python is interpreted.
@tremagne
@tremagne 5 дней назад
@@tobias8951 Python is first compiled into bytecode (thats the .pyc that you might see next to the .py file) and the bytecode is then interpreted by the python interpreter. You can have a look at the bytecode with the dis module. Do "from dis import dis", then pass your sample code as a string to the function dis and print the result. The actual disassembly will vary from version to version, but you should have a "
@noadsplease2737
@noadsplease2737 4 дня назад
@@tobias8951it’s also compiled much the way that Java is compiled for the JVM python is compiled for the python runtime/interpreter
@thomquiri9860
@thomquiri9860 11 месяцев назад
best python optimization trick: switch to another language
@lobotomy-victim
@lobotomy-victim 11 месяцев назад
thanks to this trick I managed to get a 50000% performance increase!
@sanchogodinho
@sanchogodinho 11 месяцев назад
I switched to NodeJS and then bun.sh!
@thomquiri9860
@thomquiri9860 11 месяцев назад
@@sanchogodinho lmaooo, yeah great... optimization I guess
@firstnamelastname2775
@firstnamelastname2775 11 месяцев назад
Thank you❤I started learning binary code😍😍😍🥰🥰😍
@thomquiri9860
@thomquiri9860 11 месяцев назад
@@firstnamelastname2775 nice, congratulations :)
@Martmists
@Martmists 9 месяцев назад
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.
@benshapiro9731
@benshapiro9731 2 месяца назад
Yes this is correct. Also corroborated by the fact that the variable scope (aka what namespace python will search for a given variable) is determined at compile time before the interpreter will execute the byte code. Mcoding has a great video about this
@youtubewts
@youtubewts 21 день назад
So what's going on with his performance results?
@Sentherus
@Sentherus 17 дней назад
@@youtubewts He should share his test methodology
@FictionHubZA
@FictionHubZA 3 месяца назад
"All that for a drop of blood?" - Thanos.
@sami9323
@sami9323 10 месяцев назад
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_ 10 месяцев назад
exactly this video is super wrong
@darkusboy1
@darkusboy1 10 месяцев назад
At least someone knows!
@LiamInviteMelonTeee
@LiamInviteMelonTeee 10 месяцев назад
This can make the code very complex when using nested function, see mcoding's video
@joergsonnenberger6836
@joergsonnenberger6836 10 месяцев назад
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 8 месяцев назад
​@@joergsonnenberger6836 we can declare the variable as global inside the function before the for loop
@rnseby
@rnseby 11 месяцев назад
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 11 месяцев назад
That's called dependency injection
@HirschyKiss
@HirschyKiss 11 месяцев назад
Yep, DI is the way to go. Reuse your code and it makes it easier to read.
@l3gacyb3ta21
@l3gacyb3ta21 9 месяцев назад
oh my god that is not dependency injection. that's just having a pure function
@glass1098
@glass1098 9 месяцев назад
@@l3gacyb3ta21 Realized after two weeks but who cares i got two likes thanks to misinformation
@pixelprizm
@pixelprizm 9 месяцев назад
If you need enough performance that something like this becomes worth it, you now are in need of a higher performance language.
@LovelyJacob
@LovelyJacob 9 месяцев назад
best reply imo
@l3gacyb3ta21
@l3gacyb3ta21 9 месяцев назад
I mean having faster python is still better than slow python.
@pixelprizm
@pixelprizm 9 месяцев назад
@@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 9 месяцев назад
Right
@matejnovosad9152
@matejnovosad9152 9 месяцев назад
If you care about improvement to 0.1 miliseconds from 0.11 miliseconds you should probably not be programming in python ...
@richardmelendez1626
@richardmelendez1626 10 месяцев назад
Bro I can't even here you over this beat. It's slaps more than I could have imagined 😂
@PedroHenrique-qh3bl
@PedroHenrique-qh3bl 8 месяцев назад
def func(size, mult): return mult * ((size**2 - size) / 2) print(func(1000, 10)) then add some math :p
@__mrmino__
@__mrmino__ 11 месяцев назад
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 11 месяцев назад
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__ 11 месяцев назад
@@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 11 месяцев назад
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 11 месяцев назад
@@KennyWlr Wow! Thank you so much for this detailed explanation. It's good to be wrong sometimes to learn this much. Cheers! :)
@xsamueljr
@xsamueljr 11 месяцев назад
Are you sure? I've done a "benchmark" in Google Colab trying it, and the second case gains some performance indeed (Not too much)
@macchiato_1881
@macchiato_1881 2 месяца назад
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.
@viCoN24
@viCoN24 11 месяцев назад
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.
@BlueBearOne
@BlueBearOne Месяц назад
Nice! Thanks for sharing that optimization trick :)
@KonDima123
@KonDima123 10 месяцев назад
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 6 месяцев назад
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.
@popcultureprogrammer2171
@popcultureprogrammer2171 4 месяца назад
The best optimization trick for Python is to write the performance-heavy bits in C++ and use Python as a frontend
@Jackson141vja
@Jackson141vja 11 месяцев назад
Can you make a video on how you did the performance test at the end?
@tormodhag6824
@tormodhag6824 10 месяцев назад
I dont know which he used, but he most likely used a profiler
@actiongammer668
@actiongammer668 8 месяцев назад
Maybe cprofile or mprof or else lineprofiler I guess.
@Rudxain
@Rudxain 4 месяца назад
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
@tahaahmedmallick2008
@tahaahmedmallick2008 5 месяцев назад
Definitely use this trick to run out of variable names
@youtubewts
@youtubewts 21 день назад
More specifics on the tests run needed. Some are saying the performance difference is negligible but your results of some test suggest 10%
@danix30001
@danix30001 9 месяцев назад
Another optimization trick is not to use global variables
@YousafSulaiman
@YousafSulaiman 2 месяца назад
Amazing than I will try it tomorrow 👍
@MortonMcCastle
@MortonMcCastle 5 месяцев назад
Is this why pointers are used in C and C++?
@whirvis
@whirvis 5 месяцев назад
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 5 месяцев назад
@@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 4 месяца назад
@@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
@subhankarsamanta7783
@subhankarsamanta7783 8 дней назад
Informative! Thanks!
@AndrasBalintBoroczky
@AndrasBalintBoroczky 2 месяца назад
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**)
@vo7414
@vo7414 2 месяца назад
That makes two of us who recognized this was a summation problem so far.
@NuclearShadowYT
@NuclearShadowYT 3 месяца назад
Your first mistake was using python if you're worried about performance
@serggie3
@serggie3 11 месяцев назад
That's what we call a premature optimization.
@revimfadli4666
@revimfadli4666 10 месяцев назад
Except its done after the mvp and before it's too big to optimise?
@creeperhostanimations1160
@creeperhostanimations1160 10 месяцев назад
⁠@@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.
@tyggvjhhfcv
@tyggvjhhfcv 9 месяцев назад
no, that's not
@NithinJune
@NithinJune 9 месяцев назад
it’s just an example of
@Rudxain
@Rudxain 8 месяцев назад
He literally profiled it. How's that premature??
@sirbobbyuk
@sirbobbyuk 4 месяца назад
Thanks for that, im learning Python coding, so it will help me in improving coding skills
@ankushbhagatofficial
@ankushbhagatofficial 11 месяцев назад
Which program you used to see performance difference?
@santiagoavalos3637
@santiagoavalos3637 11 месяцев назад
Came here to ask the same thing 🤔
@andgnd3674
@andgnd3674 11 месяцев назад
looks like seaborn
@smaaack
@smaaack 11 месяцев назад
Likely a python library like seaborn or matplotlib
@peter9477
@peter9477 10 месяцев назад
See module timeit
@mistkeyblade
@mistkeyblade 8 месяцев назад
I've been playing around with global variables lately and your tip is really helpful!
@olivierdulac
@olivierdulac 8 месяцев назад
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 6 месяцев назад
@@olivierdulac Use of global variables is fine in some contexts esp when writing smaller scripts or module level variables
@feelsxaadman9559
@feelsxaadman9559 6 месяцев назад
You're better off using the global keyword if worrying about scope
@GGysar
@GGysar 9 месяцев назад
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.
@ibrahimkalmati9379
@ibrahimkalmati9379 19 дней назад
Functions can take parameters Just create function with parameter Then pass variable as parameter when you call function It virtually as your solution but it is standard do that this way.
@Ryrzard
@Ryrzard 5 месяцев назад
Or, use a global keyword
@lolcat69
@lolcat69 5 месяцев назад
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 )
@NguyenHaiBinh-yo6el
@NguyenHaiBinh-yo6el 10 месяцев назад
so you are telling me that python can't even memoize declared variables position? at this point, just leave it
@illiasukonnik9966
@illiasukonnik9966 9 месяцев назад
Thanks. Yes and no about this method, Python is about writing understandable code, only big picture optimizations are needed (general algorithm). If you value even 10% loop speed increase, beter switch to C, Rust or Cython that prt of code or
@Sdwj91
@Sdwj91 11 месяцев назад
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 11 месяцев назад
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 11 месяцев назад
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 11 месяцев назад
@@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 11 месяцев назад
​@@nyther❤
@kamdenbrothers8089
@kamdenbrothers8089 11 месяцев назад
​@@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.
@apalsnerg
@apalsnerg 11 месяцев назад
That is literally what I needed for a school project I'm working on. How unimaginably lucky. Thank you!
@ahmedkhedr2631
@ahmedkhedr2631 6 месяцев назад
Were yall doing AST analysis for a school project 0_0
@imie-nazwisko
@imie-nazwisko 9 месяцев назад
I like how Python is so slow that doing something as simply as referencing variable in another scope can reduce its performance down to 10%
@tajkris
@tajkris 9 месяцев назад
in pretty much every language you will have a performance penalty by using global instead of local var global vars are in different part of memory than your local vars, which are on a stack. also usage of local vars can be optimized (to a constant in this case) - if you're not passing a reference to it, nothing is going to modify it. global variables on the other hand, compiler would need to know that your global var is not used by any of the functions down the call stack. that's quite simple in this case, but not in general and therefore it compiles to a read from mem on every access
@spookycode
@spookycode 8 месяцев назад
⁠​⁠@@tajkrisnot exactly… in C for example the only cost related to global vars is dynamic allocation, ie creation of global variables at runtime, ie calling malloc. Any globals defined statically have already been loaded into memory at program start and are already allocated. Memory access always has a speed of O(1) so every memory access has the same speed. so no… global variables aren‘t intrinsically slower at least in c and c like languages.
@ieatthighs
@ieatthighs 8 месяцев назад
Why do you like it?
@drewsarkisian9375
@drewsarkisian9375 6 месяцев назад
Waah. Then don't use it, Chief. Also, didn't you mean "by", not "to"?
@HansBezemer
@HansBezemer 6 месяцев назад
@@spookycode As a matter of fact, creating and destroying a stack frame takes the majority of the performance overhead of calling a function. Those creating VMs usually implement it as a switch() instead of a table of function pointers - just to avoid function call overhead.
@gurupartapkhalsa6565
@gurupartapkhalsa6565 3 месяца назад
better optimization: redefine your function using integration as the increase is constant and determinant
@petrlaskevic1948
@petrlaskevic1948 10 месяцев назад
Or you could declare the variable is global explicitly inside a function, using the global keyword
@peter9477
@peter9477 10 месяцев назад
Which would have zero impact on performance here.
@Alternatywny_1
@Alternatywny_1 3 месяца назад
Thank you for this short ^^
@linuxguy1199
@linuxguy1199 10 месяцев назад
One alternative I really like, is just using C.
@thedapperfoxtrot
@thedapperfoxtrot 8 месяцев назад
The synthwave theme extension though. My man 👌
@overbored1337
@overbored1337 11 месяцев назад
Optimizing python is like polishing a turd
@ThisShitWontWor
@ThisShitWontWor 8 месяцев назад
Yeah but I guess no fucking one use it for performance anyway 😂
@drewsarkisian9375
@drewsarkisian9375 6 месяцев назад
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".
@overbored1337
@overbored1337 6 месяцев назад
@@drewsarkisian9375 Its the Duplo of programming
@HansBezemer
@HansBezemer 6 месяцев назад
@@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.
@somedooby
@somedooby 2 месяца назад
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.
@aneesh1701
@aneesh1701 11 месяцев назад
or you can just say global var_name above the for loop
@ZeroSpawn
@ZeroSpawn 11 месяцев назад
Well if that Global variable is needed somewhere else one would have to dive into the for Loop to get the value.
@aneesh1701
@aneesh1701 11 месяцев назад
@@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
@nigh_anxiety
@nigh_anxiety 11 месяцев назад
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.
@AndrewRedW
@AndrewRedW 10 месяцев назад
Another python optimization trick: don’t use loops
@a.j.outlaster1222
@a.j.outlaster1222 5 месяцев назад
This is actually useful, I'll try to keep it in mind.
@_ad.w
@_ad.w 11 месяцев назад
bro is not fireship w content tho
@eldebtor6973
@eldebtor6973 8 месяцев назад
well actually, you should pass it as an argument
@vanlepthien6768
@vanlepthien6768 5 месяцев назад
If you had an (inexplicable) reason to make the variable global, you wanted it updated within the function.
@ScorpioHR
@ScorpioHR Месяц назад
If you really need performance, do it in C. If you need it to fly, use assembly.
@feelsxaadman9559
@feelsxaadman9559 6 месяцев назад
A) Calling it in the first place isn't unoptimal, the extra instruction to look outside the scope is completely irrelevant. B) you can use the global parameter. C) you should be passing it in instead if you're really worried about optimization. Because you want to avoid an instruction to look outside the local scope you force it to declare another variable in the existing stack, how is that more optimal?
@skylo706
@skylo706 6 месяцев назад
I didn't code in python for quite some time but my guess is, that it only allocated memory once and then keeps a close reference to that value, similar to how the register keyword in C works
@TyronneRatcliff
@TyronneRatcliff 13 дней назад
That's cool how you can make a reference to a global varaiable by using another variable inside a function definition.
@wiono
@wiono 4 месяца назад
the best optimization is: for i in range(1000): ans += 10 * i
@sebastiansrikanth
@sebastiansrikanth 20 дней назад
Thank you, this helps 😊 How did you find the performance difference graph? Could you explain it??
@aryanmithbawkar4309
@aryanmithbawkar4309 6 месяцев назад
What is your vs code theme?
@Alex-fk8dk
@Alex-fk8dk 5 месяцев назад
This increase your program's speed while accupying more space for another variable.
@amritsubramanian8384
@amritsubramanian8384 11 месяцев назад
That was super insightful
@rahul_chilukamari
@rahul_chilukamari Месяц назад
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.
@nagatosmith1158
@nagatosmith1158 3 месяца назад
Thanks!
@click9000
@click9000 10 месяцев назад
Interesting. Didn't know it impacts performance that much.
@_nsikak
@_nsikak Месяц назад
Great tip! I love your font. What is it?
@kobekapoor
@kobekapoor 4 месяца назад
Can you do a video on how you test performance like that?
@videos4mydad
@videos4mydad 3 месяца назад
also lookup the phrase 'premature optimization' make code harder to read for dubious gains
@luis96xd
@luis96xd 8 месяцев назад
Thanks for the tip
@bradenhelmer9795
@bradenhelmer9795 10 месяцев назад
Or just don’t use globals, only if they’re constant. Python doesn’t specifically implement constants but you can denote them with capitalization.
@anibaldk
@anibaldk День назад
Pass it as a func parameter and done. Simple, expected, cleaner 🤷‍♂️
@JohnFerrier
@JohnFerrier 10 месяцев назад
If you’re good at python, you should do the multiplication after the summation, since they’re separable.
@alefpontessilva1093
@alefpontessilva1093 6 месяцев назад
Could you give details about how to test the performance of a code?
@maurosampietro9900
@maurosampietro9900 8 месяцев назад
Another optimization technique is to write code in assembler
@ZeroChronicles01
@ZeroChronicles01 9 месяцев назад
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]
@prrithwirajbarman8389
@prrithwirajbarman8389 5 месяцев назад
hoping for more python optimization trick.
@Thekingslayer-ig5se
@Thekingslayer-ig5se 11 месяцев назад
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 11 месяцев назад
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 11 месяцев назад
@@topzozzle6319 Thanks for the info sir
@arielcg_
@arielcg_ 10 месяцев назад
1. Use "global
@joergsonnenberger6836
@joergsonnenberger6836 10 месяцев назад
Using global makes absolutely no difference here. As "dis.dis(func)" would have told you, if you had actually tried it.
@mcp-hackers
@mcp-hackers 5 месяцев назад
What font do you use?
@maruftim
@maruftim 9 месяцев назад
cant wait for the livestream
@YonatanNgusu-w4d
@YonatanNgusu-w4d 4 месяца назад
amazing trick
@nicolas.predella
@nicolas.predella 10 месяцев назад
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
@BrianOSheaPlus
@BrianOSheaPlus 10 месяцев назад
That's not the only reason to use a local variable inside the function. An arguably more important reason is that it's bad practice for functions to modify global scope. This is because other code might also access the global variable. The global variable's value is unpredictable because we don't know how often the function is called, or under what circumstances it will get called. This makes it hard to reason about the behavior of the program.
@joergsonnenberger6836
@joergsonnenberger6836 10 месяцев назад
The code doesn't modify the global scope.
@BrianOSheaPlus
@BrianOSheaPlus 10 месяцев назад
@@joergsonnenberger6836 ah, you're right, I misread that. However, there is still some risk that some other code will modify the global variable, and that will change the behavior of the function.
@awuahjimisaac5166
@awuahjimisaac5166 8 месяцев назад
Can you create a playlist on python
@sourcejet8561
@sourcejet8561 2 месяца назад
I got enough performance with cpp thanks
@hexailon
@hexailon 5 месяцев назад
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.
@LOBOTOMYtv
@LOBOTOMYtv 5 месяцев назад
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
@bcn_clips
@bcn_clips 10 месяцев назад
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 9 месяцев назад
This looks like VScode with maybe the Dracula theme?
@fishygd4723
@fishygd4723 8 месяцев назад
What is the software that you use?
@baileysmooth
@baileysmooth 4 месяца назад
you should pass the variable into the function.
@Hallilo
@Hallilo 8 месяцев назад
Alternative: use C
@noadsplease2737
@noadsplease2737 4 дня назад
This is the sort of detail that creates training warts that end up in other code often in other languages and people don’t remember why they did it 5 tests down the line when the implementation changes. I avoid these sorts of “optimizations” unless o have no other choice. Even if said choice is writing in another language.
@fedyfausto
@fedyfausto 10 месяцев назад
This is the why you must use memory managed languages such as c/cpp or go
@rafaelmesaglio1399
@rafaelmesaglio1399 3 месяца назад
This is the exact type of thing a compiler would optimize for.... oh wait
@C_itsNemo
@C_itsNemo 7 месяцев назад
Where’d this guy go I like these videos
@justinesalazar2142
@justinesalazar2142 8 месяцев назад
what's the name of the application? newbie here
@omrishaffer8461
@omrishaffer8461 4 месяца назад
Cool but is there EVER a justification for a global variable?
@johnvine5731
@johnvine5731 7 месяцев назад
When nanoseconds make a difference.
@arunsagarsa3613
@arunsagarsa3613 11 месяцев назад
How did you test it. Like how are you getting this graph?
@seriouslyWeird
@seriouslyWeird 10 месяцев назад
A person would think that python would ready pointers for all the available variables inside the scope like a decent compiled programming language
@joergsonnenberger6836
@joergsonnenberger6836 10 месяцев назад
Replace the loop with something non-trivial and it can't guarantee that the module scope object binding doesn't change. Similar to how a C compiler could have to load the same pointer again and again as if it can't proof that it wasn't changed.
Далее
What does '__init__.py' do in Python?
6:50
Просмотров 61 тыс.
I Solved The World's Hardest Maze (with Code)
9:54
Просмотров 182 тыс.
I built my own 16-Bit CPU in Excel
15:45
Просмотров 1,5 млн
Python vs Rust vs C++ Speed Comparison
1:50
Просмотров 46 тыс.
5 Good Python Habits
17:35
Просмотров 581 тыс.
How Math Becomes Difficult
39:19
Просмотров 151 тыс.
Being Competent With Coding Is More Fun
11:13
Просмотров 102 тыс.
Dear Functional Bros
16:50
Просмотров 541 тыс.