Skip to main content

Obsolete Java Optimization Tips



There are number of performance tips made obsolete by Java compiler and especially Profile-guided optimization . For example, these platform-provided optimizations can drastically (according to sources) reduces the cost of virtual function calls. VM is also capable of method inlining, loop unrolling etc.





What are other performance optimization techniques you came around still being applied but are actually made obsolete by optimization mechanisms found in more modern JVMs?


Comments

  1. The final modifier on methods and method parameters doesn't help with the performance at all.

    Also, the Java HotSpot wiki gives a good overview of the optimizations used by HotSpot and how to efficiently use them in Java code.

    ReplyDelete
  2. People replacing String a = "this" + var1 + " is " + var2; with multiple calls to StringBuilder or StringBuffer. It actually already uses StringBuilder behind the scenes.

    ReplyDelete
  3. In 2001 I made apps for a J2ME phone. It was the size of a brick. And very nearly the computational power of a brick.

    Making Java apps run acceptably on it required writing them in as procedural fashion as possible. Furthermore, the very large performance improvement was to catch the ArrayIndexOutOfBoundsException to exit for-loops over all items in a vector. Think about that!

    Even on Android there are 'fast' loops through all items in an array and 'slow' ways of writing the same thing, as mentioned in the Google IO videos on dalvik VM internals.

    However, in answer to your question, I would say that it is most unusual to have to micro-optimise this kind of thing these days, and I'd further expect that on a JIT VM (even the new Android 2.2 VM, which adds JIT) these optimisations are moot.
    In 2001 the phone ran KVM interpreter at 33MHz. Now it runs dalvik - a much faster VM than KVM - at 500MHz to 1500MHz, with a much faster ARM architecture (better processor even allowing for clock speed gains) with L1 e.t.c. and JIT arrives.

    We are not yet in the realms where I'd be comfortable doing direct pixel manipulation in Java - either on-phone or on the desktop with an i7 - so there are still normal every-day code that Java isn't fast enough for. Here's an interesting blog that claims an expert has said that Java is 80% of C++ speed for some heavy CPU task; I am sceptical, I write image manipulation code and I see an order of magnitude between Java and native for loops over pixels. Maybe I'm missing some trick...? :D

    ReplyDelete
  4. It is necessary to define time/memory trade-offs before starting the performance optimization. This is how I do it for my memory/time critical application (repeating some answers above, to be complete):


    Rule #1 Never do performance optimization on the earlier stage of development. Never do it if your don't need it really. If decided to do it, then:
    use profiler to find bottlenecks, review the source code to find the reasons for bottlenecks;
    choose appropriate data structure with the best fit into the defined time/memory trade-offs;
    choose appropriate algorithms (e.g. iteration vs recursion, etc);
    avoid using synchronized objects from java library, if you don't need it really;
    avoid explicitly/implicitly new object creation;
    override/re-implement data types/algorithms coming with the java if and only if you are sure they doesn't fit your requirements.
    Use small, independent tests to test the performance of chosen algos/data structures.

    ReplyDelete
  5. "Premature optimization is the root of all evil"(Donald Knutt)
    It is useful to optimize only the bottlenecks places.
    You should analyze the code in each situation. May be you can replace the TreeSet by more fast HashSet, because you don't need sorting feature or may be you can use float instead of double( look in Android SDK).
    If no technique helps, you can try to rewrite a piece of code and call it via JNI, so that native code is working.

    ReplyDelete
  6. Don't manually call the garbage collector, it hurts performance on modern JVM implementations.
    Integer instead of Long will not save much space, but will limit the range of the numbers.
    Avoid hand generated Enum classes and use the built in Enum instead. Java 1.5 introduced real Enums, use them.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.