技术资源

C#性能优化5大技巧

发布时间:2016/7/20 17:22:25    来源地址:本站原创

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


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

 

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

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

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

这是屏幕输出结果。

在这个程序中,方法1我在for循环中实现了异常处理机制,在方法2中则没有。输出结果说明在循环外面比在循环里面处理异常会快上2倍。

于是我们又得到一个结论:在项目中,不要在循环内部实现try-catch。(注意:不只是for循环,包括所有的循环。)

 

3.你是否疯狂了,以致于使用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倍。嗯...Sourav,我可以举出一个问题来否认结果。你一次创建1000个变量,而在我们的项目中是不可能一次创建1000个变量的。我们最多也只是创建2个或者3个。

OK!如果你的程序是web程序呢?如果你要校验流行的web程序的点击数,我确定每天将会超过1000个。

因此,我们又得到了一个结论:别太疯狂的使用new操作符来创建整型变量。

 

4.根据目的选择最好的集合。

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

这是输出结果。

我们可以清晰的看到,字典集合的查询性能是最差的,而表单和整型数据的性能差不多。

 

5.函数很好,但有时却不时。

如果你们还记得学习编程的日子,你们学到的第一个概念一般都是函数实现是一个好习惯,而且也很乐于将函数实现作为一个主要任务。函数在编程中有成千上万的优点,但是我们还是要看看函数是如何降低执行性能的。不过,这样的目的并不是反对函数,而是向你展示函数调用是有代价的机制,并且提供了一个观点:哪里需要实现函数,哪里不需要实现函数。让我们看看下面的代码。

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倍。很显然,我们并没有去争论函数的优缺点的问题

业务咨询热线:

020-82038459

产品售后服务专线:

020-82038459

在线咨询: