• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nicolas Stern
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
wise owen
Ranch Hand
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look the Long.java code.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic