Skip to content

Commit

Permalink
bench: refactor random number generation in stats/base/dists/chisquare
Browse files Browse the repository at this point in the history
PR-URL: #4860
Reviewed-by: Athan Reines <[email protected]>
  • Loading branch information
anandkaranubc authored Jan 26, 2025
1 parent 247af42 commit 02b9077
Show file tree
Hide file tree
Showing 21 changed files with 250 additions and 89 deletions.
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 Float64Array = require( '@stdlib/array/float64' );
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 @@ -31,16 +32,23 @@ var cdf = require( './../lib' );
// MAIN //

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

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = randu() * 100.0;
k = ceil( randu()*100.0 );
y = cdf( x, k );
y = cdf( x[ i % len ], k[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -55,18 +63,23 @@ bench( pkg, function benchmark( b ) {

bench( pkg+':factory', function benchmark( b ) {
var mycdf;
var len;
var k;
var x;
var y;
var i;

k = 10.0;
mycdf = cdf.factory( k );
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.0;
y = mycdf( x );
y = mycdf( x[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Loading

0 comments on commit 02b9077

Please sign in to comment.