欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

改進c# 代碼的五個技巧(二)

 更新時間:2021年01月07日 08:40:14   作者:碼農(nóng)譯站  
這篇文章主要介紹了改進c# 代碼的五個技巧(二),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下

在本文中,我將向你展示c#編程的5個最佳實踐。我從日常編程經(jīng)驗中學(xué)到了這些實踐。我在release模式下測試了所有的代碼,并在開發(fā)環(huán)境穩(wěn)定后進行了截屏。我想你會喜歡這些建議的。

在使用數(shù)據(jù)類型之前選擇它

對于許多類型,我們寧愿不決定在日常編程生活中使用什么數(shù)據(jù)類型。就在幾個月前,我也是其中之一。但是當(dāng)我開始學(xué)習(xí)編程中的最佳實踐以提高代碼性能時,我了解到了錯誤的數(shù)據(jù)類型是如何影響代碼的。我將展示一個演示來證明這個概念。

static void Main(string[] args) 
{ 
  List<Int32> li = new List<int>(); 
  Stopwatch sw =new Stopwatch(); 
  sw.Start(); 
  for (int i = 0; i < 10000; i++) 
  { 
    li.Add(i); 
  } 
  sw.Stop(); 
  Console.Write("Using Arraylist(Object)" + sw.ElapsedTicks + "\n"); 
  sw.Reset(); 
  sw.Start(); 
  Int32[] a = new Int32[10000]; 
  for (int i = 0; i < 10000; i++) 
  { 
    a[i] = i; 
  } 
  sw.Stop(); 
  Console.Write("Using Value(Integer Array)" + sw.ElapsedTicks); 
  Console.ReadLine(); 
}

在上面的代碼中,首先我使用了一個list來存儲1000個整數(shù)值,在第二次執(zhí)行相同的操作時,我使用了一個整數(shù)數(shù)組。我的輸出截圖顯示了哪種存儲機制最適合整數(shù)數(shù)組。現(xiàn)在,你可能會想為什么這個list要花更多的時間呢?原因是,list以對象格式存儲數(shù)據(jù),當(dāng)我們首先嘗試存儲值類型時,它將其轉(zhuǎn)換為引用類型,然后再存儲。因此,第一點是始終選擇適當(dāng)?shù)拇鎯C制以獲得最佳性能。

使用for循環(huán)代替foreach

我現(xiàn)在要解釋一個非常有趣的事實。我想你們都熟悉for和foreach循環(huán)?,F(xiàn)在如果我問你哪個更快?嗯…不知道。對吧?

伙計們,for循環(huán)比foreach循環(huán)快得多。讓我們看看下面的例子。

List<Int32> Count = new List<int>();
List<Int32> lst1 = new List<Int32>();
List<Int32> lst2 = new List<Int32>();

for (int i = 0; i < 10000; i++)
{
  Count.Add(i);
}

Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < Count.Count; i++)
{
  lst1.Add(i);
}
sw.Stop();

Console.Write("For Loop :- " + sw.ElapsedTicks + "\n");
sw.Restart();

foreach (int a in Count)
{
  lst2.Add(a);
}
sw.Stop();
Console.Write("Foreach Loop:- " + sw.ElapsedTicks);
Console.ReadLine();

不要擔(dān)心,我已經(jīng)在發(fā)布模式下測試了這個示例,這個屏幕截圖是在幾次測試運行后拍攝的。

選擇何時使用類,何時使用結(jié)構(gòu)體

接受這樣一個事實,即基本理解了c#中的結(jié)構(gòu)體和類,或者至少理解了最喜歡的編程語言中的結(jié)構(gòu)體和類(如果它們存在的話)。好吧,如果你在想“很久以前我學(xué)過結(jié)構(gòu)體,但在日常編碼生活中從未使用過它”,那么你就是那95%從未測量過類和結(jié)構(gòu)體性能的開發(fā)人員中的一員。別擔(dān)心;在寫這篇文章之前,我也沒有。

那么類呢?是的,我們時不時地在日常項目開發(fā)中實現(xiàn)一個類。

現(xiàn)在我的問題是“哪個更快,類還是結(jié)構(gòu)體”?我猜你會想“從未測試過”。好的,我們來測試一下。看看下面的代碼。

