Skip to the content.

What to learn in order to optimize code

If you want to learn how to optimize code then there are a few general questions you have to learn the answer to. This should explain what you need to learn/use in order to answer those questions.

What’s slow?

The most important part of optimization is to know what to optimize. It’s a waste of effort to optimize a function that only takes 0.1% of the total execution time anyway. A profiler helps you answer this question by telling you how much the code spends in each function. Many profilers can do a lot more than just looking at the time spent. visual studio has a C# profiler that can also look at the time spent in a thread, spent waiting for a lock and how much you program allocates. The information a profiler gives you is the most important part of optimizing code.

Why is it slow?

There can be a lot of answers to this question but in general there are three categories(the last category isn’t here as i am not done with it. you can see a draft of it in the link).

How can i make it faster?

When you know where and why the code it slow you will usually also know how to make it faster. Algorithm has a shit time complexity? replace it with another algorithm. Don’t know of a better algorithm? Make your own or google your way to one. Data access is shit because the data structures are shit? replace them with better ones that makes data access more sequential or at the very least utilizes the cache better.

I don’t think there is a much better answer to this. How to optimize depend a lot on the code and how it’s used. If you want to be better at optimizing then you need to know more about 3 things. Algorithms, data structures and the internal working of the CPU.

Clean code

Clean/Readable code actually go a long way with optimized code. My own experience is that optimizing code usually makes the code cleaner and simpler(not always the case ofc). In many cases optimizations remove the cruft and unessesary code that didn’t need to be there anyway. Optimized code also tends to reduce the number of branches in code, or reorder them in such a way that they are easier to reason with. If you don’t know how to optimize something then try making the code simpler to begin with. The increased understand of the code after doing so can give good ideas about optimizations.

Tests

Always have tests beforehand that you can use to verify that the result didn’t change. Don’t waste your own(and others) time thinking that you optimized something only to later find out that it’s not working as expected.

Low level optimizations

Learning resources

Unfortuately i can’t remember many of the resources i myself used to learn about optimization. I know you can google most of the things mentioned here and get a lot of great answers. I can recommend looking for stackoverflow answers as they are usually really easy to understand and not too long winded.

I will link to some of the resources i have used to learn which includes some focusing on C++.