Managed Code Performance
Today I read a very good article from a guy who works for Microsoft BCL Performance section. I found some good interesting concepts. If your interested about code performance you should read this article.
http://blogs.msdn.com/ricom/
I wrote a very simple performance test for testing the for vs foreach & string vs stringbuilder ..etc..etc.. you can find the code below.
Timer class: This calculates the code execution time.
///
/// To get the elapsed the time
///
public class MyTimer
{
long _start;
long _end ;
long _exeTime ;
public string ElapsedTime
{
get
{
//returns the Elapsed time
_exeTime = _end - _start;
return _exeTime.ToString();
}
}
///
/// Start the timer
///
public void Start()
{
_start = DateTime.Now.Ticks ;
}
///
/// End the timer
///
public void Stop()
{
_end= DateTime.Now.Ticks ;
}
}
Some of my test samples
static void Main(string[] args)
{
MyTimer m = new MyTimer();
ArrayList a = new ArrayList();
//For Tests
m.Start();
for(int i=0; i<1000;>
{
string sss= "as" + i.ToString();
Console.WriteLine(sss);
a.Add(sss);
}
m.Stop();
Console.WriteLine(m.ElapsedTime);
m.Start();
for(int i=0; i
{
Console.WriteLine(a[i].ToString());
}
m.Stop();
Console.WriteLine(m.ElapsedTime);
//Foreach Tests
m.Start();
foreach(string s in a)
{
Console.WriteLine(s);
}
m.Stop();
Console.WriteLine(m.ElapsedTime);
//String BuilderTest
StringBuilder name = new StringBuilder();
m.Start();
for(int i=0;i<100000;>
{
name.Append(i.ToString());
}
m.Stop();
Console.WriteLine(name.ToString());
Console.WriteLine(m.ElapsedTime);
}