StringBuilder比String forconcatenation更快更好! [英] StringBuilder much much faster and better than String forconcatenation !!!
问题描述
StringBuilder比字符串更好更快,可以添加很多字符串。
请看下面的内容。令人惊讶的是,StringBuilder的速度比
字符串快得多。
下面的最后一个循环告诉:添加200000个8字符串
每个
,字符串占用超过25分钟,而StringBuilder占用40
毫秒!
任何人都可以解释这种根本性的差异吗?
运行该程序的硬件是带有2 GB RAM的Pentium IV。
RL
// stringbuilder连接比字符串快得多
//////////////
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
命名空间console1
{
class program
{
static void Main(string [] args)
{
Console.WriteLine(" hi \ n");
UpdateTime myUpdateTime = new UpdateTime(1000);
myUpdateTime。 UpdateTimeMethod();
Console.WriteLine(& ;时间str,sb是:{0},{1}",
myUpdateTime.txtConcatTime,myUpdateTime.txtStringBTime);
}
}
}
/ *
*输出
*结果:
* 1000次迭代:string = 10.01ms; stringbuilder = 0
* for 5000 iterations:string = 410.6ms; stringbuilder = 0
* for 50k iterations:sring = 79013 ms; stringbuilder = 0;
* for 10k iterations:string = 1772.5 ms; stringbuilder = 0;
*用于75k次迭代:string = 186237.8ms; stringbuilder = 20.03
ms
* for 100k iterations:string = 334.4k ms(5.6 min); stringbuilder =
20.03 ms;
* for 200k iterations:string = 1515.6k ms(25.3 min); stringbuilder
= 40.06 ms;
*
*
* * /
//////////////////////////////
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
命名空间console1
{
class UpdateTime
{
int txtInterations;
public string txtConcatTime;
public string txtStringBTime;
public UpdateTime(int i)
{
txtInterations = i;
txtConcatTime ="" ;;
txtStringBTime ="" ;;
}
public void UpdateTimeMethod()
{
int iterations = txtInterations;
string theString =" MyString" ;;
DateTime strCall = DateTime.Now;
string targetString = null;
for(int x = 0 ; x< iterations; x ++)
{
targetString + = the字符串;
}
TimeSpan时间=(DateTime.Now - strCall);
txtConcatTime =时间。 TotalMilliseconds.ToString();
// StringBuilder
DateTime inCall = DateTime.Now;
string theString2 =" MyStrig2";
StringBuilder sb = new StringBuilder(theString2);
for(int x = 0; x<迭代; x ++)
{
sb.Append(theString2);
}
time =( DateTime.Now - inCall);
txtStringBTime = time.TotalMilliseconds.ToString();
}
}
}
/////////////////////
我注意到了这一点,并且不得不在奇怪的地方使用它,
通常在运行探查器之后,看看它需要它的位置。当你将两个字符串加在一起时,
基本上是相当有用的工作,
作为你每次创建一个新对象,stringbuilder是一个单独的对象,
所以并没有任何接近相同开销的地方。
科林。
" raylopez99" < ra ******** @ yahoo.comwrote in message
news:46 ********************** ************ @ m45g2000 hsb.googlegroups.com ...
StringBuilder比字符串更好,更快,可以添加许多字符串。
请看下面的内容。令人惊讶的是,StringBuilder的速度比
字符串快得多。
下面的最后一个循环告诉:添加200000个8字符串
每个
,字符串占用超过25分钟,而StringBuilder占用40
毫秒!
任何人都可以解释这种根本性的差异吗?
运行此程序的硬件是Pentium IV,内存为2 GB。
RL
9月22日,8:36 * am,raylopez99< raylope ... @ yahoo.comwrote:
StringBuilder更好更快比添加许多字符串的字符串。
请看下面的内容。 *令人惊讶的是,StringBuilder的速度比
字符串快得多。
下面的最后一个循环告诉:*添加200000字符串的8个字符
每个,字符串占用超过25分钟而StringBuilder占用40
毫秒!
任何人都可以解释这种根本性的差异吗? />
运行此程序的硬件是带有2 GB RAM的Pentium IV。
RL
请参阅以下文章。
http://www.yoda.arachsys.com/csharp/stringbuilder.html
raylopez99< ra ****** **@yahoo.comwrote:
StringBuilder比字符串更好更快,可以添加很多字符串。
请看下面的内容。令人惊讶的是,StringBuilder的速度比
字符串快得多。
下面的最后一个循环告诉:添加200000个8字符串
每个
,字符串占用超过25分钟,而StringBuilder占用40
毫秒!
有人可以解释这种根本的区别吗?
很容易。这与创建副本有关。这个区别是首先存在于StringBuilder的整个点。
参见 http://pobox.com/~skeet/csharp/stringbuilder.html
-
Jon Skeet - < sk *** @ pobox.com>
网站: http://www.pobox.com/~skeet
博客: http://www.msmvps.com/jon.skeet
C#深度:< a rel =nofollowhref =http://csharpindepth.comtarget =_ blank> http://csharpindepth.com
StringBuilder better and faster than string for adding many strings.
Look at the below. It''s amazing how much faster StringBuilder is than
string.
The last loop below is telling: for adding 200000 strings of 8 char
each, string took over 25 minutes while StringBuilder took 40
milliseconds!
Can anybody explain such a radical difference?
The hardware running this program was a Pentium IV with 2 GB RAM.
RL
// stringbuilder much faster than string in concatenation
//////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace console1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hi \n");
UpdateTime myUpdateTime = new UpdateTime(1000);
myUpdateTime.UpdateTimeMethod();
Console.WriteLine("times str,sb are: {0}, {1}",
myUpdateTime.txtConcatTime, myUpdateTime.txtStringBTime);
}
}
}
/*
* OUTPUT
* results:
* for 1000 iterations: string = 10.01ms; stringbuilder = 0
* for 5000 iterations: string = 410.6ms; stringbuilder = 0
* for 50k iterations: sring = 79013 ms; stringbuilder = 0;
* for 10k iterations : string = 1772.5 ms; stringbuilder = 0;
* for 75k iterations : string = 186237.8ms; stringbuilder = 20.03
ms
* for 100k iterations : string = 334.4k ms (5.6 min); stringbuilder =
20.03 ms;
* for 200k iterations: string = 1515.6k ms (25.3 min); stringbuilder
= 40.06 ms;
*
*
* */
//////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace console1
{
class UpdateTime
{
int txtInterations;
public string txtConcatTime;
public string txtStringBTime;
public UpdateTime(int i)
{
txtInterations = i;
txtConcatTime = "";
txtStringBTime = "";
}
public void UpdateTimeMethod()
{
int iterations = txtInterations;
string theString = "MyString";
DateTime strCall = DateTime.Now;
string targetString = null;
for (int x = 0; x < iterations; x++)
{
targetString += theString;
}
TimeSpan time = (DateTime.Now - strCall);
txtConcatTime = time.TotalMilliseconds.ToString();
//StringBuilder
DateTime inCall = DateTime.Now;
string theString2 = "MyStrig2";
StringBuilder sb = new StringBuilder(theString2);
for (int x = 0; x < iterations; x++)
{
sb.Append(theString2);
}
time = (DateTime.Now - inCall);
txtStringBTime = time.TotalMilliseconds.ToString();
}
}
}
/////////////////////
ive noticed this, and have had to use it in verious places,
usualy after ive run profiler, to see where its needed.
basically theres a fair bit of work when you add two strings together,
as your making a new object each time, stringbuilder is a single object,
and so doesnt have any where near the same overhead to apend.
Colin.
"raylopez99" <ra********@yahoo.comwrote in message
news:46**********************************@m45g2000 hsb.googlegroups.com...StringBuilder better and faster than string for adding many strings.
Look at the below. It''s amazing how much faster StringBuilder is than
string.
The last loop below is telling: for adding 200000 strings of 8 char
each, string took over 25 minutes while StringBuilder took 40
milliseconds!
Can anybody explain such a radical difference?
The hardware running this program was a Pentium IV with 2 GB RAM.
RL
On Sep 22, 8:36*am, raylopez99 <raylope...@yahoo.comwrote:StringBuilder better and faster than string for adding many strings.
Look at the below. *It''s amazing how much faster StringBuilder is than
string.
The last loop below is telling: *for adding 200000 strings of 8 char
each, string took over 25 minutes while StringBuilder took 40
milliseconds!
Can anybody explain such a radical difference?
The hardware running this program was a Pentium IV with 2 GB RAM.
RLSee the following article.
http://www.yoda.arachsys.com/csharp/stringbuilder.html
raylopez99 <ra********@yahoo.comwrote:StringBuilder better and faster than string for adding many strings.
Look at the below. It''s amazing how much faster StringBuilder is than
string.
The last loop below is telling: for adding 200000 strings of 8 char
each, string took over 25 minutes while StringBuilder took 40
milliseconds!
Can anybody explain such a radical difference?Very easily. It''s all to do with creating copies. This difference is
the whole point of StringBuilder existing in the first place.
See http://pobox.com/~skeet/csharp/stringbuilder.html
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
这篇关于StringBuilder比String forconcatenation更快更好!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!