Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When/how to optimize generic code?

When writing application code, it's generally accepted that premature micro-optimization is evil, and that profiling first is essential, and there is some debate about how much, if any, higher level optimization to do up front. However, I haven't seen any guidelines for when/how to optimize generic code that will be part of a library or framework, 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 demonstrating correctness, ease of implementation, and flexibility?

like image 870
dsimcha Avatar asked Dec 23 '22 13:12

dsimcha


2 Answers

"How should performance be balanced with other design goals...?"

  1. Get it to work.

  2. Optimize it until it cannot be optimized further.

Note the order. Avoid premature optimization means optimize it after it works.

Optimization is still very, very important. Premature optimization does not mean NO optimization. It means optimize after it works.

like image 156
S.Lott Avatar answered Jan 04 '23 19:01

S.Lott


I would say that optimization must take a back seat to other design goals such as ease of use, ease of demonstrating correctness, ease of implementation, and flexibility.

Try to write your code intelligently using good practices and avoiding the obvious pitfalls. Still, don't optimize until you can do it with a profiler and real use cases.

You will still encounter some use cases you never thought of but you can't optimize for them if you never thought of them.

A well designed framework will usually be a reasonably performing one too.

like image 44
Lawrence Dol Avatar answered Jan 04 '23 18:01

Lawrence Dol