zoukankan      html  css  js  c++  java
  • java中int->String 3种方式效率分析

    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 FilesJavajdk1.8.0_181injava.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'
    };
     

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

     

  • 相关阅读:
    线程,协程
    python魔法方法详解
    Sorted方法排序用法
    time模块
    Haroopad安装与配置: Linux系统下最好用的Markdown编辑器
    C++ Primer第五版答案
    Ubuntu14.04安装有道词典(openyoudao)
    Ubuntu14.04下Sublime Text 3解决无法输入中文
    OpenLTE安装教程
    GNU Radio: Overview of the GNU Radio Scheduler
  • 原文地址:https://www.cnblogs.com/zad27/p/9820873.html
Copyright © 2011-2022 走看看