打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【Unity3D之获取某个方法执行的时间】
    #region 计算代码执行花费的时间
    //如果要用纳秒级或微秒级的话 就多测几次取平均

    #region 纳秒级10-9秒
    public static void calculate_cudetime_namiao(this MonoBehaviour mono, string methodname)
    {
        Type t = mono.GetType();

        MethodInfo vMethodInfo = t.GetMethod(methodname);
        if (vMethodInfo == null) return;

        PerformanceTimer timer = new PerformanceTimer();
        timer.Start();
        vMethodInfo.Invoke(mono, null);
        timer.Stop();

        MyDebug.Log(methodname + " 使用时间纳秒  " + timer.Duration * 1000000000);
    }

    public static void calculate_cudetime_namiao(this MonoBehaviour mono, string methodname, object pram1)
    {
        Type t = mono.GetType();

        MethodInfo vMethodInfo = t.GetMethod(methodname, new Type[] { pram1.GetType() });
        if (vMethodInfo == null) return;

        PerformanceTimer timer = new PerformanceTimer();
        timer.Start();
        vMethodInfo.Invoke(mono, new object[] { pram1 });
        timer.Stop();

        MyDebug.Log(methodname + " 使用时间纳秒  " + timer.Duration * 1000000000);
    }

    public static void calculate_cudetime_namiao(this MonoBehaviour mono, string methodname, object pram1, object pram2)
    {
        Type t = mono.GetType();
        Type p1 = pram1.GetType();
        Type p2 = pram2.GetType();
        MethodInfo vMethodInfo = t.GetMethod(methodname, new Type[] { pram1.GetType(), pram2.GetType() });
        if (vMethodInfo == null) return;
        PerformanceTimer timer = new PerformanceTimer();
        timer.Start();
        vMethodInfo.Invoke(mono, new object[] { pram1, pram2 });
        timer.Stop();

        MyDebug.Log(methodname + " 使用时间纳秒  " + timer.Duration * 1000000000);
    }

    #endregion  纳秒级10-9秒


    #region 微秒级10-6秒
    public static void calculate_cudetime_weimiao(this MonoBehaviour mono, string methodname)
    {
        Type t = mono.GetType();

        MethodInfo vMethodInfo = t.GetMethod(methodname);
        if (vMethodInfo == null) return;

        PerformanceTimer timer = new PerformanceTimer();
        timer.Start();
        vMethodInfo.Invoke(mono, null);
        timer.Stop();

        MyDebug.Log(methodname + " 使用时间微秒  " + timer.Duration * 1000000);
    }

    public static void calculate_cudetime_weimiao(this MonoBehaviour mono, string methodname, object pram1)
    {
        Type t = mono.GetType();

        MethodInfo vMethodInfo = t.GetMethod(methodname, new Type[] { pram1.GetType() });
        if (vMethodInfo == null) return;

        PerformanceTimer timer = new PerformanceTimer();
        timer.Start();
        vMethodInfo.Invoke(mono, new object[] { pram1 });
        timer.Stop();

        MyDebug.Log(methodname + " 使用时间微秒  " + timer.Duration * 1000000);
    }

    public static void calculate_cudetime_weimiao(this MonoBehaviour mono, string methodname, object pram1, object pram2)
    {
        Type t = mono.GetType();
        Type p1 = pram1.GetType();
        Type p2 = pram2.GetType();
        MethodInfo vMethodInfo = t.GetMethod(methodname, new Type[] { pram1.GetType(), pram2.GetType() });
        if (vMethodInfo == null) return;
        PerformanceTimer timer = new PerformanceTimer();
        timer.Start();
        vMethodInfo.Invoke(mono, new object[] { pram1, pram2 });
        timer.Stop();

        MyDebug.Log(methodname + " 使用时间微秒  " + timer.Duration * 1000000);
    }

    #endregion 微秒级10-6秒

    #region 毫秒级10-3秒

    public static void calculate_cudetime(this MonoBehaviour mono, string methodname)
    {
        Type t = mono.GetType();

        MethodInfo vMethodInfo = t.GetMethod(methodname);
        if (vMethodInfo == null) return;
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        vMethodInfo.Invoke(mono, null);
        stopWatch.Stop();

        MyDebug.Log(methodname + "   " + stopWatch.ElapsedMilliseconds);
    }

    public static void calculate_cudetime(this MonoBehaviour mono, string methodname, object pram1)
    {
        Type t = mono.GetType();

        MethodInfo vMethodInfo = t.GetMethod(methodname, new Type[] { pram1.GetType() });
        if (vMethodInfo == null) return;
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        vMethodInfo.Invoke(mono, new object[] { pram1 });
        stopWatch.Stop();

        MyDebug.Log(methodname + "  " + stopWatch.ElapsedMilliseconds);
    }

    public static void calculate_cudetime(this MonoBehaviour mono, string methodname, object pram1, object pram2)
    {
        Type t = mono.GetType();
        Type p1 = pram1.GetType();
        Type p2 = pram2.GetType();
        MethodInfo vMethodInfo = t.GetMethod(methodname, new Type[] { pram1.GetType(), pram2.GetType() });
        if (vMethodInfo == null) return;
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        vMethodInfo.Invoke(mono, new object[] { pram1, pram2 });
        stopWatch.Stop();

        MyDebug.Log(methodname + "   " + stopWatch.ElapsedMilliseconds);
    }

    /*
     调用事例  必须是公有方法
     *
     public void fun(int a)
     * {
       }
     * update()
     * {
       if (Input.GetKeyDown(KeyCode.Space))
        {
         //   calculate("Call");

       this.calculate_cudetime("fun",10)
        }
     * }

     */

    #endregion 毫秒级10-3秒

    #endregion 计算代码执行花费的时间
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# TIMER定时激活某个时间/ StopWatch来计算模块计算时间-程序开发-红黑联...
8种主要排序算法的C#实现
8种主要排序算法的C#实现 (二)
MonoTouch 的限制
C#程序员经常用到的10个实用代码片段
C#代码精简优化技巧(上)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服