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

whats faster - test for identity vs. (cast + booleanValue())  RSS feed

 
Tom Hughes
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone know what's faster ?
MyArray is an array of Object and the 4th element is always a Boolean.

or

I'm writing a parser to performance is ery important.
cheers,
Tom
 
Zakaria Haque
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code you posted, identity check is not what you want, as a matter of fact, as long as you are creating the Boolean instance with a new operator, identity check with Boolean.TRUE will always return false. Rather than using,
if (MyArray[0] == Boolean.TRUE) //do summit
you should use
if (MyArray[0].equals(Boolean.TRUE)) //do summit
Among the following two expresions
MyArray[0].equals(Boolean.TRUE)
((Boolean) MyArray[0]).booleanValue()
It does not make much sense to think about speed, since the performance gain is almost absolutely insginificant.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using SDK 1.4, then there's no reason to ever use new Boolean(boolean) anyway - use Boolean.valueOf(boolean) instead. This allows you to omit the pointless creation of many identical objects - and also allows you to get away with using == rather than equals(), if you like. Identity check with == will in fact be slightly faster than value check with equals(). But still, it's unlikely to be noticeable - and is prone to failure if you or another programmer accidentally use new Boolean() rather than Boolean.valueOf(). More significant is the elimination of unnecessary object creation, as already mentioned.
 
Tom Hughes
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies.
In regards to 'creating' my objects, I think what I was doing originally is faster than both of your suggestions. A typical line where I create my object array would be like this.

In this way I can check for identity and do not actually instatiate any objects. I figure Boolean.valueOf(boolean) does pretty much this, but seeing as in my case I always know at compile time what I am going to add to the Object array, it is quicker to do it my way.
I know the peformance difference is going to be minimal but in this case I am writing a parser, so for parsing large messages this code is going to be executed a huge amount of times, so I figured if there was a quicker way to do it, I may as well do it as the difference it makes to me coding the thing is negligible.
cheers,
Tom
 
Tom Hughes
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know the answer to this query ?
cheers,
Tom
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, using the literal values is faster than creating new ones. The valueOf() method also uses the literal values internally - if you're getting the value of a boolean variable (i.e. you don't know in advance whether it's true or false) then it makes perfect sense. But if, at the point you need a Boolean, you know exactly which one you want, then there's no reason to use Boolean.valueOf(true) instead of Boolean.TRUE.
As for your query - I answered it, didn't I?

Identity check with == will in fact be slightly faster than value check with equals().

Anyway, it should be easy to test this sort of thing yourself. If you can't detect a difference by testing, then it really doesn't matter.
 
Tom Hughes
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my apologies, your right - THANKS A LOT !!
Tom
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Identity check with == will in fact be slightly faster than value check with equals().

Why? a value check with equals() is nothing more than an identity check on the objects pointer.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, for Boolean it's overridden so that new Boolean(true).equals(new Boolean(true)) returns true. So there can be a bit more overhead, as it's necessary to actually retrieve the Boolean object from the heap to find out what its internal boolean value is.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!