你的位置:首页 > Java教程

[Java教程]java Byte.toString 方法与String.ValueOf(Byte)效率比较


  

    int times = 10000000;    Byte[] li = new Byte[times];    for (int i = 0; i < times; i++) {      li[i] = (byte) i;    }        long timeA = System.currentTimeMillis();    for (int i = 0; i < times; i++) {      li[i].toString();    }    long timeB = System.currentTimeMillis();    for (int i = 0; i < times; i++) {      String.valueOf(li[i]);    }    long timeC = System.currentTimeMillis();    System.out.println("A方法耗时:" + (timeB - timeA));    System.out.println("B方法耗时:" + (timeC - timeB));    if (timeB - timeA > timeC - timeB) {      System.out.println("B方法好");    } else {      System.out.println("A方法好");    }      


执行结果:

A方法耗时:1303

B方法耗时:239

B方法好

 

String.valueOf(li[i]);

li[i] 为何变成Object了?不是应该调用String.valueOf(Byte) ?求高手指教