亲爱的读者,在这篇文章中,我提供了一些c#编程的最佳实践。

你是否在用户输入验证中使用异常处理机制?

如果是,那么你就是那个把你的项目执行速度降低了62倍的人。你不相信我吗?等几分钟;我来教你怎么做。但是在这个例子之前,让我们了解一下在什么地方需要异常处理。

例如,你正在验证用户的数据,对于任何无效的输入,你将引发一个异常并将其抛出给客户端,如下所示:

class businesslogccheck 
{ 
  public void check() 
  { 
    try 
    { 
      //your validation code is here 
    } 
    catch (exception ex) 
    { 
      throw new exception("my own exception"); 
    } 
  } 
}

亲爱的朋友,在下一个示例中,如果你看到输出屏幕,你将意识到这种做法有多糟糕。让我们看看下面的代码。

using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.diagnostics; 
using system.io; 
using system.net; 
using system.net.networkinformation; 
namespace test1 
{ 
  class program 
  { 
    public static void throwtest() 
    { 
      throw new exception("this is exceptopn"); 
    } 
    public static boolean return() 
    { 
      return false; 
    } 
    static void main(string[] args) 
    { 
      stopwatch sw = new stopwatch(); 
      sw.start(); 
      try 
      { 
          throwtest(); 
      } 
      catch 
      { 
      } 
      sw.stop(); 
      console.writeline("with exception " + sw.elapsedticks); 
      sw.restart(); 
      try 
      { 
        return(); 
      } 
      catch 
      { 
      } 
      sw.stop(); 
      console.writeline("with return " + sw.elapsedticks); 
      console.readline(); 
    } 
  } 
}

这就是你等待的输出。

我的概念证明非常简单。在一个函数中,我抛出了一个异常,在另一个函数中,我在检查用户输入后返回一个布尔值。我还附上了一个计算器的屏幕(哈哈..),让你相信异常处理是如何影响代码性能的。

因此,我们可以得出这样一个结论:“不要为用户输入验证引发异常。”使用布尔返回技术(或类似的技术)来验证业务逻辑中的输入”。因为异常对象的开销非常大。

永远不要在循环中实现try-catch

是的,它也与异常处理有关。我重复“永远不要在循环中执行try-catch”。让我用一个例子来证明。

using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.diagnostics; 
using system.io; 
using system.net; 
using system.net.networkinformation; 
namespace test1 
{ 
  class program 
  { 
    static void method1() 
    { 
      for (int i = 0; i < 1000; i++) 
      { 
        try 
        { 
          int value = i * 100; 
          if (value == -1) 
          { 
            throw new exception(); 
          } 
        } 
        catch 
        { 
        } 
      } 
    } 
    static void method2() 
    { 
      try 
      { 
        for (int i = 0; i < 1000; i++) 
        { 
          int value = i * 100; 
          if (value == -1) 
          { 
            throw new exception(); 
          } 
        } 
      } 
      catch 
      { 
      } 
    } 
    static void main(string[] args) 
    { 
      stopwatch sw = new stopwatch(); 
      sw.start(); 
      method1(); 
      sw.stop(); 
      console.writeline("within loop " + sw.elapsedticks); 
      sw.restart(); 
      method2(); 
      sw.stop(); 
      console.writeline("outside of loop " + sw.elapsedticks); 
      console.readline(); 
    } 
  } 
}

这是输出屏幕。

在method1的这个程序中,我在for循环中实现了异常处理机制,而在method2中,我在没有循环的情况下实现了异常处理机制。我们的输出窗口表明,如果我们在for循环外实现try-catch程序的执行速度将比循环内的try-catch快2倍。

同样,唯一的结论是“不要在项目的循环中实现try-catch。(是的!不仅在for循环中,而且在任何循环中。)

你是否疯狂到要使用new操作符来创建一个整数变量?

亲爱的读者,不要因为我写了这么长的标题而批评我,也不要使用new操作符来创建一个简单的整数变量。我知道你会说,如果你使用new操作符创建一个简单的整数变量就会被自动设置为0,不遭受错误,如“未赋值的局部变量”,但这真的是需要得到一个自动赋值为0,你的目的是创建一个局部变量来存储吗?让我们看看new操作符是如何降低代码执行的性能的。 

