This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Performance difference between Long.longValue() and Long.parseLong() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Performance difference between Long.longValue() and Long.parseLong()" Watch "Performance difference between Long.longValue() and Long.parseLong()" New topic
Author

Performance difference between Long.longValue() and Long.parseLong()

Nicolas Stern
Ranch Hand

Joined: Apr 26, 2002
Posts: 57
Hi everybody,

I�m trying to understand why is the reason that there is a slight performance difference between these two loops:

for ( int i = 0; i < 10000000; i++ ){
String s = ""+i;
Long myLong = new Long(s);
long l = myLong.longValue();
}

for ( int i = 0; i < 10000000; i++ ){
String s = ""+i;
long l = Long.parseLong(s);
}

On my machine, the loop using the longValue() method is about 10 % slower than the one that uses the parseLong() method.

Is that because parseLong is static ?
or is that because of the new Long() instanciation ?

Thanks for your answers.

Cheers - Nicolas
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Is that because parseLong is static ?
or is that because of the new Long() instanciation ?


It looks like it is likely the instantiation... as the constructor that takes a String actually calls parseLong internally.

Henry
[ May 17, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Look the Long.java code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Performance difference between Long.longValue() and Long.parseLong()