Skip to content
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

bench: refactor random number generation in stats/base/dists/negative-binomial #5025

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -21,8 +21,9 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var ceil = require( '@stdlib/math/base/special/ceil' );
var randu = require( '@stdlib/random/base/randu' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var uniform = require( '@stdlib/random/base/uniform' );
var Float64Array = require( '@stdlib/array/float64' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;
Expand All @@ -32,18 +33,26 @@ var cdf = require( './../lib' );
// MAIN //

bench( pkg, function benchmark( b ) {
var len;
var r;
var p;
var x;
var y;
var i;

len = 100;
x = new Float64Array( len );
r = new Float64Array( len );
p = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 0, 100.0 );
yuvi-mittal marked this conversation as resolved.
Show resolved Hide resolved
r[ i ] = discreteUniform( 1.0, 100 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r[ i ] = discreteUniform( 1.0, 100 );
r[ i ] = discreteUniform( 1, 100 );

p[ i ] = uniform( EPS, 1.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = randu()*100.0;
r = ceil( randu()*100.0 );
p = ( randu()*1.0 ) + EPS;
y = cdf( x, r, p );
y = cdf( x[ i % len ], r[ i % len ], p[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -58,6 +67,7 @@ bench( pkg, function benchmark( b ) {

bench( pkg+':factory', function benchmark( b ) {
var mycdf;
var len;
var r;
var p;
var x;
Expand All @@ -67,11 +77,15 @@ bench( pkg+':factory', function benchmark( b ) {
r = 80;
p = 0.4;
mycdf = cdf.factory( r, p );
len = 100;
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 0.0, 100.0 );
}

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