Skip to content

Commit

Permalink
bench: refactor random number generation in `stats/base/dists/exponen…
Browse files Browse the repository at this point in the history
…tial`

PR-URL: #4917
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
anandkaranubc authored Jan 26, 2025
1 parent eeb8c95 commit 3250d4f
Show file tree
Hide file tree
Showing 22 changed files with 262 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,15 +33,22 @@ var cdf = require( './../lib' );

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

len = 100;
x = new Float64Array( len );
lambda = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( 0.0, 100.0 );
lambda[ i ] = uniform( EPS, 100.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100.0 );
lambda = ( randu()*100.0 ) + EPS;
y = cdf( x, lambda );
y = cdf( x[ i % len ], lambda[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -56,17 +64,22 @@ bench( pkg, function benchmark( b ) {
bench( pkg+':factory', function benchmark( b ) {
var lambda;
var mycdf;
var len;
var x;
var y;
var i;

lambda = 10.0;
mycdf = cdf.factory( lambda );
len = 100;
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( EPS, 100.0 );
}

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

0 comments on commit 3250d4f

Please sign in to comment.