public class NumberFormatTest { public static void main(String[] args) throws Exception { String time = "20"; long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { //1 // return type int NumberFormat.getIntegerInstance().parse(time).intValue(); } System.out.println(System.currentTimeMillis() - start); long start2 = System.currentTimeMillis(); for (int j = 0; j < 100000; j++) { //2 // return type int Integer.parseInt(time); } System.out.println(System.currentTimeMillis() - start2); long start3 = System.currentTimeMillis(); for (int k = 0; k < 100000; k++) { //3 // return type Integer Integer.valueOf(time); } System.out.println(System.currentTimeMillis() - start3); } }
运行几次的结果看来,2,3 是比较快的。。我通常用第二种方法。
java 转整型 哪种方法速度最快?
最新推荐文章于 2022-03-26 13:52:40 发布