zoukankan      html  css  js  c++  java
  • C#性能优化:延迟初始化Lazy<T>

    1. 概述

    我们创建某一个对象需要很大的消耗,而这个对象在运行过程中又不一定用到,为了避免每次运行都创建该对象,这时候延迟初始化(也叫延迟实例化)就出场了。

    延迟初始化出现于.NET 4.0,主要用于提高性能,避免浪费计算,并减少程序内存要求。也可以称为,按需加载。

    2. 基本语法

    Lazy<T> xx = new Lazy<T>();//xx代表变量名

    3. 举例实现

    首先创建一个Student类,代码如下:

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LazyTest
    {
        class Student
        {
            public Student()
            {
                this.Name = "DefaultName";
                Console.WriteLine("调用Student的构造函数");
            }
    
            public string Name { get; set; }
        }
    }
    复制代码

    创建一个控制台程序,代码如下:

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LazyTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Lazy<Student> student = new Lazy<Student>();
                if (!student.IsValueCreated)
                {
                    Console.WriteLine("Student未初始化");
                }
                Console.WriteLine(student.Value.Name);
                if (student.IsValueCreated)
                {
                    Console.WriteLine("Student已经初始化");
                }
                Console.ReadKey();
            }
        }
    }
    复制代码

    设置断点调试后发现,在new完之后,student的IsValueCreated的值是false,value的值是null

    接着往下走,调用到Name属性时,student的IsValueCreated的值是true,value的值已经不为null了

    运行结果:

    结果可以看出,Student是在输出Name属性才进行初始化的,也就是在第一次使用时才会去初始化,这样就可以达到减少消耗的目的。

    这个例子很简单,也是Lazy<T>最基本的使用方式。我们还可以使用 Lazy<T> 的重载函数 Lazy<T> (Func<T>) 传入一个带返回值的委托来设置延迟初始化对象的属性值。

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LazyTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Lazy<Student> student = new Lazy<Student>(() => new Student { Name = "SetName" });
                if (!student.IsValueCreated)
                {
                    Console.WriteLine("Student未初始化");
                }
                Console.WriteLine(student.Value.Name);
                if (student.IsValueCreated)
                {
                    Console.WriteLine("Student已经初始化");
                }
                Console.ReadKey();
            }
        }
    }
    复制代码

    运行结果:

    注:Lazy<T> 对象初始化默认是线程安全的,在多线程环境下,第一个访问 Lazy<T> 对象的 Value 属性的线程将初始化 Lazy<T> 对象,以后访问的线程都将使用第一次初始化的数据。

    4. 应用场景

    有一个对象的创建开销很大,而程序可能不会使用它。例如,假定您的程序在启动时加载若干个对象实例,但只有一些对象实例需要立即执行。通过将不必要的对象的初始化延迟到已创建必要的对象之后,可以提高程序的启动性能。

  • 相关阅读:
    Linux部署之NFS方式安装系统
    VMware Workstation Pro学习探索(Linux,Docker)
    sqlserver最大内存设置太小导致无法启动sql服务
    Docker下安装Sqlserver(mssql)
    docker错误:net/http: TLS handshake timeout;解决方案
    Linux和Docker常用命令
    Linux及Docker学习记录
    .net core视图预编译
    oracle的一些简单语法
    Oracle安装连接常见错误
  • 原文地址:https://www.cnblogs.com/soundcode/p/5309881.html
Copyright © 2011-2022 走看看