主题:【倡议】Java SE 5.0和.NET 2.0擂台赛 -- Highway
String可能是使用最广泛的一种操作了,尤其是在现在Web程序开发中。事实上,Java的String是非常特殊的一种Class,即所有的String Object都是Immutable的。为什么要这样对待String呢?因为Sun想尽可能的榨取一切可能的性能。
为了提高连续拼接String的性能,Java提供了一个类叫StringBuffer。使用它在某些情况下可以成千倍的提高性能。但是Sun还是不够满意,在Java 5.0中,它又提供了一个类叫做StringBuilder。
那么StringBuilder和StringBuffer有什么区别呢?StringBuffer是Thread-safe的,而StringBuilder不是。
那么什么是Thread-safe呢?还记得国内火车内的那种厕所吗?一个人进去,把门一锁,然后开始“方便”。其间不会被外人打扰。完事后开门出来,下一个人才能进去。不会有两个人同时一起“方便”的尴尬局面。这就是Thread-safe.显然Thread-safe有一定的开销,比如关门,上锁,开门等等。在有些情况下,我们不需要这么safe,比如在家里(如果只有你和你老婆),或者你是单身汉,家里根本就没外人。在这种情况下,不用Thread-safe可能会更方便一些,效率会更高。
StringBuilder就是这样的设计思想。在一切可能的情况下使用StringBuilder,最大限度的提高性能。
大家看看下面这段小程序,就会有些感性认识了。
源程序
...... String[] strs = null; strs = this.prepareString(5000); this.stringBufferTest(strs); this.stringBuilderTest(strs); this.stringTest(strs); strs = this.prepareString(20000); this.stringBufferTest(strs); this.stringBuilderTest(strs); //this.stringTest(strs); strs = this.prepareString(100000); this.stringBufferTest(strs); this.stringBuilderTest(strs); //this.stringTest(strs); ...... 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"); return; } 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"); return; } private void stringTest(String[] input){ long start = System.currentTimeMillis(); String sb = ""; for(int i=0; i<input.length; i++) sb = sb + input[i]; long time = System.currentTimeMillis()-start; System.out.println("String Append Takes Time: " + time+ "ms"); return; }
反编译得到的程序,这是Java实际工作的Code.
private String[] prepareString(int number) { System.out.println((new StringBuilder()).append("String number: ").append(number).toString()); String strs[] = new String[number]; for(int i = 0; i < number; i++) strs[i] = (new StringBuilder()).append("Highway").append(i).toString(); 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((new StringBuilder()).append("StringBuilder Takes Time: ").append(time).append("ms").toString()); } 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((new StringBuilder()).append("StringBuffer Takes Time: ").append(time).append("ms").toString()); } private void stringTest(String input[]) { long start = System.currentTimeMillis(); String sb = ""; for(int i = 0; i < input.length; i++) sb = (new StringBuilder()).append(sb).append(input[i]).toString(); long time = System.currentTimeMillis() - start; System.out.println((new StringBuilder()).append("String Append Takes Time: ").append(time).append("ms").toString()); }
由上可见,Java在内部尽可能的在使用StringBuilder
[SIZE=3]测试结果:[/SIZE]
String number: 5000 StringBuffer Takes Time: 15ms StringBuilder Takes Time: 0ms String Append Takes Time: 7281ms String number: 20000 StringBuffer Takes Time: 31ms StringBuilder Takes Time: 16ms String number: 100000 StringBuffer Takes Time: 188ms StringBuilder Takes Time: 156ms
由此可见,StringBuilder 确实是有效果的。最慢的是String直接拼接,效果满上千倍,这是大家应该避免的情况。
那么.NET如何呢?.NET只有一个StringBuilder,没有StringBuffer。这个StringBuilder的Public static变量是Thread-safe的,而instance members却不是。哈哈,有意思吧!!!
将上面Java的程序用.NET写一遍,其运行结果比Java快很多,尤其是赤裸裸的SString拼接(20倍)。这是一个很有趣的问题。希望大家能帮我解释一下为什么!!!
String number: 5000 StringBuilder Takes Time: 0ms String Append Takes Time: 422ms String number: 20000 StringBuilder Takes Time: 15ms String number: 100000 StringBuilder Takes Time: 32ms
C#源程序为:
private String[] prepareString(int number) { System.Console.WriteLine("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.Environment.TickCount; StringBuilder sb = new StringBuilder(); for(int i=0; i<input.Length; i++) sb.Append(input[i]); long time = System.Environment.TickCount - start; System.Console.WriteLine("StringBuilder Takes Time: " + time + "ms"); return; } private void stringTest(String[] input) { long start = System.Environment.TickCount; String sb = ""; for(int i=0; i<input.Length; i++) sb = sb + input[i]; long time = System.Environment.TickCount - start; System.Console.WriteLine("String Append Takes Time: " + time + "ms"); return; }
反编译后为:
private void stringBuilderTest(string[] input) { long num1 = Environment.TickCount; StringBuilder builder1 = new StringBuilder(); for (int num2 = 0; num2 < input.Length; num2++) { builder1.Append(input[num2]); } long num3 = Environment.TickCount - num1; Console.WriteLine("StringBuilder Takes Time: " + num3 + "ms"); } private void stringTest(string[] input) { long num1 = Environment.TickCount; string text1 = ""; for (int num2 = 0; num2 < input.Length; num2++) { text1 = text1 + input[num2]; } long num3 = Environment.TickCount - num1; Console.WriteLine("String Append Takes Time: " + num3 + "ms"); }
请耐心等待。
我的那个话题目前也只能够暂停了。
首先,我对老轧的程序进行了一些简单的修改,主要如下:
一。取消了对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的机器上得出的。
首先,我对老轧的程序进行了一些简单的修改,主要如下:
一。取消了对String的测试部分,而只测试StringBuilder,因为String的性能明显落后,测试的意义不大。
二。测试范围从1000到100,000,步长为1000,这样可以看出一些比较细的东西。
下面是我的测试代码:
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace StringTest
{
class Program
{
private String[] prepareString(int number)
{
System.Console.WriteLine("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.Environment.TickCount;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
sb.Append(input[i]);
long time = System.Environment.TickCount - start;
System.Console.WriteLine("StringBuilder Takes Time: " + time + "ms");
return;
}
public void test()
{
String[] strs = null;
for (int i = 1; i <= 100; i++)
{
strs = this.prepareString(i * 1000);
this.stringBuilderTest(strs);
}
}
static void Main(string[] args)
{
Program p = new Program();
p.test();
}
}
}
在将Build结果从Debug设定为Release并且设定目标CPU为x86以后,我得到以下的测试结果:
String number: 1000
StringBuilder Takes Time: 0ms
String number: 2000
StringBuilder Takes Time: 0ms
String number: 3000
StringBuilder Takes Time: 0ms
String number: 4000
StringBuilder Takes Time: 0ms
String number: 5000
StringBuilder Takes Time: 0ms
String number: 6000
StringBuilder Takes Time: 0ms
String number: 7000
StringBuilder Takes Time: 0ms
String number: 8000
StringBuilder Takes Time: 0ms
String number: 9000
StringBuilder Takes Time: 16ms
String number: 10000
StringBuilder Takes Time: 15ms
String number: 11000
StringBuilder Takes Time: 0ms
String number: 12000
StringBuilder Takes Time: 0ms
String number: 13000
StringBuilder Takes Time: 15ms
String number: 14000
StringBuilder Takes Time: 16ms
String number: 15000
StringBuilder Takes Time: 0ms
String number: 16000
StringBuilder Takes Time: 16ms
String number: 17000
StringBuilder Takes Time: 15ms
String number: 18000
StringBuilder Takes Time: 16ms
String number: 19000
StringBuilder Takes Time: 0ms
String number: 20000
StringBuilder Takes Time: 16ms
String number: 21000
StringBuilder Takes Time: 16ms
String number: 22000
StringBuilder Takes Time: 16ms
String number: 23000
StringBuilder Takes Time: 15ms
String number: 24000
StringBuilder Takes Time: 0ms
String number: 25000
StringBuilder Takes Time: 0ms
String number: 26000
StringBuilder Takes Time: 0ms
String number: 27000
StringBuilder Takes Time: 16ms
String number: 28000
StringBuilder Takes Time: 0ms
String number: 29000
StringBuilder Takes Time: 0ms
String number: 30000
StringBuilder Takes Time: 0ms
String number: 31000
StringBuilder Takes Time: 0ms
String number: 32000
StringBuilder Takes Time: 0ms
String number: 33000
StringBuilder Takes Time: 31ms
String number: 34000
StringBuilder Takes Time: 31ms
String number: 35000
StringBuilder Takes Time: 46ms
String number: 36000
StringBuilder Takes Time: 0ms
String number: 37000
StringBuilder Takes Time: 0ms
String number: 38000
StringBuilder Takes Time: 15ms
String number: 39000
StringBuilder Takes Time: 16ms
String number: 40000
StringBuilder Takes Time: 16ms
String number: 41000
StringBuilder Takes Time: 31ms
String number: 42000
StringBuilder Takes Time: 0ms
String number: 43000
StringBuilder Takes Time: 31ms
String number: 44000
StringBuilder Takes Time: 31ms
String number: 45000
StringBuilder Takes Time: 16ms
String number: 46000
StringBuilder Takes Time: 63ms
String number: 47000
StringBuilder Takes Time: 0ms
String number: 48000
StringBuilder Takes Time: 63ms
String number: 49000
StringBuilder Takes Time: 47ms
String number: 50000
StringBuilder Takes Time: 16ms
String number: 51000
StringBuilder Takes Time: 47ms
String number: 52000
StringBuilder Takes Time: 32ms
String number: 53000
StringBuilder Takes Time: 16ms
String number: 54000
StringBuilder Takes Time: 47ms
String number: 55000
StringBuilder Takes Time: 31ms
String number: 56000
StringBuilder Takes Time: 16ms
String number: 57000
StringBuilder Takes Time: 62ms
String number: 58000
StringBuilder Takes Time: 47ms
String number: 59000
StringBuilder Takes Time: 16ms
String number: 60000
StringBuilder Takes Time: 78ms
String number: 61000
StringBuilder Takes Time: 47ms
String number: 62000
StringBuilder Takes Time: 15ms
String number: 63000
StringBuilder Takes Time: 63ms
String number: 64000
StringBuilder Takes Time: 47ms
String number: 65000
StringBuilder Takes Time: 15ms
String number: 66000
StringBuilder Takes Time: 62ms
String number: 67000
StringBuilder Takes Time: 46ms
String number: 68000
StringBuilder Takes Time: 0ms
String number: 69000
StringBuilder Takes Time: 62ms
String number: 70000
StringBuilder Takes Time: 47ms
String number: 71000
StringBuilder Takes Time: 16ms
String number: 72000
StringBuilder Takes Time: 79ms
String number: 73000
StringBuilder Takes Time: 62ms
String number: 74000
StringBuilder Takes Time: 31ms
String number: 75000
StringBuilder Takes Time: 125ms
String number: 76000
StringBuilder Takes Time: 16ms
String number: 77000
StringBuilder Takes Time: 31ms
String number: 78000
StringBuilder Takes Time: 94ms
String number: 79000
StringBuilder Takes Time: 15ms
String number: 80000
StringBuilder Takes Time: 15ms
String number: 81000
StringBuilder Takes Time: 16ms
String number: 82000
StringBuilder Takes Time: 16ms
String number: 83000
StringBuilder Takes Time: 15ms
String number: 84000
StringBuilder Takes Time: 16ms
String number: 85000
StringBuilder Takes Time: 15ms
String number: 86000
StringBuilder Takes Time: 16ms
String number: 87000
StringBuilder Takes Time: 15ms
String number: 88000
StringBuilder Takes Time: 31ms
String number: 89000
StringBuilder Takes Time: 94ms
String number: 90000
StringBuilder Takes Time: 15ms
String number: 91000
StringBuilder Takes Time: 31ms
String number: 92000
StringBuilder Takes Time: 32ms
String number: 93000
StringBuilder Takes Time: 15ms
String number: 94000
StringBuilder Takes Time: 31ms
String number: 95000
StringBuilder Takes Time: 31ms
String number: 96000
StringBuilder Takes Time: 31ms
String number: 97000
StringBuilder Takes Time: 31ms
String number: 98000
StringBuilder Takes Time: 16ms
String number: 99000
StringBuilder Takes Time: 15ms
String number: 100000
StringBuilder Takes Time: 32ms
和我对Java的对应测试相比,测试结果基本等同于Java StringBuilder所对应的结果,不管它是在Server模式下还是Client模式。
注:以上测试结果是在一台运行Windows XP Pro+SP2, 512M内存,Visual Studio 2005 Beta1+.Net 2.0 Beta1的机器上得出的。
System.gc()对JVM来说是一个Hint,而不是强制性指令。所以到底什么JVM开始回收内存,是一个不确定的事情。一旦Background thread开始回收内存,前台的程序就会受到极大的影响。这是我们测试的一个难点。
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
理论上讲,Thread-safe的操作是有代价的。如果情况允许,我们尽可能避开这个开销!
但是我的测试结果并不支持这种看法,而且测试结果的优劣分布还是比较明显的,很多组数据的差距还是比较大的,而不是在临界的+-1。
单独的数据疵点可以认为是JVM作GC所导致的结果,但是这样的数据疵点不应该很多,以至于无法改变最后结果。
另外,我给的内存是256M,对于这样的测试应该是足够的了。
StringBuiler几乎是一路领先(低端有些交替)!
java -server -Xmx256M StringTest >perf2.txt
String number: 1000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 0ms
String number: 2000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 16ms
String number: 3000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 15ms
String number: 4000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 0ms
String number: 5000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 15ms
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: 16ms
String number: 10000
StringBuffer Takes Time: 0ms
StringBuilder Takes Time: 15ms
String number: 11000
StringBuffer Takes Time: 15ms
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: 16ms
StringBuilder Takes Time: 15ms
String number: 15000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 16000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 16ms
String number: 17000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 18000
StringBuffer Takes Time: 16ms
StringBuilder Takes Time: 15ms
String number: 19000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 15ms
String number: 20000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 16ms
String number: 21000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 16ms
String number: 22000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 16ms
String number: 23000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 16ms
String number: 24000
StringBuffer Takes Time: 32ms
StringBuilder Takes Time: 15ms
String number: 25000
StringBuffer Takes Time: 32ms
StringBuilder Takes Time: 15ms
String number: 26000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 31ms
String number: 27000
StringBuffer Takes Time: 32ms
StringBuilder Takes Time: 46ms
String number: 28000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 31ms
String number: 29000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 32ms
String number: 30000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 31ms
String number: 31000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 31ms
String number: 32000
StringBuffer Takes Time: 46ms
StringBuilder Takes Time: 32ms
String number: 33000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 31ms
String number: 34000
StringBuffer Takes Time: 46ms
StringBuilder Takes Time: 47ms
String number: 35000
StringBuffer Takes Time: 46ms
StringBuilder Takes Time: 32ms
String number: 36000
StringBuffer Takes Time: 46ms
StringBuilder Takes Time: 47ms
String number: 37000
StringBuffer Takes Time: 46ms
StringBuilder Takes Time: 47ms
String number: 38000
StringBuffer Takes Time: 46ms
StringBuilder Takes Time: 47ms
String number: 39000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 47ms
String number: 40000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 47ms
String number: 41000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 16ms
String number: 42000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 31ms
String number: 43000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 16ms
String number: 44000
StringBuffer Takes Time: 31ms
StringBuilder Takes Time: 31ms
String number: 45000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 31ms
String number: 46000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 31ms
String number: 47000
StringBuffer Takes Time: 62ms
StringBuilder Takes Time: 16ms
String number: 48000
StringBuffer Takes Time: 62ms
StringBuilder Takes Time: 32ms
String number: 49000
StringBuffer Takes Time: 46ms
StringBuilder Takes Time: 32ms
String number: 50000
StringBuffer Takes Time: 47ms
StringBuilder Takes Time: 31ms
String number: 51000
StringBuffer Takes Time: 62ms
StringBuilder Takes Time: 63ms
String number: 52000
StringBuffer Takes Time: 62ms
StringBuilder Takes Time: 63ms
String number: 53000
StringBuffer Takes Time: 63ms
StringBuilder Takes Time: 47ms
String number: 54000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 47ms
String number: 55000
StringBuffer Takes Time: 62ms
StringBuilder Takes Time: 63ms
String number: 56000
StringBuffer Takes Time: 63ms
StringBuilder Takes Time: 62ms
String number: 57000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 47ms
String number: 58000
StringBuffer Takes Time: 79ms
StringBuilder Takes Time: 62ms
String number: 59000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 63ms
String number: 60000
StringBuffer Takes Time: 63ms
StringBuilder Takes Time: 62ms
String number: 61000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 62ms
String number: 62000
StringBuffer Takes Time: 62ms
StringBuilder Takes Time: 63ms
String number: 63000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 63ms
String number: 64000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 63ms
String number: 65000
StringBuffer Takes Time: 79ms
StringBuilder Takes Time: 62ms
String number: 66000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 62ms
String number: 67000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 62ms
String number: 68000
StringBuffer Takes Time: 79ms
StringBuilder Takes Time: 62ms
String number: 69000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 78ms
String number: 70000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 63ms
String number: 71000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 63ms
String number: 72000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 62ms
String number: 73000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 62ms
String number: 74000
StringBuffer Takes Time: 78ms
StringBuilder Takes Time: 79ms
String number: 75000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 62ms
String number: 76000
StringBuffer Takes Time: 79ms
StringBuilder Takes Time: 78ms
String number: 77000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 62ms
String number: 78000
StringBuffer Takes Time: 93ms
StringBuilder Takes Time: 63ms
String number: 79000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 62ms
String number: 80000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 63ms
String number: 81000
StringBuffer Takes Time: 125ms
StringBuilder Takes Time: 47ms
String number: 82000
StringBuffer Takes Time: 93ms
StringBuilder Takes Time: 47ms
String number: 83000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 47ms
String number: 84000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 47ms
String number: 85000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 47ms
String number: 86000
StringBuffer Takes Time: 93ms
StringBuilder Takes Time: 47ms
String number: 87000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 47ms
String number: 88000
StringBuffer Takes Time: 62ms
StringBuilder Takes Time: 47ms
String number: 89000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 47ms
String number: 90000
StringBuffer Takes Time: 62ms
StringBuilder Takes Time: 63ms
String number: 91000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 46ms
String number: 92000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 47ms
String number: 93000
StringBuffer Takes Time: 93ms
StringBuilder Takes Time: 47ms
String number: 94000
StringBuffer Takes Time: 94ms
StringBuilder Takes Time: 47ms
String number: 95000
StringBuffer Takes Time: 110ms
StringBuilder Takes Time: 47ms
String number: 96000
StringBuffer Takes Time: 109ms
StringBuilder Takes Time: 47ms
String number: 97000
StringBuffer Takes Time: 93ms
StringBuilder Takes Time: 63ms
String number: 98000
StringBuffer Takes Time: 109ms
StringBuilder Takes Time: 47ms
String number: 99000
StringBuffer Takes Time: 109ms
StringBuilder Takes Time: 47ms
String number: 100000
StringBuffer Takes Time: 141ms
StringBuilder Takes Time: 93ms
那就是我的测试结果了。
问题就出在你只限定了最大可用内存而没有限定最小可用内存上面了,缺省的设置是64M,这经常是不够的。在这样的情况下,应用程序迫使JVM多次调用GC和增加可用内存,这样会影响比较结果的。
我的测试方案是首先给足内存(至少是尽可能多的内存,以求减少调用GC和内存膨胀的次数),这样才可以比较真实地进行比较。
Unless you have problems with pauses, try granting as much memory as possible to the virtual machine. The default size (64MB) is often too small.
Setting -Xms and -Xmx to the same value increases predictability by removing the most important sizing decision from the virtual machine. On the other hand, the virtual machine can't compensate if you make a poor choice.
Be sure to increase the memory as you increase the number of processors, since allocation can be parallelized
没有一个一直领先。但大多情况下,StringBuilder要好一些,或者好很多。
java -server -Xms64M -Xmx512M StringTest
StringBuffer Takes Total Time: 1763ms
StringBuilder Takes Total Time: 1127ms
java -server -Xms128M -Xmx256M StringTest
StringBuffer Takes Total Time: 2095ms
StringBuilder Takes Total Time: 952ms
java -server -Xms128M -Xmx512M StringTest
StringBuffer Takes Total Time: 2108ms
StringBuilder Takes Total Time: 888ms
java -server -Xms256M -Xmx512M StringTest
StringBuffer Takes Total Time: 1097ms
StringBuilder Takes Total Time: 1107ms
java -server -Xms512M -Xmx512M StringTest
StringBuffer Takes Total Time: 1235ms
StringBuilder Takes Total Time: 1079ms
缺省下,StringBuilder性能好些。
-Server的连续三次的结果为(总时间):
StringBuffer Takes Total Time: 5857ms
StringBuilder Takes Total Time: 3984ms
StringBuffer Takes Total Time: 5664ms
StringBuilder Takes Total Time: 3996ms
StringBuffer Takes Total Time: 5772ms
StringBuilder Takes Total Time: 3992ms
-Client的连续三次的结果为(总时间):
StringBuffer Takes Total Time: 7748ms
StringBuilder Takes Total Time: 6266ms
StringBuffer Takes Total Time: 7845ms
StringBuilder Takes Total Time: 6425ms
StringBuffer Takes Total Time: 7906ms
StringBuilder Takes Total Time: 6307ms
有没兴趣我们搞一个soft 的benchmark 网站?
software 版本的 tomshardware.
你有什么别人没有的特色吗?
你能commit多少时间?我们搞这样的网站目的何在?
其实只是想论坛的方式总有它的局限性, 即使老铁
在努力, 有些东西是没法改变的.
需要多种方式互补. 比如这样的专业讨论我觉得用wiki的方式就比较好,
比如你和老兵, 可以共同修改一个文章, 别人也可以修改或者
加注. 有兴趣就写, 没兴趣就不写.
否则,还是看看球赛,混混日子吧!
而且很有事业心的样子..