-
-
Save chandlerprall/5311808 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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