Skip to content

Commit 0e2afee

Browse files
bench: refactor random value generation
PR-URL: #5374 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 9f7f048 commit 0e2afee

File tree

18 files changed

+107
-91
lines changed

18 files changed

+107
-91
lines changed

lib/node_modules/@stdlib/math/base/special/bernoulli/benchmark/benchmark.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/array/discrete-uniform' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pkg = require( './../package.json' ).name;
2727
var bernoulli = require( './../lib' );
@@ -34,7 +34,7 @@ bench( pkg, function benchmark( b ) {
3434
var y;
3535
var i;
3636

37-
x = randu( 100, 0, 500 );
37+
x = discreteUniform( 100, 0, 500 );
3838

3939
b.tic();
4040
for ( i = 0; i < b.iterations; i++ ) {

lib/node_modules/@stdlib/math/base/special/bernoulli/benchmark/benchmark.native.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/array/discrete-uniform' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
@@ -43,7 +43,7 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4343
var y;
4444
var i;
4545

46-
x = randu( 100, 0, 500 );
46+
x = discreteUniform( 100, 0, 500 );
4747

4848
b.tic();
4949
for ( i = 0; i < b.iterations; i++ ) {

lib/node_modules/@stdlib/math/base/special/bernoulli/benchmark/c/native/benchmark.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ static double rand_double( void ) {
9090
* @return elapsed time in seconds
9191
*/
9292
static double benchmark( void ) {
93-
double elapsed;
9493
double x[ 100 ];
94+
double elapsed;
9595
double y;
9696
double t;
9797
int i;

lib/node_modules/@stdlib/math/base/special/betainc/benchmark/benchmark.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var EPS = require( '@stdlib/constants/float64/eps' );
2727
var pkg = require( './../package.json' ).name;
@@ -37,12 +37,13 @@ bench( pkg, function benchmark( assert ) {
3737
var b;
3838
var i;
3939

40+
x = uniform( 100, 0.0, 1.0 );
41+
a = uniform( 100, EPS, 1000.0 );
42+
b = uniform( 100, EPS, 1000.0 );
43+
4044
assert.tic();
4145
for ( i = 0; i < assert.iterations; i++ ) {
42-
x = randu();
43-
a = ( randu()*1000.0 ) + EPS;
44-
b = ( randu()*1000.0 ) + EPS;
45-
y = betainc( x, a, b );
46+
y = betainc( x[ i % x.length ], a[ i % a.length ], b[ i % b.length ] );
4647
if ( isnan( y ) ) {
4748
assert.fail( 'should not return NaN' );
4849
}

lib/node_modules/@stdlib/math/base/special/betainc/benchmark/c/cephes/benchmark.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,23 @@ static double rand_double( void ) {
9494
* @return elapsed time in seconds
9595
*/
9696
static double benchmark( void ) {
97+
double x[ 100 ];
98+
double a[ 100 ];
99+
double b[ 100 ];
97100
double elapsed;
98-
double x;
99-
double a;
100-
double b;
101101
double y;
102102
double t;
103103
int i;
104104

105+
for ( i = 0; i < 100; i++ ) {
106+
x[ i ] = rand_double();
107+
a[ i ] = ( 1000.0*rand_double() ) + 0.1;
108+
b[ i ] = ( 1000.0*rand_double() ) + 0.1;
109+
}
110+
105111
t = tic();
106112
for ( i = 0; i < ITERATIONS; i++ ) {
107-
x = rand_double();
108-
a = ( 1000.0*rand_double() ) + 0.1;
109-
b = ( 1000.0*rand_double() ) + 0.1;
110-
y = incbet( a, b, x );
113+
y = incbet( a[ i%100 ], b[ i%100 ], x[ i%100 ] );
111114
if ( y != y ) {
112115
printf( "should not return NaN\n" );
113116
break;

lib/node_modules/@stdlib/math/base/special/betainc/test/test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,23 @@ tape( 'main export is a function', function test( t ) {
4949

5050
tape( 'the function returns `NaN` if `x` is outside `[0,1]`', function test( t ) {
5151
var val = betainc( -0.2, 1.0, 1.0 );
52-
t.equal( isnan( val ), true, 'returns NaN' );
52+
t.equal( isnan( val ), true, 'returns expected value' );
5353

5454
val = betainc( 1.1, 1.0, 1.0 );
55-
t.equal( isnan( val ), true, 'returns NaN' );
55+
t.equal( isnan( val ), true, 'returns expected value' );
5656

5757
t.end();
5858
});
5959

6060
tape( 'the function returns `NaN` negative `a` or `b`', function test( t ) {
6161
var val = betainc( 0.5, -1.0, 1.0 );
62-
t.equal( isnan( val ), true, 'returns NaN' );
62+
t.equal( isnan( val ), true, 'returns expected value' );
6363

6464
val = betainc( 0.5, 1.0, -1.0 );
65-
t.equal( isnan( val ), true, 'returns NaN' );
65+
t.equal( isnan( val ), true, 'returns expected value' );
6666

6767
val = betainc( 0.5, -1.0, -1.0 );
68-
t.equal( isnan( val ), true, 'returns NaN' );
68+
t.equal( isnan( val ), true, 'returns expected value' );
6969
t.end();
7070
});
7171

lib/node_modules/@stdlib/math/base/special/betaincinv/benchmark/benchmark.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var EPS = require( '@stdlib/constants/float64/eps' );
2727
var pkg = require( './../package.json' ).name;
@@ -37,12 +37,13 @@ bench( pkg, function benchmark( assert ) {
3737
var b;
3838
var i;
3939

40+
x = uniform( 100, 0.0, 1.0 );
41+
a = uniform( 100, EPS, 1000.0 );
42+
b = uniform( 100, EPS, 1000.0 );
43+
4044
assert.tic();
4145
for ( i = 0; i < assert.iterations; i++ ) {
42-
x = randu();
43-
a = ( randu()*1000.0 ) + EPS;
44-
b = ( randu()*1000.0 ) + EPS;
45-
y = betaincinv( x, a, b );
46+
y = betaincinv( x[ i%x.length ], a[ i%a.length ], b[ i%b.length ] );
4647
if ( isnan( y ) ) {
4748
assert.fail( 'should not return NaN' );
4849
}

lib/node_modules/@stdlib/math/base/special/betaincinv/benchmark/c/cephes/benchmark.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,23 @@ static double rand_double( void ) {
9494
* @return elapsed time in seconds
9595
*/
9696
static double benchmark( void ) {
97+
double x[ 100 ];
98+
double a[ 100 ];
99+
double b[ 100 ];
97100
double elapsed;
98-
double x;
99-
double a;
100-
double b;
101101
double y;
102102
double t;
103103
int i;
104104

105+
for ( i = 0; i < 100; i++ ) {
106+
x[ i ] = rand_double();
107+
a[ i ] = ( 1000.0*rand_double() ) + 0.1;
108+
b[ i ] = ( 1000.0*rand_double() ) + 0.1;
109+
}
110+
105111
t = tic();
106112
for ( i = 0; i < ITERATIONS; i++ ) {
107-
x = rand_double();
108-
a = ( 1000.0*rand_double() ) + 0.1;
109-
b = ( 1000.0*rand_double() ) + 0.1;
110-
y = incbi( a, b, x );
113+
y = incbi( a[ i%100 ], b[ i%100 ], x[ i%100 ] );
111114
if ( y != y ) {
112115
printf( "should not return NaN\n" );
113116
break;

lib/node_modules/@stdlib/math/base/special/betaincinv/test/test.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,48 +49,48 @@ tape( 'main export is a function', function test( t ) {
4949

5050
tape( 'if provided `NaN` for any parameter, the function returns `NaN`', function test( t ) {
5151
var y = betaincinv( NaN, 1.0, 1.0 );
52-
t.equal( isnan( y ), true, 'returns NaN' );
52+
t.equal( isnan( y ), true, 'returns expected value' );
5353

5454
y = betaincinv( 0.2, NaN, 1.0 );
55-
t.equal( isnan( y ), true, 'returns NaN' );
55+
t.equal( isnan( y ), true, 'returns expected value' );
5656

5757
y = betaincinv( 0.2, 1.0, NaN );
58-
t.equal( isnan( y ), true, 'returns NaN' );
58+
t.equal( isnan( y ), true, 'returns expected value' );
5959
t.end();
6060
});
6161

6262
tape( 'the function returns `NaN` if `p` is outside the interval `[0,1]`', function test( t ) {
6363
var y = betaincinv( 1.5, 1.0, 1.0 );
64-
t.equal( isnan( y ), true, 'returns NaN' );
64+
t.equal( isnan( y ), true, 'returns expected value' );
6565

6666
y = betaincinv( -0.5, 1.0, 1.0 );
67-
t.equal( isnan( y ), true, 'returns NaN' );
67+
t.equal( isnan( y ), true, 'returns expected value' );
6868
t.end();
6969
});
7070

7171
tape( 'if provided a nonpositive `a`, the function returns `NaN`', function test( t ) {
7272
var y;
7373

7474
y = betaincinv( 0.5, 0.0, 2.0 );
75-
t.equal( isnan( y ), true, 'returns NaN' );
75+
t.equal( isnan( y ), true, 'returns expected value' );
7676

7777
y = betaincinv( 0.5, -1.0, 2.0 );
78-
t.equal( isnan( y ), true, 'returns NaN' );
78+
t.equal( isnan( y ), true, 'returns expected value' );
7979

8080
y = betaincinv( 0.5, -1.0, 2.0 );
81-
t.equal( isnan( y ), true, 'returns NaN' );
81+
t.equal( isnan( y ), true, 'returns expected value' );
8282

8383
y = betaincinv( 0.5, NINF, 1.0 );
84-
t.equal( isnan( y ), true, 'returns NaN' );
84+
t.equal( isnan( y ), true, 'returns expected value' );
8585

8686
y = betaincinv( 0.5, NINF, PINF );
87-
t.equal( isnan( y ), true, 'returns NaN' );
87+
t.equal( isnan( y ), true, 'returns expected value' );
8888

8989
y = betaincinv( 0.5, NINF, NINF );
90-
t.equal( isnan( y ), true, 'returns NaN' );
90+
t.equal( isnan( y ), true, 'returns expected value' );
9191

9292
y = betaincinv( 0.5, NINF, NaN );
93-
t.equal( isnan( y ), true, 'returns NaN' );
93+
t.equal( isnan( y ), true, 'returns expected value' );
9494

9595
t.end();
9696
});
@@ -99,25 +99,25 @@ tape( 'if provided a nonpositive `b`, the function returns `NaN`', function test
9999
var y;
100100

101101
y = betaincinv( 0.5, 2.0, 0.0 );
102-
t.equal( isnan( y ), true, 'returns NaN' );
102+
t.equal( isnan( y ), true, 'returns expected value' );
103103

104104
y = betaincinv( 0.5, 2.0, -1.0 );
105-
t.equal( isnan( y ), true, 'returns NaN' );
105+
t.equal( isnan( y ), true, 'returns expected value' );
106106

107107
y = betaincinv( 0.5, 2.0, -1/0 );
108-
t.equal( isnan( y ), true, 'returns NaN' );
108+
t.equal( isnan( y ), true, 'returns expected value' );
109109

110110
y = betaincinv( 0.5, 1.0, NINF );
111-
t.equal( isnan( y ), true, 'returns NaN' );
111+
t.equal( isnan( y ), true, 'returns expected value' );
112112

113113
y = betaincinv( 0.5, PINF, NINF );
114-
t.equal( isnan( y ), true, 'returns NaN' );
114+
t.equal( isnan( y ), true, 'returns expected value' );
115115

116116
y = betaincinv( 0.5, NINF, NINF );
117-
t.equal( isnan( y ), true, 'returns NaN' );
117+
t.equal( isnan( y ), true, 'returns expected value' );
118118

119119
y = betaincinv( 0.5, NaN, NINF );
120-
t.equal( isnan( y ), true, 'returns NaN' );
120+
t.equal( isnan( y ), true, 'returns expected value' );
121121

122122
t.end();
123123
});

lib/node_modules/@stdlib/math/base/special/betaln/benchmark/benchmark.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pkg = require( './../package.json' ).name;
2727
var betaln = require( './../lib' );
@@ -35,11 +35,12 @@ bench( pkg, function benchmark( b ) {
3535
var z;
3636
var i;
3737

38+
x = uniform( 100, 0.0, 1000.0 );
39+
y = uniform( 100, 0.0, 1000.0 );
40+
3841
b.tic();
3942
for ( i = 0; i < b.iterations; i++ ) {
40-
x = ( randu()*1000.0 ) - 0.0;
41-
y = ( randu()*1000.0 ) - 0.0;
42-
z = betaln( x, y );
43+
z = betaln( x[ i % x.length ], y[ i % y.length ] );
4344
if ( isnan( z ) ) {
4445
b.fail( 'should not return NaN' );
4546
}

0 commit comments

Comments
 (0)