How expensive is .getClass() in Java?
<p>I'm in the process of porting over a certain data processing algorithm from Java to C++. The reason for re-writing the code is portability, it needs to run in environments where Java is not available. However, as a side benefit some performance improvement was expected.</p>

<p>Basically, the algorithm reads data from a graph made up of objects with pointers to each other and then computes a result. During the computation numerous object allocations are made, so perhaps this is responsible for the slowdown. The thing is, the C++ code currently runs about 10 times faster than the old Java code. This was really unexpected. I only thought I'd see an improvement of maybe 50-60%.</p>

<p>Unfortunately, I'm not at liberty to post the code here for analysis. It's several thousand lines, so I'm not sure how convenient that would be anyway.</p>

<p>The thing is, the algorithm is almost exactly the same. The only major difference I can think of is in Java there are many daughter classes of a single super class and if(object.getClass() == daughterx.class) is called many times during the computation whereas in the C++ code only one general class is used (since there are few code differences between the daughter classes) and a simple integer comparison is used eg. if(object->type == 15)</p>

<p>How expensive is the Object.getClass() method in Java? What exactly is happening at the low-level when this method is invoked?</p>
