浏览代码

use switch statements in Vector2/3/4.getComponents()/setComponents() based on it being 5-10x faster than index-to-name based on benchmark_vector3components.html

Ben Houston 12 年之前
父节点
当前提交
dafc56fd84
共有 5 个文件被更改,包括 191 次插入42 次删除
  1. 20 13
      src/math/Vector2.js
  2. 22 14
      src/math/Vector3.js
  3. 24 15
      src/math/Vector4.js
  4. 20 0
      test/benchmark/benchmarking_vector3components.html
  5. 105 0
      test/benchmark/core/Vector3Components.js

+ 20 - 13
src/math/Vector2.js

@@ -41,17 +41,29 @@ THREE.Vector2.prototype = {
 
 
 	},
 	},
 
 
-	setComponent: function ( index, value ) {
 
 
-		this[ THREE.Vector2.__indexToName[ index ] ] = value;
+    setComponent: function ( index, value ) {
 
 
-	},
+        switch( index ) {
 
 
-	getComponent: function ( index ) {
+            case 0: this.x = value; break;
+            case 1: this.y = value; break;
+            default: throw new Error( "index is out of range: " + index );
 
 
-		return this[ THREE.Vector2.__indexToName[ index ] ];
-		
-	},
+        }
+
+    },
+
+    getComponent: function ( index ) {
+
+        switch( index ) {
+
+            case 0: return this.x;
+            case 1: return this.y;
+            default: throw new Error( "index is out of range: " + index );
+
+    	}
+    },
 
 
 	copy: function ( v ) {
 	copy: function ( v ) {
 
 
@@ -274,9 +286,4 @@ THREE.Vector2.prototype = {
 
 
 	}
 	}
 
 
-};
-
-THREE.Vector2.__indexToName = {
-  0: 'x',
-  1: 'y'
-};
+};

+ 22 - 14
src/math/Vector3.js

@@ -54,17 +54,31 @@ THREE.Vector3.prototype = {
 
 
 	},
 	},
 
 
-	setComponent: function ( index, value ) {
+    setComponent: function ( index, value ) {
 
 
-		this[ THREE.Vector3.__indexToName[ index ] ] = value;
-		
-	},
+        switch( index ) {
 
 
-	getComponent: function ( index ) {
+            case 0: this.x = value; break;
+            case 1: this.y = value; break;
+            case 2: this.z = value; break;
+            default: throw new Error( "index is out of range: " + index );
 
 
-		return this[ THREE.Vector3.__indexToName[ index ] ];
+        }
 
 
-	},
+    },
+
+    getComponent: function ( index ) {
+
+        switch( index ) {
+
+            case 0: return this.x;
+            case 1: return this.y;
+            case 2: return this.z;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
 
 
 	copy: function ( v ) {
 	copy: function ( v ) {
 
 
@@ -590,10 +604,4 @@ THREE.Vector3.prototype = {
 
 
 	}
 	}
 
 
-};
-
-THREE.Vector3.__indexToName = {
-  0: 'x',
-  1: 'y',
-  2: 'z'
-};
+};

+ 24 - 15
src/math/Vector4.js

@@ -62,17 +62,33 @@ THREE.Vector4.prototype = {
 
 
 	},
 	},
 
 
-	setComponent: function ( index, value ) {
+    setComponent: function ( index, value ) {
 
 
-		this[ THREE.Vector4.__indexToName[ index ] ] = value;
+        switch( index ) {
 
 
-	},
+            case 0: this.x = value; break;
+            case 1: this.y = value; break;
+            case 2: this.z = value; break;
+            case 3: this.w = value; break;
+            default: throw new Error( "index is out of range: " + index );
 
 
-	getComponent: function ( index ) {
+        }
 
 
-		return this[ THREE.Vector4.__indexToName[ index ] ];
-		
-	},
+    },
+
+    getComponent: function ( index ) {
+
+        switch( index ) {
+
+            case 0: return this.x;
+            case 1: return this.y;
+            case 2: return this.z;
+            case 3: return this.w;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
 
 
 	copy: function ( v ) {
 	copy: function ( v ) {
 
 
@@ -505,11 +521,4 @@ THREE.Vector4.prototype = {
 
 
 	}
 	}
 
 
-};
-
-THREE.Vector4.__indexToName = {
-  0: 'x',
-  1: 'y',
-  2: 'z',
-  3: 'w'
-};
+};

+ 20 - 0
test/benchmark/benchmarking_vector3components.html

@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>ThreeJS Benchmark Tests - Using Files in /src</title>
+</head>
+<body>
+
+  During this Benchmarking test the browser will be unresponsive.<br/><br/>
+
+  Benchmark output is written to the JavaScript console.  To access the JavaScript console presss Ctrl-Shift-J.
+
+  <script src="benchmark-1.0.0.js"></script>
+
+  <!-- add class-based unit tests below -->
+
+  <script src="core/Vector3Components.js"></script>
+  
+</body>
+</html>

+ 105 - 0
test/benchmark/core/Vector3Components.js

@@ -0,0 +1,105 @@
+THREE = {};
+    
+THREE.Vector3 = function ( x, y, z ) {
+
+    this.x = x || 0;
+    this.y = y || 0;
+    this.z = z || 0;
+
+};
+
+THREE.Vector3.prototype = {
+
+    constructor: THREE.Vector3,
+ 
+    setComponent: function ( index, value ) {
+
+        this[ THREE.Vector3.__indexToName[ index ] ] = value;
+        
+    },
+
+    getComponent: function ( index ) {
+
+        return this[ THREE.Vector3.__indexToName[ index ] ];
+
+    }, 
+
+
+
+    setComponent2: function ( index, value ) {
+
+        switch( index ) {
+
+            case 0: this.x = value; break;
+            case 1: this.y = value; break;
+            case 2: this.z = value; break;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
+
+    getComponent2: function ( index ) {
+
+        switch( index ) {
+
+            case 0: return this.x;
+            case 1: return this.y;
+            case 2: return this.z;
+            default: throw new Error( "index is out of range: " + index );
+
+        }
+
+    },
+};
+
+
+THREE.Vector3.__indexToName = {
+  0: 'x',
+  1: 'y',
+  2: 'z'
+};
+
+var a = [];
+
+for ( var i = 0; i < 100000; i ++ ) {
+    
+    a[ i ] = new THREE.Vector3( i * 0.01, i * 2, i * -1.3 );
+    
+}
+
+
+var suite = new Benchmark.Suite;
+
+suite.add('IndexToName', function() {
+
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+        result += a[i].getComponent( i % 3 );
+    }
+
+});
+
+suite.add('SwitchStatement', function() {
+
+    var result = 0;
+
+    for ( var i = 0; i < 100000; i ++ ) {
+
+        result += a[i].getComponent2( i % 3 );
+
+    }
+
+});
+
+suite.on('cycle', function(event, bench) {
+  console.log(String(event.target));
+});
+
+suite.on('complete', function() {
+  console.log('Fastest is ' + this.filter('fastest').pluck('name'));
+  console.log( "Done" );
+});
+
+suite.run(true);