Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Testing Class Name  RSS feed

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea which one of these is faster:
(s.getClass().getName().equals("MyClass"))
or
(s instanceof MyClass)

Thanks,
Drew
 
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
The "instanceof" is most likely faster, as it's implemented in one bytecode. But it also has a different meaning. Your string comparison will only return true if the object is of the exact class named, while the "instanceof" version will evaluate to true for all subclasses of the named class as well.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you really need to know about the exact class, the following is both faster and (more importantly) easier to read and maintain:

(Though notice that when working with different ClassLoaders, they are *not* the same!)
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(s.getClass() == MyClass.class)
I tried using this in a simple Java 1.1 game and my performance decreased substantially! Almost to the point where the game was unplayable.
Why do you say it is faster?
So far, I think the instanceof comparison seems to be the fastest in my situation.
Drew
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to admit that it was a wild guess...
As I think about it, it would be reasonable if instanceof would be faster, though I still think that the String comparison should be slower.
I wouldn't expect it to be to much of a difference with *modern* JVMs, so I would almost always go with the most expressive (and correct, of course) version.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can certainly say that
s.getClass() == MyClass.class
is faster than
(s.getClass().getName().equals("MyClass"))
However it's not necessarily faster than instanceof. This probably depends on JVM version - if you're using 1.1, reflection methods like getClass() were a lot slower then than they are today. If you had a real JVM, you'd prbably get different results.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost any time we're checking classes this way I get suspicious of the design. Do you want to talk about your original problem and alternative solutions?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, there's probably a polymorphism-based solution to whatever you're trying to do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!