The smallwig theory of optimization

本文探讨了三种优化方式:采用更合理的整体方法优化、使代码变得不那么诡异的优化以及使代码变得更加诡异的优化。前两种优化可在早期进行,而第三种优化则需要遵循六个条件:完善的单元测试、可靠的基准测试、改进效果验证、论证改进的重要性、代码注释及保留清晰但较慢的实现用于对比测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值