views:

1729

answers:

7

There are many ways of converting a String to an Integer object. Which is the most efficient among the below:

Integer.valueOf()
Integer.parseInt()
org.apache.commons.beanutils.converters.IntegerConverter

My usecase needs to create wrapper Integer objects...meaning no primitive int...and the converted data is used for read-only.

+3  A: 

I know this isn't amongst your options above. IntegerConverter is ok, but you need to create an instance of it. Take a look at NumberUtils in Commons Lang:

Commons Lang NumberUtils

this provides the method toInt:

static int toInt(java.lang.String str, int defaultValue)

which allows you to specify a default value in the case of a failure.

NumberUtils.toInt("1", 0)  = 1

That's the best solution I've found so far.

Jon
+8  A: 

Don't even waste time thinking about it. Just pick one that seems to fit with the rest of the code (do other conversions use the .parse__() or .valueOf() method? use that to be consistent).

Trying to decide which is "best" detracts your focus from solving the business problem or implementing the feature.

Don't bog yourself down with trivial details. :-)

Ron

On a side note, if your "use case" is specifying java object data types for your code - your BA needs to step back out of your domain. BA's need to define "the business problem", and how the user would like to interact with the application when addressing the problem. Developers determine how best to build that feature into the application with code - including the proper data types / objects to handle the data.

Ron Savage
we have been using IntegerConverter consistently (across 70-80) classes. But IntegrConverter seems to use reflection creating Intger objects...convert(Integer.class,-1).So consistency is not the issue but efficiency! tx anyway
Pangea
That's too bad, as I like that method the least because there are "built in" simpler ways to do it. :-) I can't however imagine it causing any kind of performance problem - so I would stick to the "if it ain't broke, don't fix it" rule.
Ron Savage
+1 for the slap at the BA :)
Alastair Pitts
A: 

ParseInt returns an int, not a java.lang.Integer, so if you use tat method you would have to do

new Integer (Integer.parseInt(number));

I´ve heard many times that calling Integer.valueOf() instead of new Integer() is better for memory reasons (this coming for pmd)

In JDK 1.5, calling new Integer() causes memory allocation. Integer.valueOf() is more memory friendly.

http://pmd.sourceforge.net/rules/migrating.html

In addition to that, Integer.valueOf allows caching, since values -127 to 128 are guaranteed to have cached instances. (since java 1.5)

Tom
Java now supports auto-boxing, the automatic conversion between boxed and primitive types. So "Integer i = new Integer (Integer.parseInt(number));" is better written "Integer i = Integer.parseInt(number);"
Jim Ferrans
+3  A: 

If efficiency is your concern, use int: it is much faster than Integer.

Otherwise, class Integer offers you at least a couple clear, clean ways:

Integer myInteger = new Integer(someString);
Integer anotherInteger = Integer.valueOf(someOtherString);
Jim Ferrans
I agree. Using the Integer class seems counter to the efficiency concern. Unless of course the efficiency is with regards to just string conversion, rather than integer storage or manipulation.
aberrant80
Use the Integer.valueOf(someOtherString), it's smarter and can possibly use cached Integer instances.
deterb
+3  A: 

Your best bet is to use Integer.parseInt. This will return an int, but this can be auto-boxed to an Integer. This is slightly faster than valueOf, as when your numbers are between -128 and 127 it will use the Integer cache and not create new objects. The slowest is the Apache method.

private String data = "99";

public void testParseInt() throws Exception {
 long start = System.currentTimeMillis();
 long count = 0;
 for (int i = 0; i < 100000000; i++) {
  Integer o = Integer.parseInt(data);
  count += o.hashCode();
 }
 long diff = System.currentTimeMillis() - start;
 System.out.println("parseInt completed in " + diff + "ms");
 assert 9900000000L == count;
}

public void testValueOf() throws Exception {
 long start = System.currentTimeMillis();
 long count = 0;
 for (int i = 0; i < 100000000; i++) {
  Integer o = Integer.valueOf(data);
  count += o.hashCode();
 }
 long diff = System.currentTimeMillis() - start;
 System.out.println("valueOf completed in " + diff + "ms");
 assert 9900000000L == count;
}


