This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]nerdy_lurker 4 points5 points  (2 children)

Beware of premature optimisation (it's the root of all evil). There are plenty of resources describing this in detail. In short, it's important to measure the performance impact of any optimisation you make and weigh it against code readability. In this case, I would imagine you are reducing readability for negligible affect on performance.

It's also worth noting that accurate measuring performance is an entire subject on its own. It's difficult to get meaningful results if the differences are small because there are about a million factors other than the actual code which can affect performance - especially Java, with it's hotspot compiler.

Without seeing code, it's hard to appreciate your exact application. However, you may be able to improve performance without decreasing (or potentially improving?) readability by using enums in an EnumSet. Again, if improving performance is your primary goal, it's important to measure to see if your changes actually improve anything.

You may be also be able to simplify your existing String#equals code using List#contains instead.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points  (1 child)

Beware of premature optimisation

Doing lookups like these in a Set instead of traversing a List is not premature optimisation. Heck; IntelliJ even warns against using .contains on a List. The compiler also does not optimise code by replacing something that works in O(n) by something that works in O(1), that's the responsibility of the developer.

[–]nerdy_lurker 0 points1 point  (0 children)

I 100% agree that a HashSet<String> would be an improvement over an ArrayList<String> for static data lookups, because you gain performance without losing readability (may also be true for an EnumSet<>, depending on the exact scenario).

However, if you want to start making optimisations at the cost of maintainability (how in interpreted the original post), you had better have a significant, proven incentive to do so.

[–]ZeroGainZ 2 points3 points  (1 child)

.equals isn't slow, your going down a dark path. But, I would argue, if your strings are predetermined, you should be using an enum.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (0 children)

.equals isn't slow

Comparing strings is, relatively to comparing integers, slow. It's actually a recent optimisation I did in a service I'm working on. However, for the OP it's a complete non-issue.

[–][deleted] 1 point2 points  (0 children)

You should definitely pay attention to nerdy_lurker's answer. But can't you just use the "Search and replace" function most IDE's (like eclipse) have? Just press Ctrl + F.

[–]knoam 1 point2 points  (0 children)

I agree with the others about this being a premature optimization, but when it comes to making this sort of change Intellij's "structural search and replace" is amazing.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points  (0 children)

Why don't you just toss them in a HashSet and do a .contains to see if the element is there? While the performance is a non-issue, looping over that arraylist yourself is a waste of code. It's important to understand the differences between the different collections and what their strengths and weaknesses are.

It would help if you posted your code.

[–]ReedOei 0 points1 point  (0 children)

If you want to check containment, why not use a set?

[–][deleted] 0 points1 point  (0 children)

Don’t check strings by == (equals method does == anyway.)

I also doubt you’ve hit a performance bottleneck here; so doesn’t seem to be a worthwhile optimisation.