• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

to get a short from a string

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is better (faster, difference?) to get a short from a string:

this:


or that:
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Essentially, both of them are the same!

(Yanked from javadocs)

public Short(String s)throws NumberFormatException

Constructs a newly allocated Short object that represents the short value indicated by the String parameter. The string is converted to a short value in exactly the manner used by the parseShort method for radix 10.



Always remember: "premature optimization is a root of all evils"
All these things are in most of cases insignificant when it comes to the performance of an entire application.
[ October 23, 2008: Message edited by: Nitesh Kant ]
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, good hint!

thanks!!
 
Saloon Keeper
Posts: 28424
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short short = new Short(1);

The other two have to do ASCII-to-numeric conversions. The constructor does a fast-path call to the parseShort() code, I think (except when it does something even cleverer).

However, once again we live in the age of smart compilation, so it's not a guarantee that one way is faster than another. If I'm not confusing Java with one of the C compilers I've worked with, the optimizer is likely to crunch that down into a constant reference (since Short is immutable), and that specifically, commonly-used constants like 0 and 1 may have further magic applied to them so that when you reference them in code, no actual object may be needed at all. If it can be deduced safely.

I really very strongly discourage micro-optimizations. I started out in an era when you could look at code and tell exactly byte-for-byte what the compiler would produce. Back then, worrying about this sort of thing had some merit. That's because back then, we were impressed when they upgraded our mainframe to a full megabyte of RAM.

However, compiler writers have gotten more and more ambitious over the years to the point that you're no longer certain what will be produced, and even a 1-line change in the middle of a module can cause everything to be optimized in an entirely different way. For example, if you used a Short(1) and added a call to hash() on it, the compiler could no longer dispense with an actual Short object, since a "virtual" Short such as the optimizer could produce wouldn't have a hash value. That would force the optimizer to work with a concrete implementation of Short(1).

Because of this, I don't code clever at the micro-level. The compiler designers did studies based on everyday usage and adjusted their optimizers to recognize and optimize these common optimizable usages. If I confuse them by coding "efficient" constructs based on a brute-force implementation, I can actually produce less efficient code than someone dumber than myself would.

But in the end, there's still no substitute for measurement, and you should always strive for the most improvement for the least amount of work. There's always more work. The hard part is getting someone to pay for it. That, and staying interested in the project.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I got it, thanks you!!
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here I was thinking the answer to the original question was, "it depends on whether you want a Short (upper case S) object, or a short (lower case s) value".
 
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I can think of no case where this difference would be worth the time to post to a forum. Classic premature optimization
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pat:
And I can think of no case where this difference would be worth the time to post to a forum.



Especially when the constructor calls the parseShort() method internally!!
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic