改进c# 代码的五个技巧(二)

这篇文章主要介绍了改进c# 代码的五个技巧(二),帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下

在本文中,我将向你展示c#编程的5个最佳实践。我从日常编程经验中学到了这些实践。我在release模式下测试了所有的代码,并在开发环境稳定后进行了截屏。我想你会喜欢这些建议的。

在使用数据类型之前选择它

对于许多类型,我们宁愿不决定在日常编程生活中使用什么数据类型。就在几个月前,我也是其中之一。但是当我开始学习编程中的最佳实践以提高代码性能时,我了解到了错误的数据类型是如何影响代码的。我将展示一个演示来证明这个概念。


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<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();


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

输出结果如下:


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

这是输出:


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

是的,我们的输出屏幕是说,使用属性分配数据成员比直接分配要慢得多。

以上就是改进c# 代码的五个技巧(二)的详细内容,更多关于改进c# 代码的资料请关注得得之家其它相关文章!

本文标题为:改进c# 代码的五个技巧(二)