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();
        }
    }
}
这是屏幕输出结果。

Image2.jpg

在这个程序中,方法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();
        }
    }
}

这是输出快照。

Image3.jpg

是的,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();
        }
    }
}
这是输出结果。

Image4.jpg

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

Image5.jpg


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

这是输入结果。

Image6.jpg

这里我想在窗口上打印简单的信息,第一次调用类的静态方法,第二次直接在主函数中输出。是非常简单的Console.Writeline().而从屏幕输出的结果说明简单的单行输出比函数快了9倍。很显然,我们并没有去争论“函数的优缺点”的问题。(哈哈)

所以,我们又得到了一个简单的结论:在盲目实现一个函数前,先尝试理解情景,再作出最好的决定。


结论:

多谢读者们忍受了我这么长时间,以上示例都在电脑上进行了测试,并且是在程序的条件下编程,在release模式下输出结果。我的电脑是I3处理器、4G内存、Windows系统。如果你使用不同的电脑或者不同的输出,请看下面。那里有足够的空间来写这个版块的评论。

点击加入qq群,一起学习交流

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值