Vector3Length.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. THREE = {};
  2. THREE.Vector3 = function ( x, y, z ) {
  3. this.x = x || 0;
  4. this.y = y || 0;
  5. this.z = z || 0;
  6. };
  7. THREE.Vector3.prototype = {
  8. constructor: THREE.Vector3,
  9. lengthSq: function () {
  10. return this.x * this.x + this.y * this.y + this.z * this.z;
  11. },
  12. length: function () {
  13. return Math.sqrt( this.lengthSq() );
  14. },
  15. length2: function () {
  16. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  17. }
  18. };
  19. var a = [];
  20. for ( var i = 0; i < 100000; i ++ ) {
  21. a[ i ] = new THREE.Vector3( i * 0.01, i * 2, i * -1.3 );
  22. }
  23. var suite = new Benchmark.Suite;
  24. suite.add('NoCallTest', function() {
  25. var result = 0;
  26. for ( var i = 0; i < 100000; i ++ ) {
  27. var v = a[i];
  28. result += Math.sqrt( v.x * v.x + v.y * v.y + v.z * v.z );
  29. }
  30. });
  31. suite.add('InlineCallTest', function() {
  32. var result = 0;
  33. for ( var i = 0; i < 100000; i ++ ) {
  34. result += a[ i ].length2();
  35. }
  36. });
  37. suite.add('FunctionCallTest', function() {
  38. var result = 0;
  39. for ( var i = 0; i < 100000; i ++ ) {
  40. result += a[ i ].length();
  41. }
  42. });
  43. suite.on('cycle', function(event, bench) {
  44. console.log(String(event.target));
  45. });
  46. suite.on('complete', function() {
  47. console.log('Fastest is ' + this.filter('fastest').pluck('name'));
  48. console.log( "Done" );
  49. });
  50. suite.run(true);