• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know the answer to this query ?
cheers,
Tom
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my apologies, your right - THANKS A LOT !!
Tom
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic