在Unity中进行速度和GC Alloc的测试

 

测试脚本:

using UnityEngine;
using System;
using System.Collections;
using System.Diagnostics;

public class NullableTest : MonoBehaviour
{
    void Start()
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        for (int i = 0; i < 1000000; i++)
        {
            int a = i;
            //int? a = i;
            a.GetHashCode();
        }
        stopwatch.Stop();

        UnityEngine.Debug.Log("time(ms): " + stopwatch.ElapsedMilliseconds);
    }
}

 

100万次循环下,可空类型执行速度45ms,非可空类型执行速度12ms

C#可空类型的速度和GC Alloc测试

 

 

并且没有GC。

C#可空类型的速度和GC Alloc测试

相关文章:

  • 2021-07-21
  • 2022-01-26
  • 2021-09-10
  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-06
  • 2021-05-16
  • 2022-02-13
  • 2021-06-01
相关资源
相似解决方案