The time when counting instructions or loops can give you a good idea of the performance of any code is long gone, thanks to the many optimizations that occur at all levels of software execution.
This value is especially for VM-based languages, where the JVM may just skip some steps because it knows that it is not necessary.
For example, I read some time ago in an article (I will try to find and relate it to time) that these two methods are pretty much equivalent to cost (in the JVM HotSpot):
public void frobnicate1(Object o) { if (!(foo instanceof SomeClass)) { throw new IllegalArgumentException("Oh Noes!"); } frobnicateSomeClass((SomeClass) o); } public void frobnicate2(Object o) { frobnicateSomeClass((SomeClass) o); }
Obviously, the first method works more , but the JVM knows that the type o has already been marked in if and can actually skip the cast check type later and make it non-op.
This and many other optimizations make counting "flops" or cycles almost useless.
Generally speaking, instanceof checking is relatively cheap. In JVM HotSpot, it comes down to numerically checking the type identifier in the object header.
This classic article describes why you should "write dumb code."
There is also a 2002 article that describes how to optimize instanceof in the JVM HotSpot .
source share