public void testIntegerConverter() throws Exception {
 long start = System.currentTimeMillis();
 IntegerConverter c = new IntegerConverter();
 long count = 0;
 for (int i = 0; i < 100000000; i++) {
  Integer o = (Integer) c.convert(Integer.class, data);
  count += o.hashCode();
 }
 long diff = System.currentTimeMillis() - start;
 System.out.println("IntegerConverter completed in " + diff + "ms");
 assert 9900000000L == count;
}

parseInt completed in 5906ms
valueOf completed in 7047ms
IntegerConverter completed in 7906ms
brianegge
+5  A: 

If efficiency is your concern, then creating an Integer object is much more expensive than parsing it. If you have to create an Integer object, I wouldn't worry too much about how it is parsed.

Note: Java 6u14 allows you to increase the size of your Integer pool with a command line option -Djava.lang.Integer.IntegerCache.high=1024 for example.

Note 2: If you are reading raw data e.g. bytes from a file or network, the conversion of these bytes to a String is relatively expensive as well. If you are going to write a custom parser I suggest bypassing the step of conversing to a string and parse the raw source data.

Note 3: If you are creating an Integer so you can put it in a collection, you can avoid this by using GNU Trove (trove4j) which allows you to store primitives in collections, allowing you to drop the Integer creation as well.

Ideally, for best performance you want to avoid creating any objects at all.

Peter Lawrey
+1 for -Djava.lang.Integer.IntegerCache.high Is there a switch for Long as well?
Thilo
No, not sure why.
Peter Lawrey
+1  A: 

I'm always amazed how quickly many here dismiss some investigation into performance problems. Parsing a int for base 10 is a very common task in many programs. Making this faster could have a noticable positive effect in many environments.

As parsing and int is actually a rather trivial task, I tried to implement a more direct approach than the one used in the JDK implementation that has variable base. It turned out to be more than twice as fast and should otherwise behave exactly the same as Integer.parseInt().

public static int intValueOf( String str )
{
    int ival = 0, idx = 0, end;
    boolean sign = false;
    char ch;

    if( str == null || ( end = str.length() ) == 0 ||
       ( ( ch = str.charAt( 0 ) ) < '0' || ch > '9' )
          && ( !( sign = ch == '-' ) || ++idx == end || ( ( ch = str.charAt( idx ) ) < '0' || ch > '9' ) ) )
        throw new NumberFormatException( str );

    for(;; ival *= 10 )
    {
        ival += '0'- ch;
        if( ++idx == end )
            return sign ? ival : -ival;
        if( ( ch = str.charAt( idx ) ) < '0' || ch > '9' )
            throw new NumberFormatException( str );
    }
}

To get an Integer object of it, either use autoboxing or explicit

Interger.valueOf( intValueOf( str ) ).

x4u
The reason that people dismiss it is because gaining a 50% improvement in something that takes 1/1,000,000th of your execution time is a waste. And for any non-trivial program, particularly those that involve databases, string-integer conversion should be far less than a millionth of the execution time.
kdgregory
@kdgregory: With todays ubiquitous prevalence of text based formats like XML, JSON and HTTP parsing numbers from strings is actually a very common task in many non trivial programs. I'd rather say if your programs still spend only very little time in this kind of code they are probably very inefficient somewhere else.
x4u
I don't agree with your use of "inefficient": A simple web request will take 100 milliseconds or so to execute. Parsing the request may take 100 *microseconds*. The former far overshadows the latter, and there's no "efficient" or "inefficient" involved. Nor much that you can do about it.
kdgregory
If you're looking to improve the overall throughput of your program the rules haven't changed since I started working 25+ years ago: you create a realistic workload, profile your application with that workload, and optimize the pieces that actually consume significant amounts of time. Anything else is wasted effort.
kdgregory
I totally agree with you. Profiling is the only way to know what's worth to optimize. But if this shows that your request handling takes 1000 times more time than processing the request, you are either doing some serious work there or wasting time in this code. I.e. using uncached database access in request handlers is something I would consider unacceptable inefficent for high load servers in most scenarios.
x4u