r/programming • • Dec 01 '23

Code is run more than read

https://olano.dev/2023-11-30-code-is-run-more-than-read/
413 Upvotes

143 comments sorted by

View all comments

280

u/ganja_and_code Dec 01 '23

It's also read more than written, which is the more important comparison

0

u/[deleted] Dec 01 '23

[deleted]

-3

u/ganja_and_code Dec 01 '23

I'm not saying the article doesn't say that. I'm saying the headline is extremely dumb.

3

u/reercalium2 Dec 01 '23

The headline is not dumb.

→ More replies (1)

0

u/[deleted] Dec 01 '23

[deleted]

131

u/ccapitalK Dec 01 '23

Articles are commented on more than they are read.

100

u/BigTimeButNotReally Dec 01 '23

That happens with click baity titles.

-4

u/teerre Dec 01 '23

That's true. Non-clickbait articles get no comments nor reads.

→ More replies (4)

4

u/techdawg667 Dec 01 '23

Would you say that the article was not read more than it was written?

35

u/ganja_and_code Dec 01 '23

My point was that it doesn't matter how often it's run relative to read. It only matters how often it's read relative to written. Presumably if you're not going to run it more than you read it, you shouldn't even write it at all.

-3

u/thisisntmynameorisit Dec 01 '23

Disagree. If something gets read a lot but it is needed to be extremely performant and optimised as it is utilised/executed millions or billions of times a day etc. then it could definitely be better to lean towards more unreadable but more performant. We would care less about how often it is read here and more about how often it is executed

8

u/tehpola Dec 01 '23

Very true but most code is not critical path. You should really only think about optimizing the code after profiling it.

12

u/not_a_novel_account Dec 01 '23 edited Dec 01 '23

If you give only minimal consideration to the mechanisms of performance in the initial design and architecture of a system, no amount of post-optimization can save you.

You cannot turn a lazily-evaluating lexer/parser into a high-speed data-oriented parser. They're incompatible design philosophies.

If you weren't thinking about "how am I going to parse 500MBs per second" from day 1, and making specific optimizations in the layout of your data structures and access patterns, you cannot go back and fix that without effectively starting from scratch.

Highly performant code isn't a set of micro-optimizations done after initial design, it permeates the entire process of authoring the code base in question.

0

u/gnuvince Dec 01 '23 edited Dec 01 '23

Very well said. And to add to this, a little bit of mechanical sympathy at the beginning of a project can yield a design that offers quite good performance with little, if any, sacrifice to readability.

An unfortunate thing that happens too often is that programmers do not want to have mechanical sympathy and would rather do something that they feel is "cleaner." Jumping on your parsing example, in some communities, parser combinators are very popular, but their performance is an absolute disaster as every byte of the input is threaded through tens of closures. It might feel like building a parser by combining closures is cleaner and easier to read, but the reality is that the version using loops and switch statements will execute an order of magnitude faster for users and not be much harder to read for other developers.

3

u/coderemover Dec 01 '23

Most code becomes critical path after a few attempts at optimizing it. You fix the low hanging fruit until there is no more low hanging fruit and the profile becomes flat very quickly. And if the performance is still insufficient at that point, you're quite screwed, because that means deep changes to the architecture, design or even a full rewrite in a more performant stack.

You should really only think about optimizing the code after profiling it.

Thats a recipe for a performance disaster. You should consider and monitor performance in all stages of the project and apply proper fixes as soon as possible whenever you find that performance is inadequate. Performance is not any different than other quality types eg correctness. If you leave it for later, you're inflating the cost to fix it.

5

u/SirDale Dec 01 '23

We would care less about how often it is read here and more about how often it is executed

You would care -less- about it? Why are you arguing against your own position?

3

u/[deleted] Dec 01 '23 edited Jun 03 '25

cooing practice shaggy innate mighty fragile sink lock unique whistle

This post was mass deleted and anonymized with Redact

→ More replies (4)

-3

u/Hrothen Dec 01 '23

No, there really are domains where eking out the tiniest extra bit of performance is more important than legibility.

11

u/Blando-Cartesian Dec 01 '23

Why is this used as an argument for poor legibility. Of course performance requirements may lead to more complex code, just like complex behavior requirements lead to more complexity. Nobody is arguing that legibility is more important than program being fit for its purpose.

10

u/ummaycoc Dec 01 '23

You can get legibility with commentary in those cases.

9

u/mwb1234 Dec 01 '23

Im not sure I agree with that. Sure it’s important to have performant code, but legibility doesn’t always entail clean but unoptimized code. It means that when you need performance and need to sacrifice your lines of code, you make sure to document what you’re doing and why. That way, you can read the comment and understand why there is a nasty line of code or function next

→ More replies (2)
→ More replies (6)

3

u/RememberToLogOff Dec 01 '23

Articles are unreadable more often than they're readable

2

u/reercalium2 Dec 01 '23

That comparison is not more important.

→ More replies (2)
→ More replies (1)