Skip to content

bench: update random value generation #6857

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

Merged
merged 1 commit into from
Apr 29, 2025
Merged
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
bench: update random value generation
  • Loading branch information
hrshya committed Apr 29, 2025
commit 2bc7caa37e12fd13bcda6fa6d98693d3d233f179
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var mgf = require( './../lib' );
Expand All @@ -32,26 +31,23 @@ var mgf = require( './../lib' );
// MAIN //

bench( pkg, function benchmark( b ) {
var len;
var opts;
var n;
var p;
var t;
var y;
var i;

len = 100;
t = new Float64Array( len );
n = new Float64Array( len );
p = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
t[ i ] = uniform( 0.0, 5.0 );
n[ i ] = discreteUniform( 1, 100 );
p[ i ] = uniform( 0.0, 1.0 );
}
opts = {
'dtype': 'float64'
};
t = uniform( 100, 0.0, 5.0, opts );
n = discreteUniform( 100, 1, 100, opts );
p = uniform( 100, 0.0, 1.0, opts );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mgf( t[ i % len ], n[ i % len ], p[ i % len ] );
y = mgf( t[ i % t.length ], n[ i % n.length ], p[ i % p.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand All @@ -66,25 +62,25 @@ bench( pkg, function benchmark( b ) {

bench( pkg+':factory', function benchmark( b ) {
var mymgf;
var len;
var opts;
var n;
var p;
var t;
var y;
var i;

opts = {
'dtype': 'float64'
};
t = uniform( 100, 0.0, 5.0, opts );

n = 80;
p = 0.4;
mymgf = mgf.factory( n, p );
len = 100;
t = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
t[ i ] = uniform( 0.0, 5.0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mymgf( t[ i % len ] );
y = mymgf( t[ i % t.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ tape( 'if provided `NaN` for any parameter, the created function returns `NaN`',

mgf = factory( 20, 0.5 );
y = mgf( NaN );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

mgf = factory( NaN, 0.5 );
y = mgf( 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

mgf = factory( 20, NaN );
y = mgf( 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

mgf = factory( NaN, NaN );
y = mgf( 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

mgf = factory( NaN, NaN );
y = mgf( NaN );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});
Expand All @@ -85,22 +85,22 @@ tape( 'if provided an `n` which is not a nonnegative integer, the created functi
mgf = factory( 20.5, 0.5 );

y = mgf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = mgf( 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

mgf = factory( -10, 0.5 );
y = mgf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

mgf = factory( PINF, 0.5 );
y = mgf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

mgf = factory( NINF, 0.5 );
y = mgf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});
Expand All @@ -112,22 +112,22 @@ tape( 'if provided a success probability `p` outside `[0,1]`, the created functi
mgf = factory( 20, 1.5 );

y = mgf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = mgf( 0.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

mgf = factory( 20, -0.5 );
y = mgf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

mgf = factory( 20, PINF );
y = mgf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

mgf = factory( 20, NINF );
y = mgf( 2.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ tape( 'main export is a function', function test( t ) {

tape( 'if provided `NaN` for any parameter, the function returns `NaN`', function test( t ) {
var y = mgf( NaN, 10, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );
y = mgf( 4.0, NaN, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );
y = mgf( 4.0, 10, NaN );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );
t.end();
});

tape( 'if provided an `n` which is not a nonnegative integer, the function returns `NaN`', function test( t ) {
var y;

y = mgf( 2.0, 1.5, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = mgf( 2.0, -2, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = mgf( 2.0, -1, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = mgf( 0.0, 2.5, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = mgf( 0.0, PINF, 0.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});
Expand All @@ -80,16 +80,16 @@ tape( 'if provided a success probability `p` outside of `[0,1]`, the function re
var y;

y = mgf( 2.0, 20, -1.0 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = mgf( 0.0, 20, 1.5 );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = mgf( 2.0, 20, NINF );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

y = mgf( 2.0, 20, PINF );
t.equal( isnan( y ), true, 'returns NaN' );
t.equal( isnan( y ), true, 'returns expected value' );

t.end();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
// MODULES //

var bench = require( '@stdlib/bench' );
var Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var mode = require( './../lib' );
Expand All @@ -33,23 +31,21 @@ var mode = require( './../lib' );
// MAIN //

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

len = 100;
n = new Int32Array( len );
p = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
n[ i ] = discreteUniform( 1, 100 );
p[ i ] = uniform( 0.0, 1.0 );
}
n = discreteUniform( 100, 1, 100, {
'dtype': 'int32'
});
p = uniform( 100, 0.0, 1.0, {
'dtype': 'float64'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mode( n[ i % len ], p[ i % len ] );
y = mode( n[ i % n.length ], p[ i % p.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var Int32Array = require( '@stdlib/array/int32' );
var Float64Array = require( '@stdlib/array/float64' );
var tryRequire = require( '@stdlib/utils/try-require' );
var uniform = require( '@stdlib/random/base/uniform' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var uniform = require( '@stdlib/random/array/uniform' );
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;

Expand All @@ -42,23 +40,21 @@ var opts = {
// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var len;
var n;
var p;
var y;
var i;

len = 100;
n = new Int32Array( len );
p = new Float64Array( len );
for ( i = 0; i < len; i++ ) {
n[ i ] = discreteUniform( 1, 100 );
p[ i ] = uniform( 0.0, 1.0 );
}
n = discreteUniform( 100, 1, 100, {
'dtype': 'int32'
});
p = uniform( 100, 0.0, 1.0, {
'dtype': 'float64'
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = mode( n[ i % len ], p[ i % len ] );
y = mode( n[ i % n.length ], p[ i % p.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ tape( 'main export is a function', function test( t ) {

tape( 'if provided `NaN` for any parameter, the function returns `NaN`', function test( t ) {
var v = mode( NaN, 0.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( 10, NaN );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( NaN, NaN );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

t.end();
});
Expand All @@ -54,19 +54,19 @@ tape( 'if provided an `n` which is not a nonnegative integer, the function retur
var v;

v = mode( 1.5, 0.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( -2, 0.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( -1, 0.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( 2.5, 0.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( PINF, 0.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

t.end();
});
Expand All @@ -75,16 +75,16 @@ tape( 'if provided a success probability `p` outside of `[0,1]`, the function re
var v;

v = mode( 20, -1.0 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( 20, 1.5 );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( 20, NINF );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

v = mode( 20, PINF );
t.equal( isnan( v ), true, 'returns NaN' );
t.equal( isnan( v ), true, 'returns expected value' );

t.end();
});
Expand Down
Loading