Skip to content

Commit

Permalink
bench: refactor random number generation in benchmarks for `stats/bas…
Browse files Browse the repository at this point in the history
…e/dists/uniform`

PR-URL: stdlib-js#5176
Closes: stdlib-js#4991
Ref: stdlib-js#4837
Ref: stdlib-js#4955

Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com>
Co-authored-by: stdlib-bot <noreply@stdlib.io>
Reviewed-by: Karan Anand <119553199+anandkaranubc@users.noreply.github.com>
Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
  • Loading branch information
3 people authored and ShabiShett07 committed Feb 16, 2025
1 parent 47b6f2d commit 45dd249
Show file tree
Hide file tree
Showing 20 changed files with 296 additions and 118 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 uniform = require( '@stdlib/random/base/uniform' );
var Float64Array = require( '@stdlib/array/float64' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var cdf = require( './../lib' );
Expand All @@ -32,16 +33,24 @@ var cdf = require( './../lib' );
bench( pkg, function benchmark( b ) {
var min;
var max;
var len;
var x;
var y;
var i;

len = 100;
x = new Float64Array( len );
min = new Float64Array( len );
max = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
x[ i ] = uniform( -10.0, 10.0 );
min[ i ] = uniform( -20.0, 0.0 );
max[ i ] = uniform( min[ i ], min[ i ] + 40.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu() * 20.0 ) - 10.0;
min = ( randu() * 20.0 ) - 20.0;
max = min + ( randu() * 40.0 );
y = cdf( x, min, max );
y = cdf( x[ i % len ], min[ i % len ], max[ i % len ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -58,18 +67,23 @@ bench( pkg+':factory', function benchmark( b ) {
var mycdf;
var min;
var max;
var len;
var x;
var y;
var i;

min = -1.5;
max = 1.5;
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
Loading

0 comments on commit 45dd249

Please sign in to comment.