博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中int->String 3种方式效率分析
阅读量:5971 次
发布时间:2019-06-19

本文共 5259 字,大约阅读时间需要 17 分钟。

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 = 1539999876082e0 = 1539999876085s1 = 1539999876085e1 = 1539999876173s2 = 1539999876173e2 = 1539999876243s3 = 1539999876243e3 = 1539999876291String.valueOf(i):88Integer.toString(i):70num+"":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 iteration12         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 numbers22         // 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' };
 

由上可知,String.valueOf(),是逐位转换;而字符串拼接则是使用StringBuilder.

 

 

 

 

转载于:https://www.cnblogs.com/zad27/p/9820873.html

你可能感兴趣的文章
[React] React Router: Nested Routes
查看>>
android: 播放视频
查看>>
nginx 414 Request-URI Too Large
查看>>
Redis 学习(一)
查看>>
不用临时变量,交换两个变量的值
查看>>
RxJava
查看>>
十二、jdk命令之Java内存之本地内存分析神器:NMT 和 pmap
查看>>
Linux 下系统调用的三种方法
查看>>
LINUX 内核调试基础+编程基础
查看>>
mysql 添加列的索引
查看>>
dos下 和 批处理中的 for 语句的基本用法
查看>>
RTTI的实现(vc)--转载
查看>>
CentOS6.3编译安装Memcached的PHP客户端memcache
查看>>
java 中文 乱码 问号
查看>>
adbd cannot run as root in production builds的解决方法
查看>>
vs------各种错误解决方法
查看>>
shell脚本学习总结04--终端信息的获取和设置
查看>>
MySQL SQL优化
查看>>
操作系统中断
查看>>
iOS - UIGestureRecognizer
查看>>