关于c#:改进你的c代码的5个技巧一

2次阅读

共计 4697 个字符,预计需要花费 12 分钟才能阅读完成。

敬爱的读者,在这篇文章中,我提供了一些 c# 编程的最佳实际。

你是否在用户输出验证中应用异样解决机制?

如果是,那么你就是那个把你的我的项目执行速度升高了 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();}  
}  

}

; “ 复制代码 ”)

这就是你期待的输入。

我的概念证实非常简单。在一个函数中,我抛出了一个异样,在另一个函数中,我在检查用户输出后返回一个布尔值。我还附上了一个计算器的屏幕(哈哈..),让你置信异样解决是如何影响代码性能的。

因而,咱们能够得出这样一个论断:“不要为用户输出验证引发异样。”应用布尔返回技术 (或相似的技术) 来验证业务逻辑中的输出”。因为异样对象的开销十分大。

永远不要在循环中实现 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();}  
}  

}

; “ 复制代码 ”)

这是输入屏幕。

在 method1 的这个程序中,我在 for 循环中实现了异样解决机制,而在 method2 中,我在没有循环的状况下实现了异样解决机制。咱们的输入窗口表明,如果咱们在 for 循环外实现 try-catch 程序的执行速度将比循环内的 try-catch 快 2 倍。

同样,惟一的论断是“不要在我的项目的循环中实现 try-catch。(是的! 不仅在 for 循环中,而且在任何循环中。)

你是否疯狂到要应用 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 倍。我能够否定输入屏幕,但有一件事!! 你一次要创立 1000 个变量; 在咱们的我的项目中,咱们不会一次创立 1000 个变量,最多创立 2 到 3 个。

好的。你的应用程序是 web 应用程序吗? 如果是,那么请查看任何风行的 web 应用程序的点击数,我确信它超过 1000 每天。

同样,这一行的论断是“不要疯狂地应用 new 操作符来创立整数变量”。

依据你的目标抉择最好的汇合

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

}

; “ 复制代码 ”)

输入在这里:

咱们能够分明地看到,在字典的状况下,搜寻的性能是最差的,而在 list 和整数数组的状况下,性能十分类似。

办法是好的,但不是所有时候

如果你还记得你刚开始学习编程的那几天,你学过一个概念,就是总是实现一个办法来在代码中实现好的练习,是的,实现一个办法来执行某些工作是很好的。办法在编程中有成千上万的长处,然而让咱们看看办法是如何升高执行性能的。我再次强调,这一点并不是拥护办法,而是简略地展现办法调用是一种代价昂扬的机制,并提供了在何处实现办法以及在何处不实现办法的想法。让咱们看看上面的代码。

; “ 复制代码 ”)

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 倍。因而,惟一的论断是“在自觉执行某个性能之前,试着理解状况并做出最佳决策”

正文完
 0