提升 .NET 程序性能的 一些 原则

1. 尽量少的抛出异常


throw new System.Exception(.);
抛出异常是严重影响性能的。
(a) 对于ASP.NET, Response.Redirect(...)
会抛出 ThreadAbort 的异常, 而且,该用法会在 Client 和 Server间返回两次,应尽量采用Client 端的方式或 Server.Transfer方式来代替。

2. 对于函数,要尽量写的短小精悍,而不要认为一个函数的代码写的越长,就越节省资源。

3. 尽量采用值类型


public struct foo{
public foo(double arg){ this.y = arg; }
public double y;
}
public class bar{
public bar(double arg){ this.y = arg; }
public double y;
}

class Class1{
static void Main(string[] args){
System.Console.WriteLine("starting struct loop");
for(int i = 0; i < 50000000; i++)
{foo test = new foo(3.14);}
System.Console.WriteLine("struct loop complete.
starting object loop");
for(int i = 0; i < 50000000; i++)
{bar test2 = new bar(3.14); }
System.Console.WriteLine("All done");
}
}
}

4. 使用 AddRange 代替 循环的 Add

5. 尽量减少 NameSpace 的引用,而是采用 System.Web.UI.xxxx 的方式
原文地址:https://www.cnblogs.com/redfox241/p/972021.html