#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 计算代码执行花费的时间 |
|