JAVA中int与string互转的效率对比

今天突然心血来潮,想看看int与string互转的效率,所以试一下。

1.int转string对比

众所周知,int转string有三种方法:

<pre name="code" class="html">* (1) String.valueOf(i)
* (2) Integer.toString(i)
* (3) i + "" 

下面是对比:

public class TestInt2String {
	public static void main(String[] args) {
		int[] intArr = new int[1000000];
		String[] strArr1 = new String[1000000];// 为了公平分别定义三个数组
		String[] strArr2 = new String[1000000];
		String[] strArr3 = new String[1000000];
		// 赋值
		Long t1 = System.nanoTime();
		for (int i = 0; i < 1000000; i++) {
			intArr[i] = i + 1;
		}
		Long t2 = System.nanoTime();
		for (int i = 0; i < 1000000; i++) {
			strArr1[i] = String.valueOf(intArr[i]);
		}
		Long t3 = System.nanoTime();
		for (int i = 0; i < 1000000; i++) {
			strArr2[i] = Integer.toString(intArr[i]);
		}
		Long t4 = System.nanoTime();
		for (int i = 0; i < 1000000; i++) {
			strArr3[i] = intArr[i] + "";
		}
		Long t5 = System.nanoTime();
		System.out.println("t1 = " + t1);
		System.out.println("t2 = " + t2);
		System.out.println("t3 = " + t3);
		System.out.println("t4 = " + t4);
		System.out.println("t5 = " + t5);
		System.out.println("赋值:" + (t2 - t1)/1e6);
		System.out.println("String.valueOf(i):" + (t3 - t2)/1e6);
		System.out.println("Integer.toString(i):" + (t4 - t3)/1e6);
		System.out.println("i+\"\":" + (t5 - t4)/1e6);
	}
}

结果如下:

t1 = 19060665491808
t2 = 19060671491829
t3 = 19060801637489
t4 = 19060834225943
t5 = 19061039822991
赋值:6.000021
String.valueOf(i):130.14566
Integer.toString(i):32.588454
i+"":205.597048

可以看出来,Integer.toString是最快的。

2.string转int对比

string转int

* (1) Integer.parseInt(str)
* (2) Integer.valueOf(s).intValue()
下面是对比:

public static void main(String[] args) {
		String[] strArr = new String[1000000];
		int[] intArr1 = new int[1000000];// 为了公平分别定义三个数组
		int[] intArr2 = new int[1000000];
		int[] intArr3 = new int[1000000];
		// 赋值
		Long t1 = System.nanoTime();
		for (int i = 0; i < 1000000; i++) {
			strArr[i] = Integer.toString(i + 1);
		}
		Long t2 = System.nanoTime();
		for (int i = 0; i < 1000000; i++) {
			intArr1[i] = Integer.parseInt(strArr[i]);
		}
		Long t3 = System.nanoTime();
		for (int i = 0; i < 1000000; i++) {
			intArr2[i] = Integer.valueOf(strArr[i]);
		}
		Long t4 = System.nanoTime();
		for (int i=0;i<1000000;i++){
			intArr3[i] = Integer.valueOf(strArr[i]).intValue();
		}
		Long t5 = System.nanoTime();
		System.out.println("t1 = " + t1);
		System.out.println("t2 = " + t2);
		System.out.println("t3 = " + t3);
		System.out.println("t4 = " + t4);
		System.out.println("t5 = " + t5);
		System.out.println("赋值:" + (t2 - t1) / 1e6);
		System.out.println("Integer.parseInt(s):" + (t3 - t2) / 1e6);
		System.out.println("Integer.valueOf:" + (t4 - t3) / 1e6);
		System.out.println("Integer.valueOf(s).intValue()" + (t5 - t4) / 1e6);
	}

结果如下:

t1 = 20125416119650
t2 = 20125513532523
t3 = 20125542724055
t4 = 20125569908737
t5 = 20125596152378
赋值:97.412873
Integer.parseInt(s):29.191532
Integer.valueOf:27.184682
Integer.valueOf(s).intValue()26.243641
可以看出,基本上相同。
上一篇:SSIS常用的包—XML任务,SQL分析服务执行DDL和Processing任务


下一篇:谈谈J2EE框架Paoding-Rose