C# ile uygulama yazarken kodunuzda yapacağınız küçük değişiklikler uygulama performansında ciddi artışlara sebep olacaktır. Aşağıda hızlıca yapılabilecek performans iyileştirmeleri için 5 ipucu bulunmakta. Bunlar kolaylıkla Vb.Net diline de uyarlanabilir.



1-Doğru veri türünü (Data Type) seçin

Yanlış veri türünü seçmek uygulamanızın performansını düşürür.
Örneğin List jenerik türü ile int [ ] integer dizisini ele alırsak, aşağıdaki kod aradaki performans farkını gösterecektir.


Kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace tht
{
class Program
{
static v oid Main(string[] args)
{
List SayiListesi = new List();
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 10000; i++)
{
SayiListesi.Add(i);
}
sw.Stop();
Console.WriteLine("Generic List Sure:" + sw.ElapsedTicks);
sw.Reset();
int[] SayiDizisi = new int[10000];
sw.Start();
for (int i = 0; i < 10000; i++)
{
SayiDizisi[i] = i;
}
sw.Stop();
Console.WriteLine("int Array Sure:" + sw.ElapsedTicks);
Console.ReadLine();
}
}
}
Alıntı:
Yukarıdaki kodun çıktısı:
Generic List Sure: 399
int Array Sure: 112

<font color="olive">Görüldüğü gibi int [ ] yani integer dizisi List jenerik türüne göre 3.5 daha hızlı.


2-Mümkünse foreach yerine for kullanın.

for döngüsü foreach'e göre daha hızlıdır.

Kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace tht
{
class Program
{
static v oid Main(string[] args)
{
List Limit = new List();
List liste1 = new List();
List liste2 = new List();
for (int i = 0; i < 10000; i++)
{
Limit.Add(i);
}
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < Limit.Count; i++)
{
liste1.Add(i);
}
sw.Stop();
Console.Write("For Dongusu :- " + sw.ElapsedTicks + "\n");
sw.Restart();
foreach (int a in Limit)
{
liste2.Add(a);

}
sw.Stop();
Console.Write("Foreach Dongusu:- " + sw.ElapsedTicks);
Console.ReadLine();
}
}
}
Alıntı:
Yukarıdaki kodun çıktısı
For Döngüsü : 461
Foreach Döngüsü: 561
3-class ve struct doğru ve uygun şekilde kullanın.

struct değer tür (Value Type), class referans türdür. (Reference Type). Değer türler, referans türlere göre daha hızlıdır. Ne zaman hangisini kullanacağınızı bilin.

4-Büyük boyutlu metin, yazi, string işlemlerinde StringBuilder kullanın

StringBuilder türü string'e göre çok daha hızlıdır.

Kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace tht
{
class Program
{
static v oid Main(string[] args)
{
string First = "B";
StringBuilder sb = new StringBuilder("B");
Stopwatch st = new Stopwatch();
st.Start();
for (int i = 0; i < 1000; i++)
{
First = First + "B";

}
st.Stop();
Console.WriteLine("String Kullanarak :-" + st.ElapsedTicks);
st.Restart();
for (int i = 0; i < 1000; i++)
{
sb.Append("B");
}
st.Stop();
Console.WriteLine("Stringbuilder Kullanarak :-" + st.ElapsedTicks);
Console.ReadLine();

}

}

}
Alıntı:
Yukarıdaki kodun çıktısı
String Kullanarak : 1180
Stringbuilder Kullanarak : 31

5-Gerektiğinde sınıf (class) içinde Property yerine Field kullanın değer atamalarınızı doğrudan yapın.

Kod:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace tht
{
public class Test
{

public static string Ad { get; set; }
public static String _soyad;

}
class Program
{
static v oid Main(string[] args)
{
Stopwatch st = new Stopwatch();
st.Start();
for (int i = 0; i < 100; i++)
{
Test.Ad = "Value";
}
st.Stop();
Console.WriteLine("Property Kullanarak:" + st.ElapsedTicks);
st.Restart();
for (int i = 0; i < 100; i++)
{
Test._soyad = "Value";
}
st.Stop();
Console.WriteLine("Dogrudan Field'e atama: " + st.ElapsedTicks);
Console.ReadLine();
}
}

}
Alıntı:
Yukarıdaki kodun çıktısı şu olacaktır:
Property Kullanarak: 239
Dogrudan Field'e atama: 1