Skip to content

bench: refactor random number generation in JS benchmarks for stats/b… #5158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -21,7 +21,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;
@@ -32,17 +33,25 @@ var cdf = require( './../lib' );

bench( pkg, function benchmark( b ) {
var scale;
var len;
var mu;
var x;
var y;
var i;

len = 100;
mu = new Float64Array( len );
scale = new Float64Array( len );
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
mu[ i ] = uniform( -50.0, 100.0 );
x[ i ] = uniform( mu[ i ], 100.0 );
scale[ i ] = uniform( EPS, 20.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
mu = ( randu()*100.0 ) - 50.0;
x = ( randu()*100.0 ) + mu;
scale = ( randu()*20.0 ) + EPS;
y = cdf( x, mu, scale );
y = cdf( x[ i % len ], mu[ i % len], scale[ i % len] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
@@ -58,6 +67,7 @@ bench( pkg, function benchmark( b ) {
bench( pkg+':factory', function benchmark( b ) {
var mycdf;
var scale;
var len;
var mu;
var x;
var y;
@@ -66,11 +76,15 @@ bench( pkg+':factory', function benchmark( b ) {
mu = 0.0;
scale = 1.5;
mycdf = cdf.factory( mu, scale );
len=100;
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 0.0, 4.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = randu() * 4.0;
y = mycdf( x );
y = mycdf( x[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Loading