前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >unity3d:函数耗时记录,Profiler性能采样

unity3d:函数耗时记录,Profiler性能采样

作者头像
立羽
发布2023-08-24 15:20:03
3220
发布2023-08-24 15:20:03
举报
文章被收录于专栏:Unity3d程序开发Unity3d程序开发

Time.time

在一帧内,Time.time只会赋值更新一次,所以一帧内获取不到时间差值。

代码语言:javascript
复制
void Start () {
        TestArrayAList();
    }

    void TestArrayAList()
    {
        int max = 100000000;
        int[] array;
        array = new int[max];
        List<int> list = new List<int>();
        float time = Time.time;

        for (int i = 0; i < max; i++)
        {
            array[i] = i;
        }
        Debug.Log(Time.time - time);

        time = Time.time;
        for (int i = 0; i < max; i++)
        {
            list.Add(i);
        }
        Debug.Log(Time.time - time);
        array = null;
    }

如上代码,输出为0

Stopwatch

代码语言:javascript
复制
void TestArrayAList()
    {
        int max = 100000000;
        int[] array;
        array = new int[max];
        List<int> list = new List<int>();
        float time = Time.time;
        Stopwatch stop = new Stopwatch();
        stop.Start();
        for (int i = 0; i < max; i++)
        {
            array[i] = i;
        }
        UnityEngine.Debug.Log(stop.ElapsedMilliseconds);
        stop.Reset();
        stop.Start();
        time = Time.time;
        for (int i = 0; i < max; i++)
        {
            list.Add(i);
        }
        UnityEngine.Debug.Log(stop.ElapsedMilliseconds);
        array = null;
    }
在这里插入图片描述
在这里插入图片描述

Profiler性能采样

代码语言:javascript
复制
void TestArrayAList()
    {
        int max = 10000; 
        int[] array;
        array = new int[max];
        List<int> list = new List<int>();
        float time = Time.time;
        Stopwatch stop = new Stopwatch();
        stop.Start();
        Profiler.BeginSample("TestArray");
        for (int i = 0; i < max; i++) 
        {
            array[i] = i;
        }
        Profiler.EndSample();
        UnityEngine.Debug.Log(stop.ElapsedMilliseconds);
        stop.Reset();
        stop.Start();
        time = Time.time;
        Profiler.BeginSample("TestList");
        for (int i = 0; i < max; i++)
        {
            list.Add(i);
        }
        Profiler.EndSample();
        UnityEngine.Debug.Log(stop.ElapsedMilliseconds);
        array = null;
    }

    // Update is called once per frame
    void Update () {
        TestArrayAList();

    }
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-12-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Time.time
  • Stopwatch
  • Profiler性能采样
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档