C#性能優化:延遲初始化Lazy

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的值是nullthis

接着往下走,調用到Name屬性時,student的IsValueCreated的值是true,value的值已經不爲null了spa

運行結果:線程

結果能夠看出,Student是在輸出Name屬性才進行初始化的,也就是在第一次使用時纔會去初始化,這樣就能夠達到減小消耗的目的。3d

這個例子很簡單,也是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();
        }
    }
}

運行結果:code

注:Lazy<T> 對象初始化默認是線程安全的,在多線程環境下,第一個訪問 Lazy<T> 對象的 Value 屬性的線程將初始化 Lazy<T> 對象,之後訪問的線程都將使用第一次初始化的數據。

4. 應用場景

有一個對象的建立開銷很大,而程序可能不會使用它。例如,假定您的程序在啓動時加載若干個對象實例,但只有一些對象實例須要當即執行。經過將沒必要要的對象的初始化延遲到已建立必要的對象以後,能夠提升程序的啓動性能。

相關文章
相關標籤/搜索