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

本文齐全独立于前两篇文章。如果你喜爱它们,我心愿你也会喜爱这个。在上一篇文章中,我展现了哪种办法更快,并比拟了代码的执行速度。在本文中,我将展现不同代码片段的内存耗费状况。为了显示内存映射和调配图,我应用了CLR profiler 32位版本,和平常一样,我在Windows平台上应用了4GB RAM和Core i3 CPU。内存耗费或调配图可能依据零碎运行的过程而变动。因而,如果你失去一个不同的输入或行为的代码,那么请与咱们分享你的教训。

让咱们开始“改良c#代码的5个技巧:第3局部”的旅程。

StringBuilder耗费的内存比String少

在我的上一篇文章中,我曾经展现了在长连贯操作的场景中字符串的速度有多慢。这里咱们会看到一个字符串和StringBuilder的内存调配图。让我来演示一下。上面是我应用字符串和StringBuilder进行雷同操作的代码。

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;  
using System.Threading;  
using System.Globalization;  
using System.Data.SqlClient;  
namespace Test1  
{  
    public class Test1  
    {  
        string Name ;  
        public void Process()  
        {  
            Name = Name + "A";  
        }  
    }  
    public class Test2  
    {  
        StringBuilder sb = new StringBuilder();  
        public void Process()  
        {  
            sb.Append("A");  
        }  
    }  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Test1 t = new Test1();  
            t.Process();   
            Test2 t1 = new Test2();  
            t1.Process();   
        }  
    }  
} 

这是代码执行时的内存调配图。

这里咱们从main函数调用两个函数Process();只管它们都有雷同的名称,但它们属于不同的类和Test1.Process解决字符串数据,而Test2.Process()解决StringBuilder数据。在调配图中,咱们能够看到字符串处理函数耗费了Main()函数94%的资源,而Test2类中解决StringBuilder的Process()只耗费了Main()函数的0.21%的资源。

因而,论断是“当你想屡次连贯字符串时,总是应用StringBuilder”。

如果可能的话,应用动态函数

是的,如果可能的话,尝试实现一个动态函数,因为动态对象(函数和数据)不属于特定类的任何对象。这是大家都有的。因而,如果不创建对象,就不存在内存耗费的问题。上面我将展现一个动态函数和动态类的示例。看一下IL代码。

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;  
using System.Threading;  
using System.Globalization;  
using System.Data.SqlClient;  
namespace Test1  
{  
    public static class mySclass  
    {  
        public static void Print()  
        {  
            Console.Write("Hello");  
        }  
    }  
    public class myNclass  
    {  
        public static void Print()  
        {  
            Console.Write("Hello");  
        }  
    }  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            for (int i = 0; i < 1000; i++)  
            {  
                mySclass.Print();  
                myNclass.Print();  
            }  
        }  
    }  
} 

IL代码在左手边,在右手边是由CLR分析器获取的内存耗费类。因为空间耗费,我无奈显示CLR分析器的残缺截图。然而置信我,动态类或函数没有内存调配。

因而,论断是“如果可能,尝试创立一个动态函数并应用类名调用,而不是通过对象名调用通用函数”。

字符串格式化VS字符串连贯

在第一点中,我展现了字符串如何比StringBuilder耗费更多的资源。在这一点上,我将比拟格式化输入和字符串连贯。在第一个函数中,我应用了一个格局标准来打印格式化的输入(基本上是连贯一个字符串)。在另一个函数中,我应用(+)操作符连贯一个字符串,如下所示:

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;  
using System.Threading;  
using System.Globalization;  
using System.Data.SqlClient;  
namespace Test1  
{  
    class Test  
    {  
        public void Format()  
        {  
            int a = 100;  
            Console.WriteLine("{0}AND{1}", a, a);  
        }  
        public void Concatination()  
        {  
            int a = 100;  
            Console.WriteLine(a + "AND" +a );  
        }  
    }   
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Test t = new Test();  
            t.Format();  
            t.Concatination();  
            Console.ReadLine();  
        }  
    }  
} 

在内存调配中咱们会看到:

应用format打印字符串的函数耗费了57%的资源,而简略连贯两个字符串的函数耗费了主函数的30%的资源。因而,咱们能够分明地看到,如果应用字符串连贯而不是输入格式化,就能够节俭系统资源。

空程序中哪个类耗费的资源最多?

首先,这一点并不举荐任何最佳实际技术。我只是想阐明,如果咱们运行一个空程序(其中只有一个Main()函数),那么调配了多少内存?上面是我的一个非常简单的程序。

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;   
namespace Test1  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
        }  
    }
} 

是的,我没有在这个程序里写任何货色。让咱们看看内存映射。

在这里,我展现了与运行空程序场景相干的六个最消耗资源的类。能够分明地看到,String类占用了最多的资源(占全副资源的25%)。当初发问。在一个咱们从不应用字符串的程序中,为什么字符串类耗费的资源最多?如果咱们看一下这个程序的调用图,咱们会看到在main函数中有许多外部函数被调用,它们中的大多数都以字符串作为参数,为了生成这些参数,CLR通常应用string类。如果你有不同的意见,请应用上面的评论框。

实现一个using块来治理内存

始终实现一个using块来治理资源是一个最佳实际。实际上,咱们能够证实应用block语句比不应用block语句耗费的内存更少。咱们晓得,如果咱们实现一个using块的块代码大小可能会更大,因为using块在外部在IL代码中创立了一个try catch,但一旦它在运行时在IL代码中实现,它就能无效地解决零碎内存。为了演示这一点,我编写了一个简略的程序,如下所示:

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;  
using System.Threading;  
using System.Globalization;  
using System.Data.SqlClient;  
namespace Test1  
{  
    class Test  
    {  
        public void Test1()  
        {  
             StreamWriter wr = new StreamWriter(@"D:text.txt");  
        }  
        public void Test2()  
        {  
             using (StreamWriter wr = new StreamWriter(@"D:abc.txt"))  
             {   
             }  
        }  
    }   
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Test t = new Test();  
            t.Test1();  
            t.Test2();   
        }  
    }  
} 

在输入局部,我组合了三个输入屏幕。

在调配图中,咱们看到using块比没有using块时耗费的资源更少,因为如果咱们实现了using块,程序能够无效地治理内存。

欢送关注我的公众号,如果你有喜爱的外文技术文章,能够通过公众号留言举荐给我。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理