When / how to optimize common code?

When writing application code, it is generally recognized that premature micro- optimization is evil, and that profiling is important in the first place, and there is some debate about how, if any, higher-level optimization is in front. However, I have not seen any recommendations on when / how to optimize common code that will be part of a library or structure where you never know exactly how your code will be used in the future. What are some guidelines for this? Is premature micro-optimization still evil? How should performance be balanced with other design goals, such as ease of use, ease of demonstration, ease of implementation, and flexibility?

+4
source share
6 answers

"How to balance performance with other design goals?"

  • Get it to work.

  • Optimize it until it is optimized.

Pay attention to the order. Avoiding premature optimization means optimizing it after its operation.

Optimization is still very important. Premature optimization does not mean a lack of optimization. This means optimization after it works.

+4
source

I would say that optimization should take a back seat for other design goals, such as ease of use, ease of demonstration of correctness, ease of implementation and flexibility.

Try writing your code wisely using good practice and avoiding obvious pitfalls. However, do not optimize until you can do this with the profiler and real-world use cases.

You still come across some use cases that you never thought about, but you cannot optimize them if you don’t think about them.

A well-designed structure will usually be quite effective.

+4
source

Recently, I have heard an interesting and very interesting discussion about the famous quote from "Whip" in the podcast (I think these were deep fried bytes), which I will try to summarize:

Everyone knows the famous quote: premature optimization is the root of all evil ..
However, this is only half. Full quote:

We must forget about little efficiency, say, about 97% of the time: premature optimization is the root of all evil .

Look at it carefully - say, about 97% of the time.
The other side of this statement is about 3% of the time, "low" efficiency is critical.

My monitor displays about 50 lines of code. According to statistics, at least 1-2 lines of code on each screen will contain something that is sensitive to performance! Following the general wisdom of “do it now, optimize it later,” doesn’t seem like such a cunning plan when you think that on every screen you have a possible performance problem.

IMHO, you should always think about performance. You should not spend a lot of effort or sacrifice on this support until it is confirmed by profiling / testing, but you must have it in the depths of your consciousness.

I would personally apply this to the generic code, like this:
You definitely have some kind of code that, when you wrote it, thought that “it will be slow”, or “this is a stupid algorithm, but it doesn’t matter now, so I will fix it later”. Since you are in a shared library, and you cannot say that method A will ever be called with 5 elements, you have to go in and clear it all.

Once you figured out these things, I would not worry about what follows. Perhaps run the profiler over your unit tests to make sure nothing stupid has slipped, but otherwise wait for feedback from your library's consumers.

+2
source

My rule:

not optimize

Full rule:

if you don’t have a metric, do not optimize

This means that if you have not measured performance and created a specific metric , you should not do anything to make the code work better.

In the end: without metrics, how do you know what to optimize?

As soon as you have some kind of profiling, you may be surprised at where the bottlenecks in your system ... in my experience, it often happens that relatively minor changes can have a big impact.

+1
source

You are right, it is not always clear where the best hit for the dollar is for your time. It is best to be a user of your structure , as well as its designer.

Use your own infrastructure in a non-trivial application, try to implement the full range of functions. The more you use it, it will become clear which things you need most to be optimal.

Also get feedback and suggestions from other users as often as possible. You will inevitably find that other people want to do something with your infrastructure that you never thought of.

0
source

I think the best approach is to have a really good set of use cases for how your framework will be implemented. Only then will you have a good idea of ​​whether the performance matches its intended use.

Of course, you will never know how someone is going to use your infrastructure in the future (in the early years of my career, I never surprised me with creative ways users use my software - ways I would never have foreseen!), But thinking about Do you think that it will be used, you should get most of the way from you.

0
source

All Articles