为HttpResponse准备输出数据,ArrayList和StringBuilder性能到底差多少?
Posted on 2004-09-29 16:38 红移 阅读(2656) 评论(7) 编辑 收藏 举报
在向HttpResponse/HttpTextWriter输出结果之前,一直用ArrayList保存数据。不用StringBuilder的原因是认为ArrayList一定会比StringBuilder快许多。今天踏踏实实地做了一下测试,发现其实StringBuilder并没有想象中的那么慢。看来还是要脚踏实地才行。
合并3000个长度为1000的随机字符串,结果如下(毫秒):
ArrayList: 0
Builder: 31.194
String: 28667.286
看来StringBuilder并没有差到哪里去,当然,ArrayList还是最好的,如果想用string,那么我就不好说什么了。(这个问题好像TechEd上讲过,不过没讲ArrayList)
这几天正在精心设计我的Dev365,做了N多的性能测试。。。。。
代码如下:
合并3000个长度为1000的随机字符串,结果如下(毫秒):
ArrayList: 0
Builder: 31.194
String: 28667.286
看来StringBuilder并没有差到哪里去,当然,ArrayList还是最好的,如果想用string,那么我就不好说什么了。(这个问题好像TechEd上讲过,不过没讲ArrayList)
这几天正在精心设计我的Dev365,做了N多的性能测试。。。。。
代码如下:
using System;
using System.Text;
using System.Collections;
namespace CombinString
{
class Class1
{
private static readonly byte memChar_A, memChar_a, memChar_0;
static Class1()
{
char[] tempSrc = new char[1];
tempSrc[0] = 'A';
memChar_A = Encoding.ASCII.GetBytes(tempSrc)[0];
tempSrc[0] = 'a';
memChar_a = Encoding.ASCII.GetBytes(tempSrc)[0];
tempSrc[0] = '0';
memChar_0 = Encoding.ASCII.GetBytes(tempSrc)[0];
}
[STAThread]
static void Main(string[] args)
{
string tempInitStr = BuildString(int.Parse(args[0]));
int tempTimes = int.Parse(args[1]);
Console.WriteLine("ArrayList: {0}",ArrayListTest(tempInitStr,tempTimes).TotalMilliseconds);
Console.WriteLine("Builder: {0}",StringBuilderTest(tempInitStr,tempTimes).TotalMilliseconds);
Console.WriteLine("String: {0}",StringTest(tempInitStr,tempTimes).TotalMilliseconds);
}
static private string BuildString(int paraLength)
{
byte[] memData = new byte[paraLength];
byte memCharData;
Random tempRandom = new Random();
do
{
byte tempCharCode = (byte)tempRandom.Next(26*2+10);
if (tempCharCode<26)
memCharData = (byte)(tempCharCode + memChar_A);
else if (tempCharCode < 26*2)
memCharData = (byte)(tempCharCode - 26 + memChar_a);
else
memCharData = (byte)(tempCharCode -26*2+ memChar_0);
memData[--paraLength] = memCharData;
}while(paraLength>0);
return Encoding.ASCII.GetString(memData);
}
static private TimeSpan StringTest(String paraSrc, int paraTimes)
{
DateTime tempStart;
String tempResult = null;
tempStart = DateTime.Now;
for(;paraTimes>0;paraTimes--)
tempResult += paraSrc;
return DateTime.Now - tempStart;
}
static private TimeSpan StringBuilderTest(String paraSrc, int paraTimes)
{
DateTime tempStart;
StringBuilder tempResult = new StringBuilder();
tempStart = DateTime.Now;
for(;paraTimes>0;paraTimes--)
tempResult.Append(paraSrc);
String tempRet = tempResult.ToString();
return DateTime.Now - tempStart;
}
static private TimeSpan ArrayListTest(String paraSrc, int paraTimes)
{
DateTime tempStart;
ArrayList tempResult = new ArrayList();
tempStart = DateTime.Now;
for(;paraTimes>0;paraTimes--)
tempResult.Add(paraSrc);
string tempRet;
foreach(string tempStr in tempResult)
tempRet = tempStr;
return DateTime.Now - tempStart;
}
}
}
using System.Text;
using System.Collections;
namespace CombinString
{
class Class1
{
private static readonly byte memChar_A, memChar_a, memChar_0;
static Class1()
{
char[] tempSrc = new char[1];
tempSrc[0] = 'A';
memChar_A = Encoding.ASCII.GetBytes(tempSrc)[0];
tempSrc[0] = 'a';
memChar_a = Encoding.ASCII.GetBytes(tempSrc)[0];
tempSrc[0] = '0';
memChar_0 = Encoding.ASCII.GetBytes(tempSrc)[0];
}
[STAThread]
static void Main(string[] args)
{
string tempInitStr = BuildString(int.Parse(args[0]));
int tempTimes = int.Parse(args[1]);
Console.WriteLine("ArrayList: {0}",ArrayListTest(tempInitStr,tempTimes).TotalMilliseconds);
Console.WriteLine("Builder: {0}",StringBuilderTest(tempInitStr,tempTimes).TotalMilliseconds);
Console.WriteLine("String: {0}",StringTest(tempInitStr,tempTimes).TotalMilliseconds);
}
static private string BuildString(int paraLength)
{
byte[] memData = new byte[paraLength];
byte memCharData;
Random tempRandom = new Random();
do
{
byte tempCharCode = (byte)tempRandom.Next(26*2+10);
if (tempCharCode<26)
memCharData = (byte)(tempCharCode + memChar_A);
else if (tempCharCode < 26*2)
memCharData = (byte)(tempCharCode - 26 + memChar_a);
else
memCharData = (byte)(tempCharCode -26*2+ memChar_0);
memData[--paraLength] = memCharData;
}while(paraLength>0);
return Encoding.ASCII.GetString(memData);
}
static private TimeSpan StringTest(String paraSrc, int paraTimes)
{
DateTime tempStart;
String tempResult = null;
tempStart = DateTime.Now;
for(;paraTimes>0;paraTimes--)
tempResult += paraSrc;
return DateTime.Now - tempStart;
}
static private TimeSpan StringBuilderTest(String paraSrc, int paraTimes)
{
DateTime tempStart;
StringBuilder tempResult = new StringBuilder();
tempStart = DateTime.Now;
for(;paraTimes>0;paraTimes--)
tempResult.Append(paraSrc);
String tempRet = tempResult.ToString();
return DateTime.Now - tempStart;
}
static private TimeSpan ArrayListTest(String paraSrc, int paraTimes)
{
DateTime tempStart;
ArrayList tempResult = new ArrayList();
tempStart = DateTime.Now;
for(;paraTimes>0;paraTimes--)
tempResult.Add(paraSrc);
string tempRet;
foreach(string tempStr in tempResult)
tempRet = tempStr;
return DateTime.Now - tempStart;
}
}
}