日期:2014-05-17  浏览次数:20658 次

C#性能优化5大技巧(最高可达62倍)

原文http://www.c-sharpcorner.com/UploadFile/dacca2/5-tips-to-improve-your-C-Sharp-code-part-1/

在阅读这篇文章之前,先阅读下面这篇文章。

优化C#代码性能的5个小窍门

1、你是否用异常机制来处理过用户输入验证?

如果是,那么你的程序性能已经降低了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();
        }
    }
}

这是你正在等待的输入结果。

Image1.jpg


我的证明非常简单。在一个函数中引发一个异常,而在另一个函数中返回用户输入校验后的布尔值。而且我附加了一个计算器让你相信异常处理是如何影响代码性能的。

所以我们可以得出一个结果:不要针对用户输入验证抛出一个异常,而使用布尔值来返回验证输入的业务逻辑(或者其他相似的技术)。因为异常对象代价太高了。(不过也高不过你所钟爱的衬衣。哈哈)

2.绝对不要在循环中使用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();
        }
    }
}
这是屏幕输出结果。