Home of The JavaSpecialists' Newsletter

064Disassembling Java Classes

Posted: 2003-02-14Category: PerformanceJava Version: Dr. Heinz M. Kabutz
 

Abstract: 

 

Welcome to the 64th edition of The Java(tm) Specialists' Newsletter sent to 5869 Java Specialists in 94 countries.

I stepped off the plane last Tuesday, having come via London from Estonia, and was hit in the face by 31 degree Celsius heat. I was still wearing my warm clothes that were necessary in the -17 degree Celsius in Estonia (travel report to follow), but very quickly, I adapted again to shorts and T-shirt.

While I was in London, I had the opportunity to meet Jack Shirazi, the author of the best Java performance tuning website. Basically, when I don't know or understand something about Java performance, I go look at the website. Still coming is a review of Jack's book.

NEW: Refactoring to Java 8 Streams and Lambdas Workshop Are you currently using Java 6 or 7 and would like to see how Java 8 can improve your code base? Are you tired of courses that teach you a whole bunch of techniques that you cannot apply in your world? Check out our one day intensive Refactoring to Java 8 Streams and Lambdas Workshop.

Disassembling Java Classes

A few months ago, on our local Java User Group discussion forum, I casually asked the question what was faster: i++, ++i or i+=1. I also mentioned that the answer might surprise them. Wanting to encourage thinking, I never gave the answer ;-)

Yesterday, one of my readers, a bright young student at the Cape Town Technical University, sent me an answer based on a thorough investigation through microbenchmarks. His conclusion was correct, but the approach to getting to the conclusion led to a lot of effort being spent on his part.

The easiest way to decide which is fastest is actually to disassemble the Java Class. Note that I am saying disassemble, not decompile. Let's look at the following class:

public class Increment {
  public int preIncrement() {
    int i = 0;
    ++i; 
    return i;
  }
  public int postIncrement() {
    int i = 0;
    i++; 
    return i;
  }
  public int negative() {
    int i = 0;
    i-=-1; 
    return i;
  }
  public int plusEquals() {
    int i = 0;
    i += 1; 
    return i;
  }
}

I may ask you: which method is the fastest, which is the slowest? C programmers would probably say that the fastest are i++ and ++i. When you try running these methods many times, you will notice slight differences between them, based on which you run first, how soon the hotspot kicks in, whether you use client or server hotspot, whether you are busy playing an MP3 on your machine while running the test and the phase of the moon.

Instead of measuring the performance, why not investigate what the Java compiler did? You can disassemble a class with the standard javap tool available in your JAVA_HOME\bin directory:

javap -c Increment

Note that the class must already be compiled. The result is the following:

Compiled from Increment.java
public class Increment extends java.lang.Object {
    public Increment();
    public int preIncrement();
    public int postIncrement();
    public int negative();
    public int plusEquals();
}

Method Increment()
   0 aload_0
   1 invokespecial #9 <Method java.lang.Object()>
   4 return

Method int preIncrement()
   0 iconst_0
   1 istore_1
   2 iinc 1 1
   5 iload_1
   6 ireturn

Method int postIncrement()
   0 iconst_0
   1 istore_1
   2 iinc 1 1
   5 iload_1
   6 ireturn

Method int negative()
   0 iconst_0
   1 istore_1
   2 iinc 1 1
   5 iload_1
   6 ireturn

Method int plusEquals()
   0 iconst_0
   1 istore_1
   2 iinc 1 1
   5 iload_1
   6 ireturn

Now when we look at the different methods of incrementing the local variable, we can see that they are actually identical!

Next time you are thinking of measuring performance using System.currentTimeMillis(), think of also looking at the generated byte code. It might save you a lot of time.

I want to thank Daniël Maree for inspiring me to write this newsletter through his thorough research as to which increment method was faster.

Kind regards

Heinz

P.S. I hope you all enjoy Valentine's day. Remember to leave work early today - i.e. before 8pm ;-)

 

Related Articles

Browse the Newsletter Archive

About the Author

demo

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

Nobody ever wants to call a Java performance consultant, but with first-hand experience repairing and improving commercial Java applications - JavaSpecialists are a good place to start...

Threading Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.