by hsn915 · 5y ago · 0 comments
When Knuth wrote that quote, he meant something very specific with the word optimization: low level micro optimizations. Spending a lot of time trying to get every little ounce of performance from every little CPU instruction.
High level reasonable design decisions are not "optimizations" in this sense at all. You should think of them as non-pessimization.
Choosing Python or another slow language is premature pessimization. You just make your program slower for no reason.
So choosing a language based on how programs written in it perform is simply non-pessimization.
If you are interested in an expansion on this idea, checkout Casey Muratori's lecture about philosophies of optimization: https://www.youtube.com/watch?v=pgoetgxecw8
The article makes claims about software being "fast enough" and not needing further optimization. But what you call "fast enough" is probably 10000 times slower than what it could be.
I'm not joking.
People who program in slow languages have a really skewed perception about performance.
If a page takes 5 seconds to load, they don't see that as a problem.
If a server takes 500ms to respond to a request, they don't see that as a problem.
This is simply unacceptable.
On first load, not only the page is ready, but the video has started to auto play (including buffering), under 3 seconds. Less than 500ms on a reload.
The whole project has been running for 10 years on 7 servers. Hosting costs under $1K/mo.
And even better, the code base is a mere 11K LoC, so a single dev can manage it. And make a living out of it.
That's because:
- Python is almost never the bottleneck in a website
- Python wraps compiled languages for slow stuff. That's the whole point.
- your archi matters much more than the language you chose. Because good db providers, proper caching and optimized data fetching will be the pareto moves, unless you are google size.
- you will not code most things anyway. You are not going to code the video encoding, you will use ffmpeg. You will not recode a database, you will use an existing one. You will not code a server, you will use nginx or something else. What you will spend time on is creating logic for your clients.
So I think your are extrapolating this rare use case where your POV makes sense, and ignore the 99% of the case were, indeed, a slow language doesn't matter.
In fact, if electron success tells anything, it's that while I do value speed a lot, the market has voted.
Sounds much more likely that you wrote in python the code that bundle together the libraries that peform the actual network streaming, machine learning, encoding, search, etc, and that those libraries were not written in python.
Where in the stack do you see that Python performances matter?
Only one software out of a 1000 will actually need it. The others will mostly do plumbing or automation.
And scripting languages excel at both.
If you don't fall in that category, then yes, do use a fast language. If you are coding a chat server, use Erlang. If you want to do a rendering engine, use Rust. But if you most likely want to code your company CRUD software and glue it to some external services and providers, use Python.
As we just saw, most of the code forming your stack is not written in python because its performance would not allow it.
> Only one software out of a 1000 will actually need it.
Not what I see from your example.
It seems to me that you are overinflating the volume of the python code that you wrote and are blind to the huge volume of performance critical code you just take for granted.
You probably mean: most of the code that most of the programmer write is disposable code assembling reusable libraries. This code performance does not matter therefore python is fine.
I agree on this. The problem I see with this reasoning though is that since python is so slow and error prone compared to compiled alternatives that it is unfit when scaled up from small disposable programs to largish permanent software, in a world where all programs trend to grow and stick in production for longer than expected.
I don't know what is the website you're running or how many people use it or how much money you make. I know you can make a lot of money doing stuff in python or electron. Does that mean the market has voted that performance doesn't matter? I don't think so. Jira makes a lot of money but almost everyone hates it. I know a lot of other software that is very slow but still makes tons of money.
> you will not code most things anyway
You say that like it's a good thing! It's a bad thing.
You may say it's because you don't need to. But the flip side is that you can't, even if you wanted to.
You can't program a database or a video encoding or a web server in this language you are using to write your program.
So what is the point of this language?
You can't even use it to cache things in memory. That's why everyone uses redis or something similar.
Sure, but that isn't to say his advice can't be applied in other contexts. Applying optimizations before knowing where a bottleneck is just a bad idea, regardless of the situation. To constrain the advice to the original context is sort of odd, but maybe there's more to it than that?
I always thought the risk of premature optimization was similar to the lean principle of deferring commitment: you should to wait until the last possible moment to a decision that has high switching costs. In both cases you want to have as much information as possible to taking action.
As an aside, this bugged me for a long time: how _much_ information do you need? Recently I co-presented with the CEO of a successful manufacturing company. At his firm they use the military's rule of thumb of taking action when you have 70% of the information. Deciding sooner than that and you risk making a bad decision; Waiting for more information after 70% and you likely are missing an opportunity.
But, general high level design decisions that don't produce slow code by default do not suffer from this. They don't make the code any complicated or hard to understand. Quite the opposite, they usually simplify things. Because most of the time, the way to get good performance is to do the minimal thing that needs to be done to perform the task at hand. The code you write will be very straight forward. There's no switching cost.
On the other hand, choosing a slow language has a high switching cost. You can't just take a bad design and try to optimize it. It usually doesn't work. You have to rework the whole design.
EDIT:
I have one other important point to add:
> Applying optimizations before knowing where a bottleneck is just a bad idea, regardless of the situation.
Ignoring any and all performance concerns using this kind of reasoning will result in a codebase that is permeated with slowness. The program is slow but you can't identify any single bottleneck. You end up with a monster that simply can't be fixed.
Worse, you may not even realize how slow it actually is. If you have profiled it and found no particular bottle neck, you may think this simply the limit of computer performance and the task at hand cannot be performed any faster than this.
This actually leads me to see the concept of "micro abstractions", and I'm going to see if I see those in hard to deal with programs, now.
Premature abstraction really is very very bad (I won't go so far as to say it's the root of all evil).
The higher level the abstraction, the worse it is.
Unless it's born from experience writing the same kind of program many times. In which case it's not "premature".
"micro abstractions" are actually fine. You notice a recurring pattern in the code so you "abstract" it away into a function.
Last responsible moment, not last possible, moment. That's a big difference. You shouldn't start on the work at the last minute, that's too late. But you also don't (necessarily) need to start on something 2 years before it's due (unless it's big and/or there are anticipated problems that will need to be addressed that the time would permit; that's what makes it the last responsible moment).
Donald Knuth writes entire volumes of books in assembly language, because the details matter. If you want to have that point, then you probably shouldn't be quoting Donald Knuth.
Donald Knuth is the guy who points out that "for(int i=size-1; i>=0; i--)" is faster than "for(int i=0; i<size; i++)". When he says "don't sweat the small stuff", its because his books are filled with _incredibly_, detailed stuff. Far more detail than any other programming book / algorithms book.
IIRC, the "premature optimization is the source of evil" statement was about how GOTO statements and how efficient they were. The __context__ is that GOTO is indeed a very fast construct (especially in the context of replacing recursion).
> Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
From: "Structured Programming with Goto Statements", emphasis mine.
Archive link: https://web.archive.org/web/20131023061601/http://cs.sjsu.ed... (I haven't read it in a while, and it's 41 pages, so I probably won't get to it today.)
I translated it to C so that people got the gist of it.
There's a great deal of scientific literature about this topic, BTW. Especially in machine learning.
As a fun project, I started writing some code to do calculations on matrices. Wrote it in Python and used Numpy. Then I wrote it in Julia. Almost immediately it was a 100x improvement. I spent time doing "premature optimization" and netted a 10x improvement. So overall it went from minutes to milliseconds.
I guess if you're writing a website or something then I wouldn't worry, but for other things, doing optimization from the get-go can save you a ton of time and effort later on. And really, such optimizations can affect the architecture in ways you wouldn't think of without it. Reworking your architecture down the road is a huge time sink. I like to make sure mine is close to where I want it to be in the beginning.
Despite popular sentiment on HN and Reddit, a website is not a piece of software that can tolerate slow code. Unless you are ok with your website dying when 100 people visit it at the same time, or otherwise investing in a complicated "cloud" infrastructure to make it possible to have more than 100 concurrent visitors.
Requirements that are good enough to warrant a 5 second page load time being "unacceptable" must have, at some point, been prototyped with real use cases, real data, and in real environments.
Getting to that point - where the business case is connected with such a strong through-line to the user experience can literally take years. Early in a project lifecycle it's more important to design software that is easy to change than software that is fast.
Language choice does not matter, performance does not matter; the design philosophy of the developer(s) when building something new is everything. That philosophy should be "I'll likely have to change this" not "I need to make sure this runs quickly". Any high level design decisions should be made with the former in mind, not the latter.
Once you've earned that product wisdom, sure - carve out the slow thing and write it in CUDA or something. If things are easy to change, that's not a scary proposition; it's just part of the natural project lifecycle and Knuth lives to die another day.
"We either have perfect information or we make software that is very slow".
Optimize iteration frequency of features not for loops.
This kind of code does not tend to be rigid as you might imagine. Quite the opposite. Since it doesn't introduce a ton of premature abstractions and stays straight to the point, it tends to be flexible and easy to change/adapt.
You are still thinking in terms of a false dichotomy:
- Flexible but slow
- Fast but rigid
But it's a false dichotomy.
You don't need optimizations to get high performance software. Computers are very fast. You just need non-pessimized code.
It's funny. People use "computer are very fast" to dismiss all performance concerns. But this is not how we should think about it.
"Computer are very fast" means we don't need to optimize (as in micro-optimize on the instruction level) code to get high performance. We just need to get the clowns out of the car.
Using python for shell scripting requires extensive knowledge about the language and the standard library and all the gotchas that you can fall into.
This sort of time investment does not pay off if you just want to write small scripts that perform simple tasks.
If the task is simple, then a bash script would be better.
If the task is not so simple, then a language you are really familar with is better.
If you accept my premise about how programming in slow languages is generally a bad idea, you would not spend that much time learning any such language with all its ins and outs and gotchas.
For background, I personally have spent a lot of time learning python, and only later in life have a realizd what a mistake that was. Now I only use Go for server side programming. I'm also using it for command-line programs. Such programs usually start small but grow over time. Choosing Python for them would have been a costly mistake.
Bash script don't work on the Windows command line, and bash relies on UNIX tools as its "standard library", which also are not available on the Windows command line. That's the whole point really, Python has very good cross-platform support (and in this case cross-platform doesn't just mean "Linux and macOS", but also Windows).
> If the task is not so simple, then a language you are really familar with is better.
IMHO every programmer should be familiar with at least a handful programming languages.
If you OS vendor refuses to supply you with appropriate tools to do your job, then just chose another vendor. Vote with your foots.
Also doesn't windows now ship with a linux compatibility layer anyway (WSL)?
EDIT:
I just realized you are the guy who wrote sokol!
If your main work station is windows and you don't work on linux, then replace bash from my previous comment with bat(ch).
Bash has one strong point: 4 to 5 lines admin scripts for which concision and expressiveness matter more than anything else. That's it.
There is nothing else it can be better at than ruby, python, perl, php, or even go or kotlin.
The argument you need to learn x stands for bash as well, btw.
It's now right up there with "X considered harmful", which 80% of the time translates to "I don't like X".
Specific to Python from the article, I'll add that I personally am done with dynamically typed languages for anything where more than one person needs to work on it or it needs to be long-lived. Static typing is simply too useful. Dynamic typing is (IMHO) a false economy. I've often said "Python means writing unit tests for spelling mistakes". Refactoring a dynamically typed code base of any reasonable size is a comparative nightmare.
Shell good for simple scripts. — Shell scripts are pain to support!
Perl is astonishingly good for text manipulation. — Perl is write-only language!
Python is easy to learn and start coding. — Python2 to Python3 migration is decades long hell!
JavaScript is the language of the World Wide Web. — JavaScript is nightmare to refactor!
And so on.
If language easily accepts anything that developer pops out of his head, then program will be hard to support and refactor.
If language limits developer to certain good, safe, performant solutions, then it hard to write programs in it, because you will need to learn good patterns first, which may take years.
People really underestimate the time it takes for devs to figure this out. My company has been on TypeScript for over three years now and it's a giant mess. Everything is arbitrarily typed with no design philosophy or organization behind anything. Which leads to these massive interfaces with every single field being optional. Which means you're still doing run-time checking all over the place.
I've always maintained that if your team sucks at organization then no amount of tooling will save them. You're not so much working as a team but as bitter enemies. Possibly the turnover is too high in the company. You have mercenaries rather than ideologues. Not that I'd want to work with a team of pure zealots. But there should be enough people holding the architecture in place. It helps morale as well. You don't want to work on a project that no one gives a shit about.
You can design slow crap in any language, but I'd rather use something that has the potential to be robust and fast for my problem domain so I don't have to rewrite the entire app in the future to language "X", when I could have easily started with "X".
Make no mistake - if you do this, you made an upfront error in selecting your language for the business problem. It may not be an unrecoverable error, but it is a failure in judgement regardless.
In this case, since the entirety of the project is to be written in a specific language, it's usually worthy of placing that in the more macro-category - where that choice affects all other decisions.
Don't spend 4 hours of your time on a `for` loop but half an hour of your time deciding on the language and architecture.
I don't mean that as a criticism, either. I keep meaning to strive to having the level of cognizance to memory usage that he just drips into his software.
Viewing language choice as a high level design decision for a project of nontrivial scale is a symptom of bad high-level design decisions.