There are three kinds of optimization.
1. Optimization by using a more sensible overall approach.
2. Optimization by making the code less weird.
3. Optimization by making the code more weird.
You've probably heard, and maybe even spouted yourself, the phrase "premature optimization is the root of all evil." It's exclusively "Type 3 optimization" that this aphorism applies to. Types 1 and 2 are quite fine to engage in pre-emptively.
To make a type 3 optimization, your burdens are six:
1. Thou shalt have excellent, comprehensive unit tests.
2. Thou shalt have a reliable benchmark, based on representative inputs.
3. Thou shalt demonstrate that your change improves the benchmark.
4. Thou shalt successfully argue that this improvement really matters.
5. Thou shalt comment the code.
6. In nontrivial cases, thou shalt also preserve the clear-but-slow implementation, to use in parity tests with your optimized implementation.
In all things, remember these truths:
1. Your brain is a terrible profiler.
2. Hotspot will outsmart you.
3. It just doesn't matter, until it matters.
If you believe this post, please spread the word!
1. Optimization by using a more sensible overall approach.
2. Optimization by making the code less weird.
3. Optimization by making the code more weird.
You've probably heard, and maybe even spouted yourself, the phrase "premature optimization is the root of all evil." It's exclusively "Type 3 optimization" that this aphorism applies to. Types 1 and 2 are quite fine to engage in pre-emptively.
To make a type 3 optimization, your burdens are six:
1. Thou shalt have excellent, comprehensive unit tests.
2. Thou shalt have a reliable benchmark, based on representative inputs.
3. Thou shalt demonstrate that your change improves the benchmark.
4. Thou shalt successfully argue that this improvement really matters.
5. Thou shalt comment the code.
6. In nontrivial cases, thou shalt also preserve the clear-but-slow implementation, to use in parity tests with your optimized implementation.
In all things, remember these truths:
1. Your brain is a terrible profiler.
2. Hotspot will outsmart you.
3. It just doesn't matter, until it matters.
If you believe this post, please spread the word!
本文探讨了三种优化方式:采用更合理的整体方法优化、使代码变得不那么诡异的优化以及使代码变得更加诡异的优化。前两种优化可在早期进行,而第三种优化则需要遵循六个条件:完善的单元测试、可靠的基准测试、改进效果验证、论证改进的重要性、代码注释及保留清晰但较慢的实现用于对比测试。

被折叠的 条评论
为什么被折叠?



