unity中检测代码执行时间

来源:互联网 发布:手机修改mac的软件 编辑:程序博客网 时间:2024/05/16 06:34

使用unity编写代码的大多数使用的都是c#,c#中可以使用特定的语句来对代码的执行效率进行检测。

检测代码如下:

using UnityEngine;using System.Collections;public class Test: MonoBehaviour {    void Update()    {        if (Input.GetKeyDown(KeyCode.G))        {            TestExeTime();        }    }    void TestExeTime()    {        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();        stopwatch.Start(); //  开始监视代码运行时间        TestFunc();        stopwatch.Stop(); //  停止监视        //  获取当前实例测量得出的总时间        System.TimeSpan timespan = stopwatch.Elapsed;        //   double hours = timespan.TotalHours; // 总小时        //    double minutes = timespan.TotalMinutes;  // 总分钟        //    double seconds = timespan.TotalSeconds;  //  总秒数        double milliseconds = timespan.TotalMilliseconds;  //  总毫秒数        //打印代码执行时间        Debug.Log(milliseconds);    }    void TestFunc()    {        int m = 124;        int n = 256;        int mn = m + n;       Debug.Log(mn);    }}

运行,可得到如下结果:
这里写图片描述

将TestFunc()中的Debug.Log(mn);注释掉,再测试可得

    void TestFunc()    {        int m = 124;        int n = 256;        int mn = m + n;       // Debug.Log(mn);    }

这里写图片描述

0 0