You donāt understand where the program is consuming your time. bottleneck is where you least expect it. Donāt guess. Donāt speed up until you can prove where the bottleneck is. Measure. Donāt accelerate until you measure. Donāt speed up unless you measure it and one part of the code is consuming significantly more time than another. (Translation by Nishio of Rob Pikeās text)
Rob Pike, who became one of the great masters of C, offers a slightly different angle in Notes on C 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.
Rule 6. There is no Rule 6.
translation with notes
- tell is a nuance of āyou canāt say with certainty,ā but writing it that way is muddled, so Iāve added āyou donāt understand.ā
- The nuance of āspeed hackā or ātuneā is āto rewrite a small part of the source code for the purpose of increasing speed,ā but since it is muddled, I used āspeeding up.
- The meaning of the word alone would include āmaking it faster by devising the data structure and design,ā but weāre talking about making it faster without identifying the bottleneck after the source code is done, so thereās no misunderstanding.
- And even thenā has the nuance of āmeasure first, donāt speed up until you measure, and even thenā¦ā However, when translated into Japanese, āmeasureā in front of āand even thenā is far away. Therefore, we decided to cut the sentence and restate āmeasureā.
Related: Basics of the Unix Philosophy.
orthographical variants - First, measurement
This page is auto-translated from /nishio/ęØęø¬ćććŖćčØęø¬ćć using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. Iām very happy to spread my thought to non-Japanese readers.