Re: Cost of creating objects?
On 08/08/2013 12:14 AM, Kevin McMurtrie wrote:
In article <ktt63o$l57$1@news.albasani.net>,
Sebastian <news@seyweiler.dyndns.org> wrote:
Am 07.08.2013 12:02, schrieb Donkey Hottie:
07.08.2013 10:44, Sebastian kirjoitti:
@Override
public int compare(AttrValue o1, AttrValue o2)
{
Long ts1 = o1.getEffectiveSequenceNumber(); // ??
Long ts2 = o2.getEffectiveSequenceNumber(); // ??
return ts1.compareTo(ts2);
}
Would you expect a measureable impact of creating these
variables ts1, ts2, instead of "inlining" the calls to
getEffectiveSequenceNumber(). (Using JDK 6?)
How can I reason about this things, probably influenced by
JIT, without doing actually measurements, say as part of a
code inspection?
-- Sebastian
What is getEffectiveSequenceNumber() returning?
If Long, there is no way to prevent the creation of a Long.
If long, try and use long variables instead, and manually code the
comparison code, it's not that complicated anyway.
Thanks for the hint, yes, I forgot to state that the method returns a
long primitive.
However, your answer does not address my question, which wasn't about
how to code a long comparison.
-- Sebastian
This:
Long l= 4L;
is shorthand for:
Long l= Long.valueOf(4);
It's optimized for some values to return a singleton. For others you'll
get a performance penalty on Long creation. How big that is depends on
your system. Run it a 100 million times and see.
Since comparing the primitives is known to be fast and trivial, I'd just
compare the primitives. There's no point writing potentially slow code
for no good reason.
This little snippet from 5.1.7 of the language spec requires caching:
"If the value p being boxed is true, false, a byte, or a char in the
range \u0000 to \u007f, or an int or short number between -128 and 127
(inclusive), then let r1 and r2 be the results of any two boxing
conversions of p. It is always the case that r1 == r2."
Oddly enough, not for Long, although it's commonly done (as in OpenJDK 7).
Note that this definition does not prohibit other caching. So if using
== it's a really good idea to do some testing.
AHS
--
When a true genius appears, you can know him by this sign:
that all the dunces are in a confederacy against him.
-- Jonathan Swift