主题:【倡议】Java SE 5.0和.NET 2.0擂台赛 -- Highway
首先,我对老轧的程序进行了一些简单的修改,主要如下:
一。取消了对String的测试部分,而只测试StringBuffer和StringBuilder,因为String的性能明显落后,测试的意义不大。
二。测试范围从1000到100,000,步长为1000,这样可以看出一些比较细的东西。
三。每组测试后,我调用了System.gc()来建议JVM立刻进行内存回收,其目的是最小化前次试验对后续试验由于不定期的内存回收所造成的性能震荡。
其次,我的实验分为使用-Client和-Server两种,目的是比较不同参数下的同级别测试的性能差异,另外,所使用的参数还包括了-Xms256M -Xmx256M。
下面是我的测试代码:
package test2005_2;
class StringTest {
private String[] prepareString(int number) {
System.out.println("String number: " + number);
String[] strs = new String[number];
for (int i = 0; i < number; i++) {
strs[i] = "Highway" + i;
}
return strs;
}
private void stringBuilderTest(String[] input) {
long start = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.length; i++) {
sb.append(input[i]);
}
long time = System.currentTimeMillis() - start;
System.out.println("StringBuilder Takes Time: " + time + "ms");
}
private void stringBufferTest(String[] input) {
long start = System.currentTimeMillis();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < input.length; i++) {
sb.append(input[i]);
}
long time = System.currentTimeMillis() - start;
System.out.println("StringBuffer Takes Time: " + time + "ms");
}
public void test() {
String[] strs = null;
for (int i = 1; i <= 100; i++) {
strs = this.prepareString(i * 1000);
this.stringBufferTest(strs);
this.stringBuilderTest(strs);
System.gc();
}
}
public static void main(String[] args) {
StringTest stringtest = new StringTest();
stringtest.test();
}
}
下面是使用了-Server -Xms256M -Xmx256M参数情况下的测试结果:
String number: 1000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 2000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 3000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 4000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 0ms
String number: 5000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 6000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 7000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 8000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 9000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 10000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 11000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 12000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 13000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 14000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 15000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 16000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 17000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 0ms
String number: 18000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 19000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 20000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 21000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 22000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 23000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 24000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 25000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 26000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 27000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 28000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 29000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 31ms
String number: 30000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 31000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 0ms
String number: 32000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 31ms
String number: 33000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 34000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 35000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 36000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 37000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 38000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 39000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 40000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 41000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 42000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 43000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 44000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 45000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 46000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 47000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 48000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 32ms
String number: 49000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 15ms
String number: 50000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 15ms
String number: 51000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 31ms
String number: 52000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 15ms
String number: 53000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 54000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 55000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 16ms
String number: 56000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 16ms
String number: 57000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 58000
StringBuffer Takes Time: 32ms
StringBuilder Takes Time: 15ms
String number: 59000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 60000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 61000
StringBuffer Takes Time: 32ms
StringBuilder Takes Time: 15ms
String number: 62000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 63000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 31ms
String number: 64000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 65000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 66000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 67000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 68000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 69000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 70000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 47ms
String number: 71000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 47ms
String number: 72000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 32ms
String number: 73000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 46ms
String number: 74000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 75000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 32ms
String number: 76000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 31ms
String number: 77000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 32ms
String number: 78000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 31ms
String number: 79000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 80000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 16ms
String number: 81000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 31ms
String number: 82000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 83000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 84000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 85000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 46ms
String number: 86000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 63ms
String number: 87000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 0ms
String number: 88000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 47ms
String number: 89000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 90000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 91000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 92000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 15ms
String number: 93000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 15ms
String number: 94000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 15ms
String number: 95000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 0ms
String number: 96000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 47ms
String number: 97000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 0ms
String number: 98000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 99000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 16ms
String number: 100000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 94ms
下面是使用了 -Client -Xms256M -Xmx256M参数情况下的测试结果:
String number: 1000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 2000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 3000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 4000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 5000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 6000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 7000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 8000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 15ms
String number: 9000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 10000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 11000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 12000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 13000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 14000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 15000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 16000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 17000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 18000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 19000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 20000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 21000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 22000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 23000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 0ms
String number: 24000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 25000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 26000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 27000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 28000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 15ms
String number: 29000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 30000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 0ms
String number: 31000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 32000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 0ms
String number: 33000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 0ms
String number: 34000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 35000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 0ms
String number: 36000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 37000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 0ms
String number: 38000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 39000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 40000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 41000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 42000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 43000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 44000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 45000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 46000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 31ms
String number: 47000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 15ms
String number: 48000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 49000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 0ms
String number: 50000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 51000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 31ms
String number: 52000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 53000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 54000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 16ms
String number: 55000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 62ms
String number: 56000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 63ms
String number: 57000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 63ms
String number: 58000
StringBuffer Takes Time: 32ms
StringBuilder Takes Time: 62ms
String number: 59000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 47ms
String number: 60000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 47ms
String number: 61000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 63ms
String number: 62000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 78ms
String number: 63000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 78ms
String number: 64000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 94ms
String number: 65000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 62ms
String number: 66000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 78ms
String number: 67000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 78ms
String number: 68000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 78ms
String number: 69000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 78ms
String number: 70000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 78ms
String number: 71000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 78ms
String number: 72000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 78ms
String number: 73000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 62ms
String number: 74000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 78ms
String number: 75000
StringBuffer Takes Time: 15ms
StringBuilder Takes Time: 78ms
String number: 76000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 78ms
String number: 77000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 62ms
String number: 78000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 62ms
String number: 79000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 63ms
String number: 80000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 47ms
String number: 81000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 63ms
String number: 82000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 78ms
String number: 83000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 78ms
String number: 84000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 63ms
String number: 85000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 62ms
String number: 86000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 62ms
String number: 87000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 78ms
String number: 88000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 62ms
String number: 89000
StringBuffer Takes Time: 46ms
StringBuilder Takes Time: 79ms
String number: 90000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 15ms
String number: 91000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 15ms
String number: 92000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 15ms
String number: 93000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 0ms
String number: 94000
StringBuffer Takes Time: 93ms
StringBuilder Takes Time: 16ms
String number: 95000
StringBuffer Takes Time: 110ms
StringBuilder Takes Time: 0ms
String number: 96000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 0ms
String number: 97000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 15ms
String number: 98000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 16ms
String number: 99000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 15ms
String number: 100000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 31ms
测试结果概述:
一。对于使用了-Server参数的测试,我们可以看出以下结果:
1。在35000以前,结果是不确定的,也就是说,交替出现了某方为0的结果。
2。从35000到69000,基本是交替领先,领先次数基本是平局。
3。从70000到100000,StringBuffer领先于StringBuilder的比例是22:8。
结论,在这样的测试环境下,StringBuffer的总体表现要比StringBuilder强!
二。对于使用了-Client参数的测试,我们可以看出以下结果:
1。在50000以前,结果是不确定的,也就是说,交替出现了某方为0的结果。
2。从50000到89000,StringBuffer领先于StringBuilder的比例是1:36
3。从90000到100000,StringBuilder领先于StringBuffer的比例是11:0。
结论:本组测试的101组数据中,前五十组可以认为比较失真,后面的五十组StringBuilder领先于StringBuffer的比例是12:36,而且是只有最后的十一组才体现出StringBuilder的优势,因此我们可以得出结论:只有在非常大的字符串数组情况下,StringBuilder的表现才领先于StringBuffer,否则并不占优势。
注:以上测试是在一台运行Windows XP Pro+SP2, 512M内存,Java 1.5,JBuilder 2005 Enterprise的机器上得出的。
- 相关回复 上下关系8
😄老兵肯定有积蓄 ppw 字22 2004-10-06 14:07:09
😅String, StringBuffer &StringBuilder的玄妙 Highway 字7702 2004-10-04 08:33:08
我对.Net2.0 Beta1下StringBuilder的测试结果 老兵帅客 字7255 2004-10-05 10:17:26
我对Java StringBuffer/StringBuilder的测试结果
其实这种测试波动很大。因为我们没有办法控制GC. Highway 字639 2004-10-05 10:24:24
理论上的确如此 老兵帅客 字282 2004-10-05 10:40:33
😉看看我的测试结果! Highway 字8579 2004-10-05 11:07:49
😉你试着再加个-Xms256M,看看结果如何? 老兵帅客 字808 2004-10-05 12:03:36