C# kodunuzun performansını arttırmak için 5 ipucu

alattin (16999) 10 yıl önce sordu

Microsoft.Net ortamında C# ile uygulama geliştirirken uygulamanın hızlı çalışması için dikkat edilmesi gerekenler nelerdir?

C# ile uygulama geliştirirken uygulamanın performansını nasıl arttırırsınız?

Toplam 1 cevap


alattin (16999) 10 yıl önce cevapladı

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<int> jenerik türü ile int[] integer dizisini ele alırsak, aşağıdaki kod aradaki performans farkını gösterecektir.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    
    namespace uzmanimNet
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<int> SayiListesi = new List<int>();
                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();
            }
        }
    }
    Yukarıdaki kodun çıktısı
    Generic List Sure:399
    int Array Sure:112
    Görüldüğü gibi int[] yani integer dizisi List<int> 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.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    
    namespace uzmanim.Net
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Int32> Limit = new List<int>();
                List<Int32> liste1 = new List<Int32>();
                List<Int32> liste2 = new List<Int32>();
                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();
            }
        }
    }
    Yukarıdaki kodun çıktısı
    For Dongusu :- 461
    Foreach Dongusu:- 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.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    
    namespace uzmanim.Net
    {
        class Program
        {
            static void 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();
    
            }
    
        }
    
    }
    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.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    
    namespace uzmanim.Net
    {
        public class Test
        {
    
            public static string Ad { get; set; }
            public static String _soyad;
    
        }
        class Program
        {
            static void 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();
            }
        }
    
    }
    Yukarıdaki kodun çıktısı şu olacaktır:
    Property Kullanarak:239
    Dogrudan Field'e atama: 1