by 9rx · 1y ago · 0 comments
If a problem area is so intuitively obvious, why would you introduce the problem in the first place? In reality, performance optimizations are usually needed where you least expect them. Which means that you can't get there intuitively. Hence, the suggestion of using profiling to help track down where the problem is instead.
Bring in a performance expert and their intuition can quickly identify many performance improvements. The tough part for the business is when and where do you pay for the experience of performant code, and when is good enough to leave alone?
Pretty much everyone I know will throw down an O(n^2) algorithm or whatever in their first pass and replace it with something more thought out once they have the time to think deeply about it.
If you're fretting about optimization at every stage of development, you're really doing it wrong. This is precisely the type of early optimization that everyone and their dog will tell you is bad. You should focus first on making your program work and laying out the logic and data flows. Optimization does not matter until the program is almost finished.
Most of the times I've seen this, the faster algorithm is literally just a dictionary with a well-defined key.
I honestly do not understand why that's not the first solution most devs think of and why n^2 seems to dominate.
As an example, I've seen code like this quiet a bit
result = []
for (item: items) {
for (item2: items) {
if (item != item2 && item.foo == item2.foo) {
result.add(item)
}
}
}
Easily replaced and massively easier to read as result = []
itemFoo = HashSet()
for (item: items) {
if (itemFoo.contains(item.foo))
result.add(item)
else
itemFoo.add(item.foo)
}
The mindset of re-looking for values in a collection you are already iterating through is just foreign to me. The first solution for something like this in my mind is always utilizing dictionaries.Disagree
> faster for small n (which is the common case)
How can you know that? And for smaller n, the choice hardly matters.
> can not run out of memory due to the hash table
If n gets large enough that hash table size becomes a problem, n is large enough that you need the hash table or some other structure that reduces the complexity down from O(n^2).
> compiles to less code
This is just nonsense. Any codebase of significant size already has a hashtable setup and unless you are using a language like C++, the code size of using a hash table anywhere is constant. The cost is already paid.
The actual amount of compiled code from the switch is identical.
> does not need item to be hashable
True.
There would be reasons not to choose option 2, but it should be default until it's been proven to be a problem. And even then, the next step is almost certainly not going to be option 1.
But then this could also be language and application specific. I deal primarily with backend services/Java where memory is plentiful, and allocation is fast. Algorithmic complexity is for my applications very often the better indicator of what good code should be.
A pretty common attitude that I've ran into. People take the "premature optimization" quote to mean "Never think about big oh when writing code".