Premature Optimization

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I truly do believe that premature optimization is the root of all evil [Music] [Music] most conversations about performance are a total waste of time not because performance isn't important but because people feel very very strongly about performance I tend to think of performance as an element of this trade-off triangle velocity here is how quickly one adds new features and adaptability is how well the system can change to new requirements you might think that velocity and adaptation go hand in hand and they do but sometimes one can hurt the other focusing on pure velocity means hacking together something as fast as possible taking the shortest path to the Feature Future maintainers be damned and at the beginning you'll gain a lot of immediate velocity as you hack things together but as you do this you're creating a bunch of technical debt which will weigh you down to a halt adaptability is about writing the code in a way to enable changes as new requirements come in think creating reusable extensible components beautifully crafted interfaces and configurability with the right designs you can increase velocity by reducing the size of changes if needed to add new features but if you make things too adaptable you also hurt velocity if you hit a crystal ball and could see the future you'd be able to know exactly which use cases you'd need to design for and also which ones you don't need to design for but when you build a highly adaptable system that can adapt to a whole bunch of cases that never happen then all of that just ends up being a big waste of time with highly extensible systems you also heard performance adding lots of adaptability naturally adds more abstraction and indirection in your code which usually has a negative impact on performance but this trade-off is usually worth it because of the adaptability it allows you might think you always want something in the middle but I actually believe this depends on the stage of your project a feature complete Game pushing final ship date would focus on performance you might be okay with reducing the velocity of new changes and adaptability in order to squeeze out the last little bit of performance but when a game is earlier in development you might focus on getting more features out quickly or building up an extensible system that lets you tweak the game freely [Music] when Mark Zuckerberg wrote Facebook he did it in PHP PHP is an awkward language to say the least and it gave Facebook a whole slew of scaling issues as Facebook grew they ended up making a PHP to C plus compiler and later when that hit a performance Plateau they basically created a dialect of PHP called hack but would Zuckerberg be better off if he wrote it in highly optimized C code to start no I don't think so I think writing in the inconsistent inefficient PHP was the right move because it meant that he could build Facebook quickly once performance became a real problem Engineers went to solve it if he focused on choosing performance over velocity it's not clear that Facebook would have taken off and Zuckerberg wouldn't have had the privilege to visit Congress as much as he has so the key with the triangle is that performance is a problem but it's not usually the first problem and you should be deliberate about which way you're leaning it's useful to differentiate performance issues into two camps macro performance which I think of as design level performance these are system-wide performance considerations and micro performance which is fine-tuned performance this could be looking at the perf of a single module or function premature optimization usually occurs for micro performance this is typically where someone comments in a code review that you should do X instead of Y because X is faster than y but computers are really fast I have some python code that helps me generate the code animations for this series and that python code does some horrific stuff each character my code is basically in a big array and on every frame I just linear search through it to find the relevant characters and it still renders me out of video fast enough that it's not worth making it any faster at the end of the day your writing code to solve a real world problem and typically getting to the solution of that real world problem faster is better than solving the problem slower with faster code let's look at a few examples of premature optimization in C plus plus there's two operators that both let you increment a variable by one proponents of plus plus I will say it's faster because technically I plus plus needs to make a copy of the value since when it's part of a larger expression the incrementation needs to happen after the expression is evaluated oh man this one gets me you might say but code aesthetic if it's faster well then why don't you just do it why don't you just always do plus plus I because seruma Dam think of the Precedence I don't know for a fact that it is indeed faster and so should I blindly trust Daryl because he thinks it's faster no of course not so now I need to go through and do a thorough and lengthy investigation into the distinction between I plus plus and plus plus I now I gotta go and disassemble it I write a for loop with the pre-increment operator and look at what it does okay here it loads the value from I into a register EX then adds 1 to it and then puts the result back into the memory for I okay seems straightforward so let's look at post increment and it's identical you get the same code for both okay but this is just an INT what if it's an iterator we have a vector which is just C plus plus versions of a list and you can access the elements of the vector through an object that overrides the two plus plus operators the post one would need to make a copy right well let's look aha gotcha look at the difference between these two long functions you'll notice that this one calls the function for the post and this one calls the function for the pre and the post version of this has four extra assembly lines to make a copy of the iterator and it calls into the other operator so you got me it does cost a little bit more to do I plus plus except when you turn on optimization those function calls completely disappear and you end up with identical code for both cases so now the answer is a thorough and totally satisfying Maybe we must measure after measuring the difference on my MacBook the speed of the slowest increment is 3.4 nanoseconds if you notice the little counter below this is how many worst case increments have occurred since I started talking about this is my increment really going to execute this many times is my code going to even ship foreign startup fail spent three hours on this investigation could have spent three hours building the best dog walking out known to humanity does anything really matter so my point with this is that you should ask yourself is this conversation even worth it if it brings you Joy to figure out which is technically faster go for it but I don't think this should end up on a code review because it doesn't matter otherwise I have to go through this whole thing again I like I plus plus because I think it looks prettier and until someone tells me that we can't ship because our app is too slow and we've measured that the solution is to change all of our I plus pluses to plus plus size I'm sticking with it this also applies to functions in many of my previous videos I've argued about using extraction to help readability some have argued that functions are expensive but rarely has the cost of a function been so significant that removing the function is the solution to a performance problem and in the rare instance that it was a solution it doesn't mean that it's proof that it's worth the readability loss by default a big issue with making performance rules to follow is that often the rules have a lot of exceptions here we have a class that keeps track of the currently logged in users our first implementation just has an array which contains the list of users then we have this logged in method which returns whether or not the user is logged in it has a simple for Loop which looks through the list searching for the current user during a code review someone says hey this thing is slow you shouldn't just search the list of users like that it's slow and you should use a set instead a data structure that lets you look up unique elements much faster but when you measure the difference you actually find that set is slower when you only have a few users logged in which is normal for your system I'd speculate this is because our integers in the array are right beside each other in memory and the implementation of set likely allocates objects in lots of different places in memory reducing cache hits but like I said before until you've shown that this function specifically is the leading cause of your performance issues go with what's more readable so I do think that set creates cleaner more readable and less bug prone code so I'd go with that there's so many factors to Performance that there's only one way to properly optimize measure try something measure again measuring is critical because like we just showed your assumptions of what will make things faster can make things slower you can help form a hypothesis of how to make things better by doing an analysis data structure selection by far is the most important and that's because choosing the right data structure can give dramatically better results over the wrong data structure when dealing with performance issues I tend to think in terms of 80 moves first what are changes that could have an 80 reduction and often the only thing that can get you that far are data structure changes once you've implemented them and measured the difference and still see it's not good enough then you have to look at the smaller things you might not know where to start and that's where you'd want to look at the profiler a profiler can tell you what are the hot spots of your code it can point out functions that are the most expensive it was kind of a funny story about Grand Theft Auto 5 online it was so slow to launch that one player who goes by tossed wanted to figure out why even though they didn't have the source code tossed user profiler to figure out why it was so slow it turned out pretty much all of the time was spent parsing and processing a Json file a patch to fix just that one part improved the load Time by 70 that's the funny thing with performance is that sometimes when things are slow it's just one silly thing slowing everything down for GTA 5 all someone had to do is look after data structures and profiling then you just have to start making educated guesses thinking about how your code could be working underneath the hood and find ways to simplify stuff a lot of performance comes from how your code uses memory allocating memory which is done whenever you make a new object or array can slow things down in critical sections because the system has to find free chunks to put stuff you'll get a lot of speed for having critical elements close by in memory because things are much much faster when they're in the CPUs cache but again I wouldn't worry about these things until you know you have a performance problem and have tried other things first so when you optimize first have a real performance problem then measure it try to make 80 moves by swapping data structures or moving to a well-known faster algorithm profile and find hot spots then worst case start thinking about what your code is doing under the hood what are some interesting cases you've hit while trying to make code faster when a video of mine is getting long I sometimes cut sections and post them as deleted scenes on my patreon for this video we talked about micro optimization but there's a section on my patreon about macro optimization strategies if you're curious [Music]
Info
Channel: CodeAesthetic
Views: 769,356
Rating: undefined out of 5
Keywords: Programming, Optimization
Id: tKbV6BpH-C8
Channel Id: undefined
Length: 12min 39sec (759 seconds)
Published: Fri Feb 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.