using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.diagnostics; 
using system.io; 
using system.net; 
using system.net.networkinformation; 
namespace test1 
{ 
  class program 
  { 
    static void main(string[] args) 
    { 
      stopwatch sw = new stopwatch(); 
      sw.start(); 
      for (int i = 0; i < 1000; i++) 
      { 
        int a = new int(); 
        a = 100; 
      } 
      sw.stop(); 
      console.writeline("using new operator:- " + sw.elapsedticks); 
      sw.restart(); 
      for (int i = 0; i < 1000; i++) 
      { 
        int a; 
        a = 100; 
      } 
      sw.stop(); 
      console.writeline("without new operator:- "+ sw.elapsedticks); 
      console.readline(); 
    } 
  } 
}

输出的截图如下:

new操作符将执行速度降低了5倍。我可以否认输出屏幕,但有一件事!!你一次要创建1000个变量;在我们的项目中,我们不会一次创建1000个变量,最多创建2到3个。

好的。你的应用程序是web应用程序吗?如果是,那么请检查任何流行的web应用程序的点击数,我确信它超过1000每天。

同样,这一行的结论是“不要疯狂地使用new操作符来创建整数变量”。

根据你的目的选择最好的集合

我们.net开发人员非常熟悉c#中的集合以及它们用来存储值的方法。让我们看看它们是如何执行搜索的。查看搜索整数的性能。这是我的代码。

using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.diagnostics; 
using system.io; 
using system.net; 
using system.net.networkinformation; 
namespace test1 
{ 
  class program 
  { 
    static void main(string[] args) 
    { 
      list<int32> li = new list<int32>(1000); 
      dictionary<int, int> di = new dictionary<int, int>(1000); 
      int[] arr = new int[1000]; 
      int a; 
      for (int i = 0; i < 1000; i++) 
      { 
        li.add(i); 
        di.add(i, i); 
        arr[i] = i; 
      } 
      stopwatch sw = new stopwatch(); 
      sw.start(); 
      a = li[500]; 
      sw.stop(); 
      console.writeline("from list:- " + sw.elapsedticks); 
      sw.start(); 
      a = arr[500]; 
      sw.stop(); 
      console.writeline("from integer array:- " + sw.elapsedticks); 
      sw.restart(); 
      a = di[500]; 
      sw.stop(); 
      console.writeline("from dictionary:- " + sw.elapsedticks); 
      console.readline(); 
    } 
  } 
}

输出在这里:

我们可以清楚地看到,在字典的情况下,搜索的性能是最差的,而在list和整数数组的情况下,性能非常相似。

方法是好的,但不是所有时候

如果你还记得你刚开始学习编程的那几天,你学过一个概念,就是总是实现一个方法来在代码中实现好的练习,是的,实现一个方法来执行某些任务是很好的。方法在编程中有成千上万的优点,但是让我们看看方法是如何降低执行性能的。我再次强调,这一点并不是反对方法,而是简单地展示方法调用是一种代价高昂的机制,并提供了在何处实现方法以及在何处不实现方法的想法。让我们看看下面的代码。

using system; 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.diagnostics; 
using system.io; 
using system.net; 
using system.net.networkinformation; 
namespace test1 
{ 
  class test 
  { 
    public static void print() 
    { 
      console.writeline("i am function from class"); 
    } 
  } 
  class program 
  { 
    static void main(string[] args) 
    { 
      stopwatch sw = new stopwatch(); 
      sw.start(); 
      test.print(); 
      sw.stop(); 
      console.writeline(sw.elapsedticks); 
      sw.restart(); 
      console.writeline("i am single statement within main"); 
      sw.stop(); 
      console.writeline(sw.elapsedticks); 
      console.readline(); 
    } 
  } 
}

下面是屏幕输出:

在这里,我想在输出窗口中打印一条消息,首先,我在一个静态函数中实现了它,并通过类名调用它,第二次我只是在主函数中编写它。可以,通过console.writeline()非常简单。输出屏幕显示单行执行比函数快9倍。因此,唯一的结论是“在盲目执行某个功能之前,试着了解情况并做出最佳决策”

结论

谢谢你能容忍我这么长时间。我在我的笔记本电脑上做了上面的测试,我的笔记本电脑有core i3处理器,4gb内存和windows环境,在程序稳定后以释放模式输出。如果你使用不同的平台和不同的输出,在评论部分有足够的空间写评论。

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