R
raylopez99
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();
}
}
}
/////////////////////
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();
}
}
}
/////////////////////