namespace BlogProject
{
  struct MyStructure
  {
    public string Name;
    public string Surname;
  }
  class MyClass
  {
    public string Name;
    public string Surname;
  }
  class Program
  {
    static void Main(string[] args)
    {

      MyStructure[] objStruct = new MyStructure[1000];
      MyClass[] objClass = new MyClass[1000];


      Stopwatch sw = new Stopwatch();
      sw.Start();
      for (int i = 0; i < 1000; i++)
      {
        objStruct[i] = newMyStructure();
        objStruct[i].Name = "Sourav";
        objStruct[i].Surname = "Kayal";
      }
      sw.Stop();
      Console.WriteLine("For Structure:- " + sw.ElapsedTicks);
      sw.Restart();

      for (int i = 0; i < 1000; i++)
      {
        objClass[i] = newMyClass();
        objClass[i].Name = "Sourav";
        objClass[i].Surname = "Kayal";
      }
      sw.Stop();
      Console.WriteLine("For Class:- " + sw.ElapsedTicks);

      Console.ReadLine();
    }
  }
}

輸出結(jié)果如下:

現(xiàn)在很明顯,結(jié)構(gòu)體要比類快得多。同樣,我在發(fā)布模式下測試了這段代碼,并獲得了至少20個輸出,以使程序達(dá)到穩(wěn)定的位置。

現(xiàn)在最大的問題是“為什么結(jié)構(gòu)體比類快?”

正如我們所知,結(jié)構(gòu)體變量是值類型,值(或結(jié)構(gòu)體變量)存儲在一個位置。

類對象是引用類型。如果是對象類型,則創(chuàng)建引用,并將值存儲在內(nèi)存的其他位置?;旧?,值存儲在一個可管理的堆中,指針創(chuàng)建在堆棧中。以這種方式在內(nèi)存中實現(xiàn)一個對象,通常要比結(jié)構(gòu)體變量花費更多的時間。

始終使用Stringbuilder進行字符串連接操作

這一點對開發(fā)人員來說非常關(guān)鍵。在進行大量字符串拼接操作時,請使用StringBuilder代替String。為了演示它對代碼性能的影響,我準(zhǔn)備了以下示例代碼。我在for循環(huán)中執(zhí)行了500次字符串拼接操作。

public classTest
{
  public static string Name { get; set; }
  public static string surname; 
} 
class Program
{
  static void Main(string[] args)
  {
    string First = "A";
    StringBuilder sb = new StringBuilder("A");

    Stopwatch st = new Stopwatch();
    st.Start();
    for (int i = 0; i < 500; i++)
    {
      First = First + "A";
    }
    st.Stop();
    Console.WriteLine("Using String :-" + st.ElapsedTicks);
    st.Restart();

    for (int i = 0; i < 500; i++)
    {
      sb.Append("A");
    }
    st.Stop();
    Console.WriteLine("Using Stringbuilder :-" + st.ElapsedTicks);
    Console.ReadLine();
  }
}

這是輸出:

選擇分配類數(shù)據(jù)成員的最佳方式

在為類變量賦值之前,我建議你現(xiàn)在查看以下代碼和輸出屏幕。

namespace Test
{ 
  public class Test 
  { 
    public staticstring Name { get; set; }
    public staticString surname;
  }
  class Program
  {
    static void Main(string[] args)
    {

      Stopwatch st = new Stopwatch();
      st.Start();
      for (int i = 0; i < 100; i++)
      {
        Test.Name = "Value";
      }
      st.Stop();
      Console.WriteLine("Using Property: " + st.ElapsedTicks);
      st.Restart();
      for (int i = 0; i < 100; i++)
      {
        Test.surname = "Value";
      }
      st.Stop();
      Console.WriteLine("Direct Assign: " + st.ElapsedTicks);
      Console.ReadLine();
    }
  } 
}

是的,我們的輸出屏幕是說,使用屬性分配數(shù)據(jù)成員比直接分配要慢得多。

以上就是改進c# 代碼的五個技巧(二)的詳細(xì)內(nèi)容,更多關(guān)于改進c# 代碼的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論