技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運(yùn)營(yíng)

贊助商

分類目錄

贊助商

最新文章

搜索

[C#技巧]在for循環(huán)外比在for循環(huán)內(nèi)使用try-catch速度快

作者:admin    時(shí)間:2023-5-5 10:18:30    瀏覽:

C#編程中try-catch非常常用,它是用來處理異常的結(jié)構(gòu),不過“嘗試-捕捉”是以犧牲代碼性能為代價(jià)的,它會(huì)影響代碼的運(yùn)行速度,因此我們要謹(jǐn)慎使用它。

不要在for循環(huán)內(nèi)使用try-catch

本文通過實(shí)測(cè),證明在for循環(huán)內(nèi)比在for循環(huán)外使用try-catch速度慢很多。測(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 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();  
        }  
    }  
}  

下面是輸出屏幕

 在for循環(huán)內(nèi)比在for循環(huán)外使用try-catch速度慢2倍

在方法 1 中的這個(gè)程序中,我在 for 循環(huán)中實(shí)現(xiàn)了異常處理機(jī)制,而在方法 2 中,我在沒有循環(huán)的情況下實(shí)現(xiàn)了它。我們的輸出窗口顯示,如果我們?cè)?for 循環(huán)之外實(shí)現(xiàn) try-catch,那么我們的程序執(zhí)行速度將比循環(huán)內(nèi)的 try-catch 快 2 倍。

因此,不要在項(xiàng)目的循環(huán)內(nèi)實(shí)現(xiàn) try-catch,不僅在 for 循環(huán)內(nèi),而且在任何循環(huán)內(nèi)。

不要在用戶輸入時(shí)處理異常

你是否使用異常處理機(jī)制來驗(yàn)證用戶的輸入?

如果是,那么你就是將項(xiàng)目執(zhí)行速度降低 62 倍的人。例如,如下所示:

class BusinessLogcCheck  
{  
    public void Check()  
    {  
        try  
        {  
            //Your validation code is here  
        }  
        catch (Exception ex)  
        {  
            throw new Exception("My own exception");  
        }  
    }  
}  

在下一個(gè)示例中,如果你看到輸出屏幕,你就會(huì)意識(shí)到這種做法有多糟糕。讓我們看看下面的代碼。

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

下面是執(zhí)行結(jié)果

 不要在用戶輸入時(shí)處理異常

我的概念證明非常簡(jiǎn)單。在一個(gè)函數(shù)中我引發(fā)異常,在另一個(gè)函數(shù)中我在檢查用戶輸入后返回一個(gè)布爾值。我附上了一個(gè)計(jì)算器屏幕,讓你相信異常處理是如何影響代碼性能的。

因此,我們可以得出一個(gè)結(jié)論“不要為用戶輸入驗(yàn)證引發(fā)異常,而是使用布爾返回技術(shù)(或類似技術(shù))來驗(yàn)證業(yè)務(wù)邏輯中的輸入”,因?yàn)楫惓?duì)象是非常昂貴的。

總結(jié)

C#使用“try-catch”是以犧牲代碼性能為代價(jià)的,它會(huì)影響代碼的運(yùn)行速度,因此我們要謹(jǐn)慎使用它。另外,不要為用戶輸入驗(yàn)證引發(fā)異常,因?yàn)楫惓?duì)象是非常昂貴的。

相關(guān)文章

標(biāo)簽: asp.net  CSharp  代碼性能  優(yōu)化  try-catch  for  
x
  • 站長(zhǎng)推薦
/* 左側(cè)顯示文章內(nèi)容目錄 */