by klysm · 1y ago · 0 comments
You do profiling because it's WAY too easy to get obsessed about theoretical problems when a simple measurement will show you the actual problems.
You do the math on the actual problem location, not a method with O(n!) which only gets called with n=3.
You still have to look at the entire call stack when profiling (which means thinking about the overarching algorithm).
The fact remains most projects that do small trivial modular prototypes first will ultimately know which paths are viable before painting themselves into a corner algorithmically.
Best of luck =3
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.Measuring doesn't mean don't think. Measuring and thinking are two different things. You need to do them both to optimize effectively.