at java.lang.Throwable.fillInStackTrace(Throwable.java:-1)
at java.lang.Throwable.fillInStackTrace(Throwable.java:782)
- locked <0x6c> (a sun.misc.CEStreamExhausted)
at java.lang.Throwable.(Throwable.java:250) 0x6c>
at java.lang.Exception.(Exception.java:54)
at java.io.IOException.(IOException.java:47)
at sun.misc.CEStreamExhausted.(CEStreamExhausted.java:30)
at sun.misc.BASE64Decoder.decodeAtom(BASE64Decoder.java:117)
at sun.misc.CharacterDecoder.decodeBuffer(CharacterDecoder.java:163)
at sun.misc.CharacterDecoder.decodeBuffer(CharacterDecoder.java:194)
Made 100.000.000 iterations for string ‘12345‘ : time = 12.109 sec
Made 1.000.000 iterations for string ‘12345a‘ : time = 21.764 sec
at java.lang.Throwable.fillInStackTrace(Throwable.java:-1)
at java.lang.Throwable.fillInStackTrace(Throwable.java:782)
- locked <0x87> (a java.lang.NumberFormatException)
at java.lang.Throwable.(Throwable.java:265) 0x87>
at java.lang.Exception.(Exception.java:66)
at java.lang.RuntimeException.(RuntimeException.java:62)
at java.lang.IllegalArgumentException.(IllegalArgumentException.java:53)
at java.lang.NumberFormatException.(NumberFormatException.java:55)
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:441)
at java.lang.Long.valueOf(Long.java:540)
at com.mvorontsov.javaperf.StrConvTests.pack(StrConvTests.java:69)
at com.mvorontsov.javaperf.StrConvTests.test(StrConvTests.java:38)
at com.mvorontsov.javaperf.StrConvTests.main(StrConvTests.java:29)
public static Object strToObject( final String str )
{
if ( str == null || str.length() > 17 )
{ //out of Long range
return str;
}
if ( str.equals( "" ) )
return ""; //ensure interned string is returned
if ( str.length() == 1 )
return str.charAt( 0 ); //return Character
//if starts with zero - support only "0" and "0.something"
if ( str.charAt( 0 ) == ‘0‘ )
{
if ( str.equals( "0" ) )
return 0;
if ( !str.startsWith( "0." ) ) //this may be a double
return str;
}
long res = 0;
int sign = 1;
for ( int i = 0; i < str.length(); ++i )
{
final char c = str.charAt( i );
if ( c <= ‘9‘ && c >= ‘0‘ )
res = res * 10 + ( c - ‘0‘ );
else if ( c == ‘.‘ )
{
//too lazy to write a proper Double parser, use JDK one
try
{
final Double val = Double.valueOf( str );
//check if value converted back to string equals to an original string
final String reverted = val.toString();
return reverted.equals( str ) ? val : str;
}
catch ( NumberFormatException ex )
{
return str;
}
}
else if ( c == ‘-‘ )
{
if ( i == 0 )
sign = -1; //switch sign at first position
else
return str; //otherwise it is not numeric
}
else if ( c == ‘+‘ )
{
if ( i == 0 )
sign = 1; //sign at first position
else
return str; //otherwise it is not numeric
}
else //non-numeric
return str;
}
//cast to int if value is in int range
if ( res < Integer.MAX_VALUE )
return ( int ) res * sign;
//otherwise return Long
return res * sign;
}
Pack: Made 100.000.000 iterations for string ‘12345‘ : time = 12.145 sec
Pack: Made 1.000.000 iterations for string ‘12345a‘ : time = 23.248 sec
strToObject: Made 100.000.000 iterations for string ‘12345‘ : time = 6.311 sec
strToObject: Made 100.000.000 iterations for string ‘12345a‘ : time = 5.807 sec
原文:http://blog.csdn.net/spidercoco/article/details/22033369