vec_array_perf.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2011 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  10. <title>Closure Performance Tests - Vector Array math</title>
  11. <link rel="stylesheet" type="text/css"
  12. href="../testing/performancetable.css"/>
  13. <script type="text/javascript" src="../base.js"></script>
  14. <script type="text/javascript">
  15. goog.require('goog.testing.jsunit');
  16. goog.require('goog.testing.PerformanceTable');
  17. goog.require('goog.vec.Vec4');
  18. goog.require('goog.vec.Mat4');
  19. </script>
  20. </head>
  21. <body>
  22. <h1>Closure Performance Tests - Vector Array Math</h1>
  23. <p>
  24. <strong>User-agent:</strong>
  25. <script type="text/javascript">document.write(navigator.userAgent);</script>
  26. </p>
  27. <p>
  28. These tests compare various methods of performing vector operations on
  29. arrays of vectors.
  30. </p>
  31. <div id="perfTable"></div>
  32. <hr>
  33. <script type="text/javascript">
  34. var table = new goog.testing.PerformanceTable(
  35. goog.dom.getElement('perfTable'));
  36. function createRandomFloat32Array(length) {
  37. var array = new Float32Array(length);
  38. for (var i = 0; i < length; i++) {
  39. array[i] = Math.random();
  40. }
  41. return array;
  42. }
  43. function createRandomIndexArray(length) {
  44. var array = [];
  45. for (var i = 0; i < length; i++) {
  46. array[i] = Math.floor(Math.random() * length);
  47. array[i] = Math.min(length - 1, array[i]);
  48. }
  49. return array;
  50. }
  51. function createRandomVec4Array(length) {
  52. var a = [];
  53. for (var i = 0; i < length; i++) {
  54. a[i] = goog.vec.Vec4.createFromValues(
  55. Math.random(), Math.random(), Math.random(), Math.random());
  56. }
  57. return a;
  58. }
  59. function createRandomMat4() {
  60. var m = goog.vec.Mat4.createFromValues(
  61. Math.random(), Math.random(), Math.random(), Math.random(),
  62. Math.random(), Math.random(), Math.random(), Math.random(),
  63. Math.random(), Math.random(), Math.random(), Math.random(),
  64. Math.random(), Math.random(), Math.random(), Math.random());
  65. return m;
  66. }
  67. function createRandomMat4Array(length) {
  68. var m = [];
  69. for (var i = 0; i < length; i++) {
  70. m[i] = createRandomMat4();
  71. }
  72. return m;
  73. }
  74. /**
  75. * Vec4Object is a 4-vector object with x,y,z,w components.
  76. * @param {number} x The x component.
  77. * @param {number} y The y component.
  78. * @param {number} z The z component.
  79. * @param {number} w The w component.
  80. * @constructor
  81. */
  82. Vec4Object = function(x, y, z, w) {
  83. this.x = x;
  84. this.y = y;
  85. this.z = z;
  86. this.w = w;
  87. };
  88. /**
  89. * Add two vectors.
  90. * @param {Vec4Object} v0 A vector.
  91. * @param {Vec4Object} v1 Another vector.
  92. * @param {Vec4Object} r The result.
  93. */
  94. Vec4Object.add = function(v0, v1, r) {
  95. r.x = v0.x + v1.x;
  96. r.y = v0.y + v1.y;
  97. r.z = v0.z + v1.z;
  98. r.w = v0.w + v1.w;
  99. };
  100. function createRandomVec4ObjectArray(length) {
  101. var a = [];
  102. for (var i = 0; i < length; i++) {
  103. a[i] = new Vec4Object(
  104. Math.random(), Math.random(), Math.random(), Math.random());
  105. }
  106. return a;
  107. }
  108. function setVec4FromArray(v, a, o) {
  109. v[0] = a[o + 0];
  110. v[1] = a[o + 1];
  111. v[2] = a[o + 2];
  112. v[3] = a[o + 3];
  113. }
  114. function setArrayFromVec4(a, o, v) {
  115. a[o + 0] = v[0];
  116. a[o + 1] = v[1];
  117. a[o + 2] = v[2];
  118. a[o + 3] = v[3];
  119. }
  120. /**
  121. * This is the same as goog.vec.Vec4.add(). Use this to avoid namespace lookup
  122. * overheads.
  123. * @param {goog.vec.Vec4.Vec4Like} v0 A vector.
  124. * @param {goog.vec.Vec4.Vec4Like} v1 Another vector.
  125. * @param {goog.vec.Vec4.Vec4Like} r The result.
  126. */
  127. function addVec4(v0, v1, r) {
  128. r[0] = v0[0] + v1[0];
  129. r[1] = v0[1] + v1[1];
  130. r[2] = v0[2] + v1[2];
  131. r[3] = v0[3] + v1[3];
  132. }
  133. function addVec4ByOffset(v0Buf, v0Off, v1Buf, v1Off, rBuf, rOff) {
  134. rBuf[rOff + 0] = v0Buf[v0Off + 0] + v1Buf[v1Off + 0];
  135. rBuf[rOff + 1] = v0Buf[v0Off + 1] + v1Buf[v1Off + 1];
  136. rBuf[rOff + 2] = v0Buf[v0Off + 2] + v1Buf[v1Off + 2];
  137. rBuf[rOff + 3] = v0Buf[v0Off + 3] + v1Buf[v1Off + 3];
  138. }
  139. function addVec4ByOptionalOffset(v0, v1, r, opt_v0Off, opt_v1Off, opt_rOff) {
  140. if (opt_v0Off && opt_v1Off && opt_rOff) {
  141. r[opt_rOff + 0] = v0[opt_v0Off + 0] + v1[opt_v1Off + 0];
  142. r[opt_rOff + 1] = v0[opt_v0Off + 1] + v1[opt_v1Off + 1];
  143. r[opt_rOff + 2] = v0[opt_v0Off + 2] + v1[opt_v1Off + 2];
  144. r[opt_rOff + 3] = v0[opt_v0Off + 3] + v1[opt_v1Off + 3];
  145. } else {
  146. r[0] = v0[0] + v1[0];
  147. r[1] = v0[1] + v1[1];
  148. r[2] = v0[2] + v1[2];
  149. r[3] = v0[3] + v1[3];
  150. }
  151. }
  152. function mat4MultVec4ByOffset(mBuf, mOff, vBuf, vOff, rBuf, rOff) {
  153. var x = vBuf[vOff + 0], y = vBuf[vOff + 1],
  154. z = vBuf[vOff + 2], w = vBuf[vOff + 3];
  155. rBuf[rOff + 0] = x * mBuf[mOff + 0] + y * mBuf[mOff + 4] +
  156. z * mBuf[mOff + 8] + w * mBuf[mOff + 12];
  157. rBuf[rOff + 1] = x * mBuf[mOff + 1] + y * mBuf[mOff + 5] +
  158. z * mBuf[mOff + 9] + w * mBuf[mOff + 13];
  159. rBuf[rOff + 2] = x * mBuf[mOff + 2] + y * mBuf[mOff + 6] +
  160. z * mBuf[mOff + 10] + w * mBuf[mOff + 14];
  161. rBuf[rOff + 3] = x * mBuf[mOff + 3] + y * mBuf[mOff + 7] +
  162. z * mBuf[mOff + 11] + w * mBuf[mOff + 15];
  163. }
  164. var NUM_ITERATIONS = 200000;
  165. function testAddVec4ByOffset() {
  166. var nVecs = NUM_ITERATIONS;
  167. var nVals = nVecs * 4;
  168. var a0 = createRandomFloat32Array(nVals);
  169. var a1 = createRandomFloat32Array(nVals);
  170. var a2 = new Float32Array(nVals);
  171. table.run(
  172. function() {
  173. for (var i = 0; i < nVecs; i++) {
  174. addVec4ByOffset(a0, i * 4, a1, i * 4, a2, i * 4);
  175. }
  176. },
  177. 'Add vectors using offsets');
  178. }
  179. function testAddVec4ByOptionalOffset() {
  180. var nVecs = NUM_ITERATIONS;
  181. var nVals = nVecs * 4;
  182. var a0 = createRandomFloat32Array(nVals);
  183. var a1 = createRandomFloat32Array(nVals);
  184. var a2 = new Float32Array(nVals);
  185. table.run(
  186. function() {
  187. for (var i = 0; i < nVecs; i++) {
  188. addVec4ByOptionalOffset(a0, a1, a2, i * 4, i * 4, i * 4);
  189. }
  190. },
  191. 'Add vectors with optional offsets (requires branch)');
  192. }
  193. /**
  194. * Check the overhead of using an array of individual
  195. * Vec4s (Float32Arrays of length 4).
  196. */
  197. function testAddVec4ByVec4s() {
  198. var nVecs = NUM_ITERATIONS;
  199. var a0 = createRandomVec4Array(nVecs);
  200. var a1 = createRandomVec4Array(nVecs);
  201. var a2 = createRandomVec4Array(nVecs);
  202. table.run(
  203. function() {
  204. for (var i = 0; i < nVecs; i++) {
  205. addVec4(a0[i], a1[i], a2[i]);
  206. }
  207. },
  208. 'Add vectors using an array of Vec4s (Float32Arrays of length 4)');
  209. }
  210. function testAddVec4ByTmp() {
  211. var nVecs = NUM_ITERATIONS;
  212. var nVals = nVecs * 4;
  213. var a0 = createRandomFloat32Array(nVals);
  214. var a1 = createRandomFloat32Array(nVals);
  215. var a2 = new Float32Array(nVals);
  216. table.run(
  217. function() {
  218. var t0 = new Float32Array(4);
  219. var t1 = new Float32Array(4);
  220. for (var i = 0; i < nVecs; i++) {
  221. setVec4FromArray(t0, a0, i * 4);
  222. setVec4FromArray(t1, a1, i * 4);
  223. addVec4(t0, t1, t0);
  224. setArrayFromVec4(a2, i * 4, t0);
  225. }
  226. },
  227. 'Add vectors using tmps');
  228. }
  229. /**
  230. * Check the overhead of using an array of Objects with the implicit hash
  231. * lookups for the x,y,z,w components.
  232. */
  233. function testAddVec4ByObjects() {
  234. var nVecs = NUM_ITERATIONS;
  235. var a0 = createRandomVec4ObjectArray(nVecs);
  236. var a1 = createRandomVec4ObjectArray(nVecs);
  237. var a2 = createRandomVec4ObjectArray(nVecs);
  238. table.run(
  239. function() {
  240. for (var i = 0; i < nVecs; i++) {
  241. Vec4Object.add(a0[i], a1[i], a2[i]);
  242. }
  243. },
  244. 'Add vectors using an array of Objects ' +
  245. '(with implicit hash lookups for the x,y,z,w components)');
  246. }
  247. function testAddVec4BySubarray() {
  248. var nVecs = NUM_ITERATIONS;
  249. var nVals = nVecs * 4;
  250. var a0 = createRandomFloat32Array(nVals);
  251. var a1 = createRandomFloat32Array(nVals);
  252. var a2 = new Float32Array(nVals);
  253. table.run(
  254. function() {
  255. for (var i = 0; i < nVecs; i++) {
  256. var t0 = a0.subarray(i * 4 * 4);
  257. var t1 = a1.subarray(i * 4 * 4);
  258. var t2 = a2.subarray(i * 4 * 4);
  259. addVec4(t0, t1, t2);
  260. }
  261. },
  262. 'Add vectors using Float32Array.subarray()');
  263. }
  264. function testAddVec4ByView() {
  265. var nVecs = NUM_ITERATIONS;
  266. var nVals = nVecs * 4;
  267. var a0 = createRandomFloat32Array(nVals);
  268. var a1 = createRandomFloat32Array(nVals);
  269. var a2 = new Float32Array(nVals);
  270. table.run(
  271. function() {
  272. for (var i = 0; i < nVecs; i++) {
  273. var t0 = new Float32Array(a0.buffer, i * 4 * 4);
  274. var t1 = new Float32Array(a1.buffer, i * 4 * 4);
  275. var t2 = new Float32Array(a2.buffer, i * 4 * 4);
  276. addVec4(t0, t1, t2);
  277. }
  278. },
  279. 'Add vectors using Float32 view');
  280. }
  281. function testMat4MultVec4ByOffset() {
  282. var nVecs = NUM_ITERATIONS;
  283. var nVecVals = nVecs * 4;
  284. var nMatVals = nVecs * 16;
  285. var m = createRandomFloat32Array(nMatVals);
  286. var a0 = createRandomFloat32Array(nVecVals);
  287. var a1 = new Float32Array(nVecVals);
  288. table.run(
  289. function() {
  290. for (var i = 0; i < nVecs; i++) {
  291. mat4MultVec4ByOffset(m, i * 16, a0, i * 4, a1, i * 4);
  292. }
  293. },
  294. 'vec4 = mat4 * vec4 using offsets.');
  295. }
  296. /**
  297. * Check the overhead of using an array of individual
  298. * Vec4s (Float32Arrays of length 4).
  299. */
  300. function testMat4MultVec4ByVec4s() {
  301. var nVecs = NUM_ITERATIONS;
  302. var a0 = createRandomVec4Array(nVecs);
  303. var a1 = createRandomVec4Array(nVecs);
  304. var m = createRandomMat4Array(nVecs);
  305. table.run(
  306. function() {
  307. for (var i = 0; i < nVecs; i++) {
  308. goog.vec.Mat4.multVec4(m[i], a0[i], a1[i]);
  309. }
  310. },
  311. 'vec4 = mat4 * vec4 using arrays of Vec4s and Mat4s');
  312. }
  313. /**
  314. * Do 10x as many for the one vector tests.
  315. * @type {number}
  316. */
  317. var NUM_ONE_ITERATIONS = NUM_ITERATIONS * 10;
  318. function testAddOneVec4ByOffset() {
  319. var a0 = createRandomFloat32Array(4);
  320. var a1 = createRandomFloat32Array(4);
  321. var a2 = new Float32Array(4);
  322. table.run(
  323. function() {
  324. for (var i = 0; i < NUM_ONE_ITERATIONS; i++) {
  325. addVec4ByOffset(a0, 0, a1, 0, a2, 0);
  326. }
  327. },
  328. 'Add one vector using offset of 0');
  329. }
  330. function testAddOneVec4() {
  331. var a0 = createRandomFloat32Array(4);
  332. var a1 = createRandomFloat32Array(4);
  333. var a2 = new Float32Array(4);
  334. table.run(
  335. function() {
  336. for (var i = 0; i < NUM_ONE_ITERATIONS; i++) {
  337. addVec4(a0, a1, a2);
  338. }
  339. },
  340. 'Add one vector');
  341. }
  342. function testAddOneVec4ByOptionalOffset() {
  343. var a0 = createRandomFloat32Array(4);
  344. var a1 = createRandomFloat32Array(4);
  345. var a2 = new Float32Array(4);
  346. table.run(
  347. function() {
  348. for (var i = 0; i < NUM_ONE_ITERATIONS; i++) {
  349. addVec4ByOptionalOffset(a0, a1, a2);
  350. }
  351. },
  352. 'Add one vector with optional offsets (requires branch)');
  353. }
  354. function testAddRandomVec4ByOffset() {
  355. var nVecs = NUM_ITERATIONS;
  356. var nVals = nVecs * 4;
  357. var a0 = createRandomFloat32Array(nVals);
  358. var a1 = createRandomFloat32Array(nVals);
  359. var a2 = new Float32Array(nVals);
  360. var i0 = createRandomIndexArray(nVecs);
  361. var i1 = createRandomIndexArray(nVecs);
  362. var i2 = createRandomIndexArray(nVecs);
  363. table.run(
  364. function() {
  365. for (var i = 0; i < nVecs; i++) {
  366. addVec4ByOffset(a0, i0[i] * 4, a1, i1[i] * 4, a2, i2[i] * 4);
  367. }
  368. },
  369. 'Add random vectors using offsets');
  370. }
  371. function testAddRandomVec4ByVec4s() {
  372. var nVecs = NUM_ITERATIONS;
  373. var a0 = createRandomVec4Array(nVecs);
  374. var a1 = createRandomVec4Array(nVecs);
  375. var a2 = createRandomVec4Array(nVecs);
  376. var i0 = createRandomIndexArray(nVecs);
  377. var i1 = createRandomIndexArray(nVecs);
  378. var i2 = createRandomIndexArray(nVecs);
  379. table.run(
  380. function() {
  381. for (var i = 0; i < nVecs; i++) {
  382. addVec4(a0[i0[i]], a1[i1[i]], a2[i2[i]]);
  383. }
  384. },
  385. 'Add random vectors using an array of Vec4s');
  386. }
  387. // Make sure the tests are run in the order they are defined.
  388. var testCase = new goog.testing.TestCase(document.title);
  389. testCase.order = goog.testing.TestCase.Order.NATURAL;
  390. testCase.autoDiscoverTests();
  391. G_testRunner.initialize(testCase);
  392. </script>
  393. </body>
  394. </html>