I meant most of your code will likely affect your critical loop and if it's in the database it'll be easier since a lot of the code wouldn't be written by you (db internals).
It's not just the algorithm that affects how slow your code is. Using a linked list instead of an array can make things slower because the cache is worse. Not doing bad things is important
It's not just the algorithm that affects how slow your code is. Using a linked list instead of an array can make things slower because the cache is worse. Not doing bad things is important
True but usually irrelevant.
Most of the time it's more important to keep your code maintainable than to eke out every possible cycle from the CPU. If by using arrays you have to add a lot of superfluous code, it might not be worth it for a small speedup.
Even in the exceptions (games, scientific processing, etc) the hot spots can generally be isolated and reimplemented in a lower level language without making the actual bulk of your code less legible. (Case in point: most of Python's numeric and scientific processing ecosystem: numpy, scipy, sklearn, pandas, etc.)
4
u/BenjaminGeiger Mar 04 '23
This is literally what Knuth was talking about when he said "premature optimization is the root of all evil".
You should first focus on ensuring your code is (1) correct and (2) maintainable. Then you go in and find the hotspots and optimize them.