TestPerformance.cs

  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using LuaInterface;
  5. public class TestPerformance : MonoBehaviour
  6. {
  7. LuaState state = null;
  8. private string tips = "";
  9. void Start ()
  10. {
  11. #if UNITY_5
  12. Application.logMessageReceived += ShowTips;
  13. #else
  14. Application.RegisterLogCallback(ShowTips);
  15. #endif
  16. new LuaResLoader();
  17. state = new LuaState();
  18. state.Start();
  19. LuaBinder.Bind(state);
  20. state.DoFile("TestPerf.lua");
  21. state.LuaGC(LuaGCOptions.LUA_GCCOLLECT);
  22. state.LogGC = false;
  23. Debug.Log(typeof(List<int>).BaseType);
  24. }
  25. void ShowTips(string msg, string stackTrace, LogType type)
  26. {
  27. tips += msg;
  28. tips += "\r\n";
  29. }
  30. void OnApplicationQuit()
  31. {
  32. #if UNITY_5
  33. Application.logMessageReceived -= ShowTips;
  34. #else
  35. Application.RegisterLogCallback(null);
  36. #endif
  37. state.Dispose();
  38. state = null;
  39. }
  40. void OnGUI()
  41. {
  42. GUI.Label(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 100, 400, 300), tips);
  43. if (GUI.Button(new Rect(50, 50, 120, 45), "Test1"))
  44. {
  45. float time = Time.realtimeSinceStartup;
  46. for (int i = 0; i < 200000; i++)
  47. {
  48. Vector3 v = transform.position;
  49. transform.position = v + Vector3.one;
  50. }
  51. time = Time.realtimeSinceStartup - time;
  52. tips = "";
  53. Debugger.Log("c# Transform getset cost time: " + time);
  54. transform.position = Vector3.zero;
  55. LuaFunction func = state.GetFunction("Test1");
  56. func.BeginPCall();
  57. func.Push(transform);
  58. func.PCall();
  59. func.EndPCall();
  60. func.Dispose();
  61. func = null;
  62. }
  63. else if (GUI.Button(new Rect(50, 150, 120, 45), "Test2"))
  64. {
  65. float time = Time.realtimeSinceStartup;
  66. for (int i = 0; i < 200000; i++)
  67. {
  68. transform.Rotate(Vector3.up, 1);
  69. }
  70. time = Time.realtimeSinceStartup - time;
  71. tips = "";
  72. Debugger.Log("c# Transform.Rotate cost time: " + time);
  73. LuaFunction func = state.GetFunction("Test2");
  74. func.BeginPCall();
  75. func.Push(transform);
  76. func.PCall();
  77. func.EndPCall();
  78. func.Dispose();
  79. func = null;
  80. }
  81. else if (GUI.Button(new Rect(50, 250, 120, 45), "Test3"))
  82. {
  83. float time = Time.realtimeSinceStartup;
  84. for (int i = 0; i < 2000000; i++)
  85. {
  86. new Vector3(i, i, i);
  87. }
  88. time = Time.realtimeSinceStartup - time;
  89. tips = "";
  90. Debugger.Log("c# new Vector3 cost time: " + time);
  91. LuaFunction func = state.GetFunction("Test3");
  92. func.Call();
  93. func.Dispose();
  94. func = null;
  95. }
  96. else if (GUI.Button(new Rect(50, 350, 120, 45), "Test4"))
  97. {
  98. float time = Time.realtimeSinceStartup;
  99. for (int i = 0; i < 20000; i++)
  100. {
  101. new GameObject();
  102. }
  103. time = Time.realtimeSinceStartup - time;
  104. tips = "";
  105. Debugger.Log("c# new GameObject cost time: " + time);
  106. //光gc了
  107. LuaFunction func = state.GetFunction("Test4");
  108. func.Call();
  109. func.Dispose();
  110. func = null;
  111. }
  112. else if (GUI.Button(new Rect(50, 450, 120, 45), "Test5"))
  113. {
  114. int[] array = new int[1024];
  115. for (int i = 0; i < 1024; i++)
  116. {
  117. array[i] = i;
  118. }
  119. float time = Time.realtimeSinceStartup;
  120. int total = 0;
  121. for (int j = 0; j < 100000; j++)
  122. {
  123. for (int i = 0; i < 1024; i++)
  124. {
  125. total += array[i];
  126. }
  127. }
  128. time = Time.realtimeSinceStartup - time;
  129. tips = "";
  130. Debugger.Log("Array cost time: " + time);
  131. List<int> list = new List<int>(array);
  132. time = Time.realtimeSinceStartup;
  133. total = 0;
  134. for (int j = 0; j < 100000; j++)
  135. {
  136. for (int i = 0; i < 1024; i++)
  137. {
  138. total += list[i];
  139. }
  140. }
  141. time = Time.realtimeSinceStartup - time;
  142. tips = "";
  143. Debugger.Log("Array cost time: " + time);
  144. LuaFunction func = state.GetFunction("TestTable");
  145. func.Call();
  146. func.Dispose();
  147. func = null;
  148. }
  149. else if (GUI.Button(new Rect(50, 550, 120, 40), "Test7"))
  150. {
  151. float time = Time.realtimeSinceStartup;
  152. Vector3 v1 = Vector3.zero;
  153. for (int i = 0; i < 200000; i++)
  154. {
  155. Vector3 v = new Vector3(i,i,i);
  156. v = Vector3.Normalize(v);
  157. v1 = v + v1;
  158. }
  159. time = Time.realtimeSinceStartup - time;
  160. tips = "";
  161. Debugger.Log("Vector3 New Normalize cost: " + time);
  162. LuaFunction func = state.GetFunction("Test7");
  163. func.Call();
  164. func.Dispose();
  165. func = null;
  166. }
  167. else if (GUI.Button(new Rect(250, 50, 120, 40), "Test8"))
  168. {
  169. float time = Time.realtimeSinceStartup;
  170. for (int i = 0; i < 200000; i++)
  171. {
  172. Quaternion q1 = Quaternion.Euler(i, i, i);
  173. Quaternion q2 = Quaternion.Euler(i * 2, i * 2, i * 2);
  174. Quaternion.Slerp(q1, q2, 0.5f);
  175. }
  176. time = Time.realtimeSinceStartup - time;
  177. tips = "";
  178. Debugger.Log("Quaternion Euler Slerp cost: " + time);
  179. LuaFunction func = state.GetFunction("Test8");
  180. func.Call();
  181. func.Dispose();
  182. func = null;
  183. }
  184. else if (GUI.Button(new Rect(250, 150, 120, 40), "Test9"))
  185. {
  186. tips = "";
  187. LuaFunction func = state.GetFunction("Test9");
  188. func.Call();
  189. func.Dispose();
  190. func = null;
  191. }
  192. else if (GUI.Button(new Rect(250, 250, 120, 40), "Quit"))
  193. {
  194. Application.Quit();
  195. }
  196. state.CheckTop();
  197. state.Collect();
  198. }
  199. }

?