I am working on an application and one design approach involves extremely heavy use of the instanceof
operator. While I know that OO design generally tries to avoid using instanceof
, that is a different story and this question is purely related to performance. I was wondering if there is any performance impact? Is is just as fast as ==
?
For example, I have a base class with 10 subclasses. In a single function that takes the base class, I do checks for if the class is an instance of the subclass and carry out some routine.
One of the other ways I thought of solving it was to use a "type id" integer primitive instead, and use a bitmask to represent categories of the subclasses, and then just do a bit mask comparison of the subclasses "type id" to a constant mask representing the category.
Is instanceof
somehow optimized by the JVM to be faster than that? I want to stick to Java but the performance of the app is critical. It would be cool if someone that has been down this road before could offer some advice. Am I nitpicking too much or focusing on the wrong thing to optimize?
instanceof
is really fast, taking only a few CPU instructions.Apparently, if a class
X
has no subclasses loaded (JVM knows),instanceof
can be optimized as:The main cost is just a read!
If
X
does have subclasses loaded, a few more reads are needed; they are likely co-located so the extra cost is very low too.Good news everyone!
Answering your very last question: Unless a profiler tells you, that you spend ridiculous amounts of time in an instanceof: Yes, you're nitpicking.
Before wondering about optimizing something that never needed to be optimized: Write your algorithm in the most readable way and run it. Run it, until the jit-compiler gets a chance to optimize it itself. If you then have problems with this piece of code, use a profiler to tell you, where to gain the most and optimize this.
In times of highly optimizing compilers, your guesses about bottlenecks will be likely to be completely wrong.
And in true spirit of this answer (which I wholeheartly believe): I absolutely don't know how instanceof and == relate once the jit-compiler got a chance to optimize it.
I forgot: Never measure the first run.
With regard to Peter Lawrey's note that you don't need instanceof for final classes and can just use a reference equality, be careful! Even though the final classes cannot be extended, they are not guaranteed to be loaded by the same classloader. Only use x.getClass() == SomeFinal.class or its ilk if you are absolutely positive that there is only one classloader in play for that section of code.
instanceof is very efficient, so your performance is unlikely to suffer. However, using lots of instanceof suggests a design issue.
If you can use xClass == String.class, this is faster. Note: you don't need instanceof for final classes.
I just made a simple test to see how instanceOf performance is comparing to a simple s.equals() call to a string object with only one letter.
in a 10.000.000 loop the instanceOf gave me 63-96ms, and the string equals gave me 106-230ms
I used java jvm 6.
So in my simple test is faster to do a instanceOf instead of a one character string comparison.
using Integer's .equals() instead of string's gave me the same result, only when I used the == i was faster than instanceOf by 20ms (in a 10.000.000 loop)
I have got same question, but because i did not find 'performance metrics' for use case similar to mine, i've done some more sample code. On my hardware and Java 6 & 7, the difference between instanceof and switch on 10mln iterations is
So, instanceof is really slower, especially on huge number of if-else-if statements, however difference will be negligible within real application.