Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cost of a call to instanceof  RSS feed

 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody got an idea of the cost of an call? Which would be faster?

Would it be faster to keep a String with the object type in the object itself?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know for sure whether instanceof is faster or slower than string comparison, but I do know that storing a string containing the class name sounds horrible. Until you're absolutely sure that (a) there really is a performance improvement, and (b) you **really** need the tiny performance improvement that you might get, don't do it. Make your code concise, readable and maintainable.

By the way, instanceof a bit of a "code smell". If you are writing proper, object-oriented code, making use of polymorphism etc., then you should rarely, if ever, need to use instanceof. Are you sure you couldn't rework your code so you don't need instanceof?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since instanceof is VERY widely used in core Java classes I'm sure it has been highly optimzed.
Looking at my copy of Venners "Inside the Java virtual machine" I see that instanceof is not a call but a JVM opcode - you cant get faster than that.
Bill
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that instanceof is almost certainly faster or at worst equal to any alternative. Moreover, forgetting about performance, using Strings would mean losing the ability to detect subtypes. Consider that an ArrayList is an instance of List as well as an instance of Collection. The instanceof operator understands this. How would you detect this by comparing the strings "ArrayList", "List", and "Collection"? Not easily, I'm sure. This is exactly what instanceof is designed for; there's no need to try to invent an alternative to avoid an imagined problem.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't imagine a programe where 'instanceof' would be your bottleneck.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by steve souza:
I can't imagine a programe where 'instanceof' would be your bottleneck.


Me neither. I see a lot of uses of instanceof where a better design would get rid of it, though.
 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all your valuable comments. I used instanceof and didn't notice any performance issue. I guess it's pretty fast.

In my code I have to use Instance of, the scenario is as follows. Please correct me if I'm doing anything wrong here (mostly in the design perspective).

I have a set of commands being fed to a server. I have modeled these commands into objects which hold the parameters necessary for executing the command. Since these commands share some common parameters I noticed that I can form an Inheritance hierarchy, which I did as follows:

The server design has a single method which accepts an object of Type Command, performs standard validations on the common parametrs and then determines the command type by checking on the object type with the instanceof operator.

I would appreciate if you can contemplate on my design approach. Any constructive comments on it are highly appreciated.

Thanks.

[ July 02, 2006: Message edited by: Isuru Sampath ]

[ July 02, 2006: Message edited by: Isuru Sampath ]
[ July 04, 2006: Message edited by: Isuru Sampath ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like a classic implementation of the "Command pattern." To remove the instanceofs, you just give the base command class an abstract method, and move all the class-specific code into the subclass's implementations of that method. Then in the server, you simply call the single method on the incoming command; all the type-specificity is then handled by polymorphism rather than by if-instanceof-then.
 
Isuru Sampath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Earnest. The solution you suggested is perfect.

I just can't undertand why did not thought about it. :roll:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!