What is the difference between new Integer( string) vs Integer.valueOf( string)? I suppose variety is the spice of life, but is there any reason to create Integers one way over the other? The specification notes:
In other words, this method returns an Integer object equal to the value of: new Integer(Integer.parseInt(s))
I take that is sun's way of saying "no difference", correct?
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1863
posted
0
hi Matt, try the following code and review the results for different s[0] argument values. you will notice the difference. i am not sure about the reason but this what i observed. "for first for loop the time is higher than the second for loop". this might lead us to a conclusion that- new Integer() is slower than Integer.valueOf() actually, i tried this coder some days back to compare new Integer().intValue() and Integer.parseInt() which returns "int" and i observed that new Integer().intValue() is taking more time than Integer.parseInt().
here is a result snapshot of what i obtained for limit=9999999, --------------------- 5192 4399 5125 4384 5112 4382 5120 4390 5111 4390 5119 4383 5112 4382 5119 4383 5203 4389 5115 4389 5198 4386 5190 4384 5118 4387 5118 4588 5164 4393 5115 4388 5118 4405 5198 4406 5186 4382 5121 4382 5113 4392 5114 4389 5192 4343 5203 4345 5192 4355 5254 4367 5278 4388 5117 4345 5115 4350 5197 4351 5225 4343 5209 4345 5122 4350 5133 4351 5149 4343 5254 4345 5123 4377 --------------------- for the previous code which was comparing new Integer().intValue() and Integer.parseInt() i get the following snapshot for limit=9999999, ---------------------- 5223 4402 5145 4390 5219 4402 5147 4375 5210 4381 5126 4376 5115 4383 5200 4381 5130 4376 5125 4376 5192 4386 5191 4386 5128 4375 5124 4377 5121 4386 5117 4382 5200 4376 5125 4381 5156 4398 5235 4401 ---------------------- these results complies to my conclusion i guess. here in second example we can argue that new Integer() calls constructor hence explicit object creation takes place and Integer.parseInt() doesn't call explicit constructor but to be frank i am not clear about the exact reason because anyways it has to call constructor at some point (even if it is calling it internally). we can post this to Intermediate or Advance forum to get some convincing answer. regards maulin
Thanks for your time Maulin! I ran your code and watched the same trend (where init arg: 1x10^6) 801 741 781 721 841 741 811 752 801 761 811 741 831 741 801 772 801 761 831 741 831 741 821 812 801 761 791 751 831 751 822 771 801 771 811 751 821 751 842 761 811 771 821 761 832 741 841 751 Interestingly, an additional order of magnitude (I first ran 10,000,000 objs by fat finger) shows much more variance, but the same general trend. I wonder why Integer.valueOf would be so much faster than new Integer?? Thoughts? anyone?
I'm looking at the code and it looks like there is a lot of room for compiler/JIT optimizations. Have a look at the bytecode to see what is actually being executed. Jamie
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1863
posted
0
hi Jamie, i don't know how to look at byte code. can you tell me? i can't do just "vi OrderBy.class" right? and i didn't understand your comment about Optimization. what that has to do with this performance matrix. Or was that related to the byte code stuff you wrote? regards maulin
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1863
posted
0
hi all, can somebody help me with the question i posted in the above post from me? "how to read byte code" and "what to read in there"... regards maulin
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Originally posted by Maulin Vasavada: i don't know how to look at byte code. can you tell me? i can't do just "vi OrderBy.class" right?
No, you can't. Use "javap -c <fully.qualified.classname>".
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus