主题:【倡议】Java SE 5.0和.NET 2.0擂台赛 -- Highway
首先,我对老轧的程序进行了一些简单的修改,主要如下:
一。取消了对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的机器上得出的。
- 相关回复 上下关系8
😉如果有人出钱,我一定会有积极性。 Highway 字32 2004-10-06 12:05:47
😄老兵肯定有积蓄 ppw 字22 2004-10-06 14:07:09
😅String, StringBuffer &StringBuilder的玄妙 Highway 字7702 2004-10-04 08:33:08
我对.Net2.0 Beta1下StringBuilder的测试结果
我对Java StringBuffer/StringBuilder的测试结果 老兵帅客 字21245 2004-10-05 09:41:54
其实这种测试波动很大。因为我们没有办法控制GC. Highway 字639 2004-10-05 10:24:24