java中int->String 3種方式效率分析
阿新 • • 發佈:2018-10-20
nbsp ecif digits bsp num turn mode getchar obj
1.0 int轉String方式
java中,int轉String共有如下3種方式
(1) 字符串拼接(即num+"")
(2) String.valueof(num)
(3) Integer.toString(num)
其中,方法(2)內部直接調用了方法(3),效率相差無幾
2.0 效率測試
1 int[] intArr = new int[1000000];
2 String[] strArr1 = new String[1000000];
3
4 Long s0 = System.currentTimeMillis();
5 for (int i = 0; i < 1000000; i++) {
6 intArr[i] = i + 1;
7 }
8 Long e0 = System.currentTimeMillis();
9
10 Long s1 = e0;
11 for (int i = 0; i < 1000000; i++) {
12 strArr1[i] = String.valueOf(intArr[i]);
13 }
14 Long e1 = System.currentTimeMillis();
15
16 Long s2 = e1;
17 for (int i = 0; i < 1000000; i++) {
18 strArr1[i] = Integer.toString(intArr[i]);
19 }
20 Long e2 = System.currentTimeMillis();
21
22 Long s3 = e2;
23 for (int i = 0; i < 1000000; i++) {
24 strArr1[i] = intArr[i] + "";
25 }
26 Long e3 = System.currentTimeMillis();
27
28 System.out.println("s0 = " + s0);
29 System.out.println("e0 = " + e0);
30 System.out.println("s1 = " + s1);
31 System.out.println("e1 = " + e1);
32 System.out.println("s2 = " + s2);
33 System.out.println("e2 = " + e2);
34 System.out.println("s3 = " + s3);
35 System.out.println("e3 = " + e3);
36 System.out.println("String.valueOf(i):" + (e1 - s1));
37 System.out.println("Integer.toString(i):" + (e2 - s2));
38 System.out.println("num + \"\":" + (e3 - s3));
測試結果如下
"D:\Program Files\Java\jdk1.8.0_181\bin\java.exe"
s0 = 1539999876082
e0 = 1539999876085
s1 = 1539999876085
e1 = 1539999876173
s2 = 1539999876173
e2 = 1539999876243
s3 = 1539999876243
e3 = 1539999876291
String.valueOf(i):88
Integer.toString(i):70
num+"":48
那麽為何在JDK1.8中,String.valueOf()效率為何比字符串拼接低呢?
3.0 源碼分析
3.1 字符串拼接
字符串拼接解釋:https://docs.oracle.com/javase/8/docs/api/
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.
String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.
For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.
3.2 Integer.toString()
1 public static String toString(int i) {
2 if (i == Integer.MIN_VALUE)
3 return "-2147483648";
4 int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); //判斷i位數
5 char[] buf = new char[size];
6 getChars(i, size, buf);
7 return new String(buf, true);
8 }
getChars()
1 static void getChars(int i, int index, char[] buf) {
2 int q, r;
3 int charPos = index;
4 char sign = 0;
5 // 初始化符號
6 if (i < 0) {
7 sign = ‘-‘;
8 i = -i;
9 }
10
11 // Generate two digits per iteration
12 while (i >= 65536) {
13 q = i / 100;
14 // really: r = i - (q * 100); 獲得 十位 個位
15 r = i - ((q << 6) + (q << 5) + (q << 2));
16 i = q;
17 buf [--charPos] = DigitOnes[r];
18 buf [--charPos] = DigitTens[r];
19 }
20
21 // Fall thru to fast mode for smaller numbers
22 // assert(i <= 65536, i);
23 for (;;) {
24 q = (i * 52429) >>> (16+3);
25 r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
26 buf [--charPos] = digits [r];
27 i = q;
28 if (i == 0) break;
29 }
30 if (sign != 0) {
31 buf [--charPos] = sign;
32 }
33 }
final static char [] DigitTens = {
‘0‘, ‘0‘, ‘0‘, ‘0‘, ‘0‘, ‘0‘, ‘0‘, ‘0‘, ‘0‘, ‘0‘,
‘1‘, ‘1‘, ‘1‘, ‘1‘, ‘1‘, ‘1‘, ‘1‘, ‘1‘, ‘1‘, ‘1‘,
‘2‘, ‘2‘, ‘2‘, ‘2‘, ‘2‘, ‘2‘, ‘2‘, ‘2‘, ‘2‘, ‘2‘,
‘3‘, ‘3‘, ‘3‘, ‘3‘, ‘3‘, ‘3‘, ‘3‘, ‘3‘, ‘3‘, ‘3‘,
‘4‘, ‘4‘, ‘4‘, ‘4‘, ‘4‘, ‘4‘, ‘4‘, ‘4‘, ‘4‘, ‘4‘,
‘5‘, ‘5‘, ‘5‘, ‘5‘, ‘5‘, ‘5‘, ‘5‘, ‘5‘, ‘5‘, ‘5‘,
‘6‘, ‘6‘, ‘6‘, ‘6‘, ‘6‘, ‘6‘, ‘6‘, ‘6‘, ‘6‘, ‘6‘,
‘7‘, ‘7‘, ‘7‘, ‘7‘, ‘7‘, ‘7‘, ‘7‘, ‘7‘, ‘7‘, ‘7‘,
‘8‘, ‘8‘, ‘8‘, ‘8‘, ‘8‘, ‘8‘, ‘8‘, ‘8‘, ‘8‘, ‘8‘,
‘9‘, ‘9‘, ‘9‘, ‘9‘, ‘9‘, ‘9‘, ‘9‘, ‘9‘, ‘9‘, ‘9‘,
} ;
final static char [] DigitOnes = {
‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
} ;
final static char[] digits = {
‘0‘ , ‘1‘ , ‘2‘ , ‘3‘ , ‘4‘ , ‘5‘ ,
‘6‘ , ‘7‘ , ‘8‘ , ‘9‘ , ‘a‘ , ‘b‘ ,
‘c‘ , ‘d‘ , ‘e‘ , ‘f‘ , ‘g‘ , ‘h‘ ,
‘i‘ , ‘j‘ , ‘k‘ , ‘l‘ , ‘m‘ , ‘n‘ ,
‘o‘ , ‘p‘ , ‘q‘ , ‘r‘ , ‘s‘ , ‘t‘ ,
‘u‘ , ‘v‘ , ‘w‘ , ‘x‘ , ‘y‘ , ‘z‘
};
由上可知,Integer.valueOf(),是逐位轉換;而字符串拼接則是使用StringBuilder.
java中int->String 3種方式效率分析