分享

【Unity3D之获取某个方法执行的时间】

 鸿蛟家平 2016-05-19
    #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 计算代码执行花费的时间

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多