• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look the Long.java code.
 
Happily living in the valley of the dried frogs with a few tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic