aspose file tools*
The moose likes Beginning Java and the fly likes autoboxing performance question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "autoboxing performance question" Watch "autoboxing performance question" New topic
Author

autoboxing performance question

Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
Hi, I have a quick question here about autoboxing performance.

I know that autoboxing numbers for use with lists is much slower than dealing with arrays of primitives, but how does it compare to manual wrapping/unwrapping?

Common sense tells me that the boxing/unboxing behavior should never give poorer performance than doing it manually. For example, in each of following three cases, the first statement should not be any slower than the second one? (Assuming only one statement is executed in each run, so there is no JVM or CPU magic speeding up subsequent similar statements.)



So am I correct in my reasoning, or does autoboxing/unboxing incure a performance hit that I am unaware of.

Thank you,
Yuriy
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
You're mostly correct. In some cases the autoboxing could be faster, in other cases slower. The JVM is required to cache certain values, so in those cases it's not actually creating a new object where "manually" wrapping it would. In those cases, it might be slower to do it yourself. On the other hand, if you were working with a set of common values that you have cached where the JVM doesn't and you pulled from that cache it might be slower.

For most intents and purposes you can consider them equivalent.
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
Thank you for the explanation, Ken!

Yuriy
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You will likely see false claims surrounding auto-boxing and performance (I know of one book that is blatantly incorrect). What is often overlooked is the ability for the runtime compiler to determine whether or not a method fits the description of a mathematical function.

C compilers have been doing it for decades but (as with Java) they must be conservative since there is not an available proof (since they cannot affect program output!). In the case of the wrapper types they are all able to be mutated to a set of functions (aka they are immutable and do not affect observable state) Simple Type-Theoretic Foundations for Object-Oriented Programming (1993). Therefore, such an optimisation is always possible and probable - most runtime compilers can (at least should be capable) deduce that each function can be inlined. Doing so is a single performance hit at one point in time with a performance gain over time (unlike C compilers that do the optimisation at compile-time and gain over time thereafter). You might also want to check out the -XXCompileThreshold JVM option and importantly, ignore the misleading hyperbole (as I said, there is at least one publication that simplifies this reality so as to draw false conclusions).

In any case, I have never seen:
1) A performance description within the context of Java that is accurate (alarming? I think so)
2) a Java application without a performance bottleneck that is thousands of times more detrimental to program execution than the one mentioned - I claim, because mathematics/logic and Java are orthogonal entities that cannot co-exist. An unfortunate consequence of the state of our tertiary institutions, blah, blah, rant...


Tony Morris
Java Q&A (FAQ, Trivia)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Tony Morris:
You will likely see false claims surrounding auto-boxing and performance (I know of one book that is blatantly incorrect). What is often overlooked is the ability for the runtime compiler to determine whether or not a method fits the description of a mathematical function.

C compilers have been doing it for decades but (as with Java) they must be conservative since there is not an available proof (since they cannot affect program output!). In the case of the wrapper types they are all able to be mutated to a set of functions (aka they are immutable and do not affect observable state) Simple Type-Theoretic Foundations for Object-Oriented Programming (1993). Therefore, such an optimisation is always possible and probable - most runtime compilers can (at least should be capable) deduce that each function can be inlined. Doing so is a single performance hit at one point in time with a performance gain over time (unlike C compilers that do the optimisation at compile-time and gain over time thereafter). You might also want to check out the -XXCompileThreshold JVM option and importantly, ignore the misleading hyperbole (as I said, there is at least one publication that simplifies this reality so as to draw false conclusions).

In any case, I have never seen:
1) A performance description within the context of Java that is accurate (alarming? I think so)
2) a Java application without a performance bottleneck that is thousands of times more detrimental to program execution than the one mentioned - I claim, because mathematics/logic and Java are orthogonal entities that cannot co-exist. An unfortunate consequence of the state of our tertiary institutions, blah, blah, rant...


Was anything in my post inaccurate? With the optimizations you mentioned, is there any difference at all between "manually" wrapping the values or using autoboxing?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Perhaps it is a bit general. The notion that creating a new object somehow affects performance is a simplification that does not necessarily hold. Since new Integer(7) and new Integer(7) are entirely equivalent in terms of specification, the runtime compiler is free to treat them so.

In other words, given:

...there is absolutely no way that x and y can be differentiated except with the reference equiality comparison operator. Ever seen articles that say things like "never use the equality comparison operator" or "strive to use immutable types"?

<propaganda>
If only we knew we were just reinventing functional programming (we can't due to inherent apathy and lack of formal analysis skills in our culture perhaps?). We have gone backwards in the last 20 years and poor old Backus is 80 years old - let's show him that his creation has merit while we can and ditch this crap!
</propaganda>
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Actually, it might help to acknowledge that the following code has indeterminate output:

Whether or not this a bad thing is an aside. What is important is that is was done and for a reason.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Tony Morris:
Perhaps it is a bit general. The notion that creating a new object somehow affects performance is a simplification that does not necessarily hold. Since new Integer(7) and new Integer(7) are entirely equivalent in terms of specification, the runtime compiler is free to treat them so.


Well that would be why it was littered with "could" and "might". I've never worried much about it one way or the other. In every project I've worked on the performance difference wouldn't be anything we'd care about anyway. I was more concerned with whether the post was directed at the "manual" vs autoboxing question or the notion that using Integer was going to be significantly slower than using primitives.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Fortunately Yuriy's actual question seems to have been answered already, so digressions into Tony's usual pet peeves aren't too damaging to the original topic. One would think that if functional programming is an important topic to consider, it might occasionally warrant a thread of its own rather than always being inserted into other topics which are only loosely related.

[Tony]: I claim, because mathematics/logic and Java are orthogonal entities that cannot co-exist.

If entities are orthogonal, they are independent, neither helping nor hindering one another. If entities "cannot co-exist", they are not orthogonal.


"I'm not back." - Bill Harding, Twister
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Jim Yingst:
One would think that if functional programming is an important topic to consider, it might occasionally warrant a thread of its own rather than always being inserted into other topics which are only loosely related.


It is exactly this misconception that compells to do such a thing. They are not loosely related at all. I had a conversation yesterday with a fellow Haskell/OCaml user, and he posed the question:
"How do I convince Java zealots that they are reinventing functional programming with a deficient tool that can never cut it?" Now this question should be elaborated on in order to quantify it, but I'll skip that.

I didn't come up with a satisfactory answer, but together we did offer lots of conjecture. We concluded that showing a Java user a pure FP language like Haskell, Erlang or Clean is too much of a step for most people. That is, the person is likely to conclude that there exists two worlds and not make the connection since the leap is too great. This is of course a generalisation and I know many people personally who have made exactly this step, but the appeal is to the masses, not individuals.

We thought of languages like CL, Scheme, OCaml and figured that they are probably good half-way steps, but they will not display a true representation of functional programming - certainly not pure FP - in a way that makes them worth learning i.e. there is an investment in time/effort to make these leaps of understanding but these are not worth it for the return of understanding.

After much discussion, we threw out the value of a fringe language such as Scala. We concluded that this is probably the best value for investment in understanding for return on that understanding so long as the participant is willing to concede that they are looking at a partial solution i.e. a step in a forward direction (as opposed to a backward one as has been done since the tech-boom-wreck over the last couple of decades) requiring many more steps to go.

I realise that you (Jim) - and probably many others - consider my rants excessive noise, but if just one person is able to humble themselves and start questioning the legitimacy of what many others might perceive as "expertise", then my "war" is won. I have a sole agenda of promoting progress in this industry through propagation of information (or destruction of misinformation) which I claim is incredibly prolific.

Am I an authority on right and wrong? Absolutely not. If I were to claim such a thing, I'd be just as hypocritical as IBM, Sun or Microsoft who claim to purport truth (but do the exact opposite). Instead, I would prefer an audience who is capable of thinking for themselves, but "cannot see the foret through the trees" and so just need someone to point in the right direction. Sadly, such an audience is very rare but glimpses of hope do arise - I am an optimist after all.
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
Java is an imperative(or procedural) language. Java has assignment to variables, so it is not a functional language.

Complaining about functional programming in terms of java is like a Prolog fan complaining that Scheme is not a declarative language.

Is Java perfect? Heck no, not even close to a robust, "clean" lenguage. But few languages(if any) can boast of regularity, that is the language has perfect generality, orthogonality and uniformity.

Java is a tool, nothing more. If you don't like it, you never have to use it. These theoretical digressions, while interesting, are not helpful to a beginners forum.

Perhaps we need an anal-retentive language lawyer forum.
[ September 27, 2006: Message edited by: Robert Hill ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Robert Hill:
Java is an imperative(or procedural) language. Java has assignment to variables, so it is not a functional language.

I am not implying anything to the contrary, only that developers (particularly those that are considered "experienced") attempt to apply attributes of functional languages - perhaps unknowingly - and fall desperately short of ideal. That Java has assignment to variables is exactly an example of "never being able to cut it".

Exactly why are people saying things like:
* "Favour immutable objects" (referential transparency)
* "Avoid checked exceptions" (f(x) may not evaluate, but behaviour is determinate if x is known)
* "Use a callback" (first-class functions)
* "Avoid too many methods on an interface" (function currying)
* "Use a mock framework if you have lots of methods to 'mock out'" (partial application)
* "How do I convert my List to an array?" (type dictating equivalent contract)

To take it further:
* "Why aren't a java.util.List<T> and java.util.Map<Integer, T> assignable to each other?"
* "Why aren't List<Character> and String assignable to each other?"
* "Why aren't boolean[] and an int assignable to each other?"

and further again:
* "Why do we (the Java community) keep writing the same thing but give it a different name?"

We (myself and some others) are toying with the idea of presenting FP languages specifically targetted to users of Java (since I am biased towards this audience anyway) in a public forum anyway - discussion is welcome.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tony Morris:
... We (myself and some others) are toying with the idea of presenting FP languages specifically targetted to users of Java (since I am biased towards this audience anyway)...

I don't mean to sidetrack this thread any further, but I'm curious: Why are you biased towards an audience of Java users? And why do you use Java?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by marc weber:

I don't mean to sidetrack this thread any further, but I'm curious: Why are you biased towards an audience of Java users? And why do you use Java?


I am biased that way because I arrived at these somewhat unorthodox positions by following the same lines of reasoning that most Java developers follow only to extend it further. I picked up Java one day and said "does this work?". After some years of analysis, I find that it doesn't and in trying to make it work, I independently reinvented many aspects of functional programming. I admit that at one point I held a position that is very common today. I once believed in the legitimacy of all those refutations that are thrown at me and would have once been the one doing the throwing, but continual reasoning has brought to light some contradictions in these positions and so I have been forced to shift.

I target that audience for no reason in particular but that I believe I am capable and as qualified as much as anyone else. As an example, I regularly argue with a friend who is a user of OCaml who holds contradictory views on the values of pure functional programming and lazy evaluation (and importantly, both things at once), but since I do not have a deep enough understanding of OCaml sometimes so I am forced to concede with "you may be right since I do not know" or "I need to stop and think about it some more". Of course, this discussion requires a thorough knowledge of many topics at once - far beyond that which is required to understand Java and start writing a Hello World J2EE application.

I concede that internet forums are probably not the right place for this kind of dialog. Nobody(?) on this forum knows me personally and quite often my words are taken way out of context - maybe it's a property of myself.

The target of Java is so much easier - it is blatantly obvious what Java really is when you extract all those deeply held preconceptions of reality that are actually "not quite true". That is, someone makes a very subtle and minor assumption that is false and builds up an entire perspective of reality that is based on that assumption. The only opposition is corporations with vested financial interest and those who subscribe to what they purport. I am of the belief that there is much more unlearning to do than learning. My son (pre-school age) is learning Haskell using an interpreter - he can achieve tasks that my former colleagues never could only because he makes no assumptions and I am careful not to give him any that may turn out to be false (I am but human).

I find nobody who has a deep understanding of both functional programming and Java who does not disagree with me at a very broad level (we nitpick on other issues). Someone has to be right - I don't care too much about who it is (yet another position shift possibly?), but when I found out which it is, it will most certainly be profound! It is foolish for me say I am wrong, since if I were, I'd be right immediately with a position shift. Nobody says they are wrong then doesn't assume a position that they think is the correction. Likewise, it would be foolish of me to spend much effort to go around spreading what I see as lies and mistruth when I have nothing to gain from it (unless you accuse me of enjoying the suffering of others or something also profound).
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I picked up Java one day and said "does this work?". After some years of analysis, I find that it doesn't ...


Tony, I enjoy reading your posts and hope I'm learning a little over time, but I just can't get my head around any useful reading of "Java doesn't work." Teams in my company are building applications of tremendous value to the company, some that differentiate us in the marketplace to the extent that Wall Street analysts care about our release schedules. I'd have to say Java works. And COBOL worked before that.

I understand appreciation of theoretical beauty, and even some uncharitable feelings toward those who don't share your own viewpoint on it. I had a music theory teacher who could not tolerate 12-bar blues because of the V-IV chord change. Breaks functional harmony rules, often features parallel voices in 4ths and 5ths, on and on. It hurt his ears. I just figured it was his loss.

So could we do this cheaper, better, faster with some other language or better Java practices? I wouldn't doubt it, and that's why I keep reading your posts. Help us go that direction and we'll all listen.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tony Morris:
...

Yes, but why do you use Java at all, given the inherent shortcomings and flaws that seem to aggravate you so? There must be something you like about Java...
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by marc weber:

Yes, but why do you use Java at all, given the inherent shortcomings and flaws that seem to aggravate you so? There must be something you like about Java...


When it comes down to it, I use Java because I have two very hungry children. It would be blind and ideological of me to say "because Java is a deficient tool, I am going to starve myself and my dependants".
However, I am not aggravated at all - I assure you I sit in my chair completely rational and reasoned about what I am doing - even if it is using the Java programming language or describing it in some way.

To be more honest, I have been on full-time research for the last few months (not using Java), which is quite enjoyable and I appreciate the opportunity offered by my employer, but if tomorrow I were put on a Java project, I wouldn't care so much. Aggravation - perhaps towards corporations and assumed authorities spreading disinformation to a naive audience who lack the ability to reason (they don't teach logic at school anymore). I even see people with fancy job titles and things like that who have internalised the legitimacy of their own "expertise" and have no idea whatsoever that they really have achieved so little compared to what is available. Whatever the case, I try to keep this a side issue - I'd rather just assist in enlightenment of those who seek it and embrace the opportunity myself when it arises.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
For what it is worth, the ICFP 2006 has just been held and is the largest international conference of its kind. Although unable to attend, I am looking forward to what is going to come out of it.

I pose the question; If we (a majority of industry) are "just reinventing functional programming with a deficient tool", then what is the next logical step?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Hey Tony -

I'd rather just assist in enlightenment of those who seek it


You have a potentially interesting topic. My request is that you start your own thread(s) concerning this topic, but start them in either the intermediate or advanced forum. When you hijack a thread in the beginner forum you do a disservice to the people involved in the thread, and you do a disservice to yourself, as you're far less likely to get an interesting conversation going.

thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
For the record I'd be quite interested in seeing the aforementioned thread(s). The advanced forum is so often devoid of advanced topics it's a perfect fit.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Bert Bates:
Hey Tony -



You have a potentially interesting topic. My request is that you start your own thread(s) concerning this topic, but start them in either the intermediate or advanced forum. When you hijack a thread in the beginner forum you do a disservice to the people involved in the thread, and you do a disservice to yourself, as you're far less likely to get an interesting conversation going.

thanks,

Bert

Point taken and I agree.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Stan James:
[QB]

Tony, I enjoy reading your posts and hope I'm learning a little over time, but I just can't get my head around any useful reading of "Java doesn't work."

This requires a slight digression to a branch of Philosophy called Axiology. Perhaps this is a suitable topic to discuss elsewhere - it is a simple discussion but one that needs to be had (since "X doesn't work" definitely holds and definitely doesn't hold at the same time without it ).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: autoboxing performance question
 
Similar Threads
Confusion in Integer Object
"==" and Number-Wrappers
Autoboxing help
Integer CompareTo
i need help with a simple add, subtract, cal. applet