This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nicolas Stern
Ranch Hand
Posts: 57
  • 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: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look the Long.java code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic