Skip to content

Instantly share code, notes, and snippets.

@chandlerprall
Forked from mikolalysenko/gist:5311694
Created April 4, 2013 16:19
Show Gist options
  • Save chandlerprall/5311808 to your computer and use it in GitHub Desktop.
Save chandlerprall/5311808 to your computer and use it in GitHub Desktop.
(function() {
'use strict';
var FLOAT_VIEW = new Float32Array( 1 ),
INT_VIEW = new Uint32Array( FLOAT_VIEW.buffer ),
rsqrt = function( number ) {
var x2 = 0.5 * number;
FLOAT_VIEW[0] = number;
INT_VIEW[0] = 0x5f3759df - ( INT_VIEW[0] >>> 1 );
FLOAT_VIEW[0] *= ( 1.5 - ( x2 * FLOAT_VIEW[0] * FLOAT_VIEW[0] ) );
FLOAT_VIEW[0] *= ( 1.5 - ( x2 * FLOAT_VIEW[0] * FLOAT_VIEW[0] ) );
FLOAT_VIEW[0] *= ( 1.5 - ( x2 * FLOAT_VIEW[0] * FLOAT_VIEW[0] ) );
return FLOAT_VIEW[0];
};
var go1 = function( vector ) {
var x;
for ( var i = 0; i < 10000000; i++ ) {
x = rsqrt( i / 100 );
}
},
go2 = function( vector ) {
var x;
for ( var i = 0; i < 10000000; i++ ) {
x = 1 / Math.sqrt( i / 100 );
}
};
var start = Date.now();
go1();
var end = Date.now();
print( 'rsqrt: ' + ( end - start ) );
var start = Date.now();
go2();
var end = Date.now();
print( 'Math.sqrt: ' + ( end - start ) );
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment