-4

There are a number of Java programming practices that I commonly see in use that can be optimized to provide significant speedups.

Example: For lot's of + operations on Strings, use StringBuilder instead.

What are some simple, useful optimizations one can make to potentially improve your program's performance by a significant amount?

EDIT: I'm not looking for trivial premature optimizations. This is not my intention in asking this question. Instead I would like to learn common constructs/mistakes that do constitute a significant performance hit.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • There are tons of little things you can do to optimize your program. So theres not real answer to this question. – RMT Jul 26 '11 at 15:19
  • 2
    Your example is a premature optimization. *When Strings are added using the + operator, the compiler in Java 5 and 6 will automatically use StringBuilder* [source](http://javamoods.blogspot.com/2010/02/optimization-dont-do-it-compiler-will.html) – Goran Jovic Jul 26 '11 at 15:24
  • 1
    @Goran Jovic: This is only true if the +'s are in one statement. If you are concatenating in a loop, `StringBuilder` is significantly faster and certainly not premature optimization. – tskuzzy Jul 26 '11 at 15:26
  • @tskuzzy: No, this covers loops too. If all pluses are in one statement and all arguments are String literals compiler should go even further and replace entire expression with a single String object. – Goran Jovic Jul 26 '11 at 15:30
  • I don't understand why this was closed. I am looking for concrete facts in asking this question. – tskuzzy Jul 26 '11 at 15:31
  • @Goran Jovic: Please see the answers in this question: http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java – tskuzzy Jul 26 '11 at 15:32
  • I had no idea about the String + compiler optimization. Very nice. – RHSeeger Jul 26 '11 at 15:38
  • @tskuzzy: Seems you were right about the loop. Thanks for sharing! Anyway, your problem was most likely closed because there is no finite answer to it. It would probably spawn dozens of answers each containing a single optimization. This is usually not considered constructive because it is very easy for initial Q&A format to degrade to a poll. – Goran Jovic Jul 26 '11 at 15:44
  • @Goran Jovic: You're just forgetting the JIT here. While the java compiler isn't intelligent enough to optimize the StringBuilder init, append, toString in the loop, it's quite possible that the JIT will do it. So this is one of those premature optimizations that make the code completely unreadable without real profit. I dare anyone to find a real world program where replacing all StringBuilders with string concat will make a noticeable difference. – Voo Jul 26 '11 at 16:43
  • @Voo: If it was just another premature optimization that makes code unreadable, why is it in the Java library and not deprecated? It has made a very significant difference in some of my programs. – tskuzzy Jul 26 '11 at 17:07
  • @tskuzzy Well because the compiler/JIT needs the class internally if it wants to optimize it? You'll notice there's also a Unsafe class in Java but you'll hardly see anyone use it directly. And I'd love to hear what performance critical application in Java spends most of its time appending strings in a loop (and then, I'd still have to check if the JIT doesn't optimize it away - that's certainly a possibility) – Voo Jul 26 '11 at 19:13
  • @Voo: Well here is proof that `StringBuilder` is faster: http://java.blogs.webucator.com/2010/04/04/java-optimization-with-stringbuilder/ . As for an example of an application that appends `String`s in a loop, just think of the `toString()` method for a `LinkedList` or any kind of large data structure. – tskuzzy Jul 26 '11 at 19:27
  • @tskuzzy First of all I doubt that the JIT optimized that loop if they really tested the code in that way, which makes it quite useless. And then your real-word performance critical application in Java is a for loop in a toy program? Your other examples are also quite likely only to happen in a debug situation or exceptional circumstances (say logging) - nothing performance critical at that. – Voo Jul 26 '11 at 20:40
  • @Voo: The link was not my real-world example -_-". And how is writing a library function not a real world example? Using StringBuilder seemed important enough for the Java developers to use it in their JDK. Regardless, it seems that I won't be able to convince you of it's use so I'd like to end this discussion. Perhaps you may not have ever run into (or ever will) such a situation where it's useful and that's fine. Believe what you will. – tskuzzy Jul 26 '11 at 23:57

1 Answers1

3
  1. Spend at least an order of magnitude more time thinking hard about the most efficient algorithms and data structures for the problem as you do thinking about how to micro-optimize specific operators, control-flow constructs, etc.
  2. See (1)
Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • I've spent years studying/researching algorithms/data structures. But a bad implementation of a good algorithm is no good either. – tskuzzy Jul 26 '11 at 15:28
  • If you know as much as you imply about algorithms/data structures, you can only be agreeing with my advice. – Patrick87 Jul 26 '11 at 15:39
  • If you're advice was indeed 100% correct, you would not have taken the time to type of this question http://stackoverflow.com/questions/6754454/speed-difference-between-if-else-and-ternary-operator-in-c and instead better spent your time reading an algorithms book. – tskuzzy Jul 26 '11 at 15:41
  • 1
    A few problems with this: (a) I wasn't asking a question about optimization since, if you read my post, you'd see that I had explicitly turned optimization off; rather it was a question about how the compiler was generating code, and the accepted answer reflects that (b) even if this did represent an inconsistency or contradiction in my opinion, it says nothing about the validity of this advice; in other words, "tu quoque" arguments are fallacious. – Patrick87 Jul 26 '11 at 15:44
  • Why do we use buffered input/output? Why do we support SIMD instructions? Why do people overclock? These are all small optimizations on the same order of magnitude that people use because they CAN and result in a noticeable performance difference. There comes a time when the algorithm is no longer the bottleneck of your program. – tskuzzy Jul 26 '11 at 16:04
  • There's a difference between providing hardware support/faster hardware and micro-optimizing code given to the compiler, and I suspect you know it. While there's a place for for profiling/tweaking (and even post-processing the assembly output by the compiler), that's small potatoes compared to getting everything else right. In my defense, I said to spend 10x more time on the big picture... not that you should spend zero time on micro-optimizing. – Patrick87 Jul 26 '11 at 16:12
  • I agree that certain things are too trivial to consider in most cases. However why is it that there can't be optimizations to be made that are higher level than "post-processing assembly output" and the overarching algorithm? In my question, I referred to one such non-trivial optimization and I'll wager there are many more. – tskuzzy Jul 26 '11 at 17:20