Skip to content

Commit

Permalink
bench: refactor random number generation in `stats/base/dists/discret…
Browse files Browse the repository at this point in the history
…e-uniform`

PR-URL: #4887
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
anandkaranubc authored Jan 26, 2025
1 parent 906a39e commit 13c0b92
Show file tree
Hide file tree
Showing 23 changed files with 291 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var cdf = require( './../lib' );
Expand All @@ -44,9 +44,9 @@ bench( pkg, function benchmark( b ) {
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = ( randu() * 20.0 ) - 10.0;
min[ i ] = round( randu()*10.0 );
max[ i ] = round( randu() * 10.0 ) + min[ i ];
x[ i ] = uniform( -10.0, 10.0 );
min[ i ] = discreteUniform( 0, 10 );
max[ i ] = discreteUniform( min[ i ], min[ i ] + 10 );
}

b.tic();
Expand All @@ -68,18 +68,23 @@ bench( pkg+':factory', function benchmark( b ) {
var mycdf;
var min;
var max;
var len;
var x;
var y;
var i;

min = -2;
max = 2;
mycdf = cdf.factory( min, max );
len = 100;
x = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( -2.0, 0.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*2.0 ) - 2.0;
y = mycdf( x );
y = mycdf( x[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;
Expand Down Expand Up @@ -53,9 +53,9 @@ bench( pkg+'::native', opts, function benchmark( b ) {
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = ( randu() * 20.0 ) - 10.0;
min[ i ] = round( randu()*10.0 );
max[ i ] = round( randu() * 10.0 ) + min[ i ];
x[ i ] = uniform( -10.0, 10.0 );
min[ i ] = discreteUniform( 0, 10 );
max[ i ] = discreteUniform( min[ i ], min[ i ] + 10 );
}

b.tic();
Expand Down
Loading

0 comments on commit 13c0b92

Please sign in to comment.