五个编程原则:Rob Pike‘s 5 Rules of Programming

原文

https://users.ece.utexas.edu/\~adnan/pike.html

Rob Pike's 5 Rules of Programming

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.

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.

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.)

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.

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".

翻译

Rob Pike的编程五原则

规则1. 你无法确定程序将花费时间的具体位置。瓶颈往往出现在意想不到的地方,因此不要试图预测并在证明瓶颈位置之前进行速度优化。

规则2. 测量 。在测量之前不要为了速度进行调优,即使在测量之后,也不要调优,除非代码的某一部分压倒了其他部分。

规则3. 在n很小时,复杂算法速度很慢,而且n通常很小。复杂算法具有很大的常数。除非你知道n经常会变大,否则不要使用复杂算法 。(即使n变大,也要先遵循规则2。)

规则4. 复杂算法比简单算法更容易出错,而且实现起来更困难。使用简单算法以及简单数据结构。

规则5. 数据至关重要。如果你选择了正确的数据结构并组织得当,算法几乎总是不言自明的。数据结构而不是算法是编程的核心。

Pike的第1和第2条规则重新阐述了Tony Hoare的著名格言"过早优化是万恶之源 "。Ken Thompson将Pike的第3和第4条规则重新表述为"怀疑时,使用暴力法"。规则3和规则4是KISS设计哲学的实例。规则5以前由Fred Brooks在《人月神话》中提出。规则5通常缩写为"编写使用智能对象的愚蠢代码"。

笔记

规则5倒是没想到的,但我不是很认同。

反例如Dijkstra最短路径算法,或者经典算法都是反例。

似乎没有经典数据结构这个说法。

个人一直觉得是相辅相成的,都很重要,很难分先后。

当然,以数据为中心 ,大部分事务实际都是基础算法的组合,将数据描述清楚了,问题都已经解决80%了。

从这个角度(以数据为中心)来讲,数据结构的确优先于算法。

相关推荐
黑科技Python5 小时前
生活中的“小智慧”——认识算法
学习·算法·生活
Yupureki5 小时前
从零开始的C++学习生活 16:C++11新特性全解析
c语言·数据结构·c++·学习·visual studio
青云交5 小时前
Java 大视界 -- Java 大数据在智能教育学习社区互动模式创新与用户活跃度提升中的应用(426)
java·大数据·学习·flink 实时计算·智能教育社区·互动模式创新·用户活跃度
武清伯MVP7 小时前
阮一峰《TypeScript 教程》学习笔记——类型映射
笔记·学习·typescript
月阳羊7 小时前
【论文学习与撰写】Mathtype的安装与word插件安装
学习·word
BreezeJuvenile8 小时前
MAX30102脉搏血氧传感器相关内容整理(理论版)
学习·max30102·心率血氧传感器
报错小能手8 小时前
C++笔记(面向对象)深赋值 浅赋值
c++·笔记·学习
一介书生-0078 小时前
2025-10-27 Java AI学习路线
java·人工智能·学习
繁花与尘埃9 小时前
CSS引入方式(本文为个人学习笔记,内容整理自哔哩哔哩UP主【非学者勿扰】的公开课程。 > 所有知识点归属原作者,仅作非商业用途分享)
css·笔记·学习
Century_Dragon9 小时前
新能源汽车故障诊断与排除虚拟实训软件:赋能职业教育利器
学习