``` 罗布·派克的编程五条规则 ```
Rob Pike’s Rules of Programming (1989)

原始链接: https://www.cs.unc.edu/~stotts/COMP590-059-f24/robsrules.html

## 代码优化:关键原则总结 有效的代码优化并非猜测,而是一个数据驱动的过程。**不要过早优化**——你无法可靠地预测性能瓶颈在哪里(规则1 & 2)。在尝试任何加速改进之前,**始终进行测量**,并且只关注那些明显减慢速度的区域。 除非绝对必要,否则避免使用复杂的算法(规则3 & 4)。它们通常对小型数据集存在隐藏的性能成本,并会增加更多引入错误的可能。**优先考虑简洁性**——“蛮力”通常是一个好的起点(KISS原则)。 最终,**数据结构比算法更重要**(规则5)。选择正确的数据组织方式通常会使最佳算法显而易见。首先关注设计良好的数据结构,因为它们是高效编程的核心。 这些原则呼应了霍尔、汤普森和布鲁克斯等人的智慧,强调了一种务实的性能方法:测量、简化和优先组织数据。

相关文章

原文
  1. Rule 1. You can't tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you've proven that's where the bottleneck is.

  2. Rule 2. Measure. Don't tune for speed until you've measured, and even then don't unless one part of the code overwhelms the rest.

  3. Rule 3. Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don't get fancy. (Even if n does get big, use Rule 2 first.)

  4. Rule 4. Fancy algorithms are buggier than simple ones, and they're much harder to implement. Use simple algorithms as well as simple data structures.

  5. Rule 5. Data dominates. If you've chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.

Pike's rules 1 and 2 restate Tony Hoare's famous maxim "Premature optimization is the root of all evil."

Ken Thompson rephrased Pike's rules 3 and 4 as "When in doubt, use brute force.".

Rules 3 and 4 are instances of the design philosophy KISS.

Rule 5 was previously stated by Fred Brooks in The Mythical Man-Month. Rule 5 is often shortened to "write stupid code that uses smart objects".

联系我们 contact @ memedata.com