Skip to content

Commit 9b2d78d

Browse files
bench: refactor random value generation
PR-URL: #5465 Co-authored-by: stdlib-bot <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Karan Anand <[email protected]>
1 parent 394de5a commit 9b2d78d

File tree

29 files changed

+148
-122
lines changed

29 files changed

+148
-122
lines changed

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

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

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
25-
var floor = require( '@stdlib/math/base/special/floor' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2625
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2726
var pkg = require( './../package.json' ).name;
2827
var binet = require( './../lib' );
@@ -35,10 +34,11 @@ bench( pkg, function benchmark( b ) {
3534
var y;
3635
var i;
3736

37+
x = discreteUniform( 100, 0, 79 );
38+
3839
b.tic();
3940
for ( i = 0; i < b.iterations; i++ ) {
40-
x = floor( randu()*79.0 );
41-
y = binet( x );
41+
y = binet( x[ i%x.length ] );
4242
if ( isnan( y ) ) {
4343
b.fail( 'should not return NaN' );
4444
}

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

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

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
26-
var floor = require( '@stdlib/math/base/special/floor' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2726
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2827
var tryRequire = require( '@stdlib/utils/try-require' );
2928
var pkg = require( './../package.json' ).name;
@@ -44,10 +43,11 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4443
var y;
4544
var i;
4645

46+
x = discreteUniform( 100, 0, 79 );
47+
4748
b.tic();
4849
for ( i = 0; i < b.iterations; i++ ) {
49-
x = floor( randu() * 79.0 );
50-
y = binet( x );
50+
y = binet( x[ i%x.length ] );
5151
if ( isnan( y ) ) {
5252
b.fail( 'should not return NaN' );
5353
}

lib/node_modules/@stdlib/math/base/special/binet/benchmark/c/benchmark.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
#include "stdlib/math/base/special/binet.h"
1920
#include <stdlib.h>
2021
#include <stdio.h>
2122
#include <math.h>
@@ -99,16 +100,19 @@ double binet( double x ) {
99100
* @return elapsed time in seconds
100101
*/
101102
static double benchmark( void ) {
103+
double x[ 100 ];
102104
double elapsed;
103105
double t;
104-
double x;
105106
double y;
106107
int i;
107108

109+
for ( i = 0; i < 100; i++ ) {
110+
x[ i ] = floor( 79.0*rand_double() );
111+
}
112+
108113
t = tic();
109114
for ( i = 0; i < ITERATIONS; i++ ) {
110-
x = floor( 79.0*rand_double() );
111-
y = binet( x );
115+
y = stdlib_base_binet( x[ i%100 ] );
112116
if ( y < 0 ) {
113117
printf( "should return a nonnegative number\n" );
114118
break;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,19 @@ static double rand_double( void ) {
9090
* @return elapsed time in seconds
9191
*/
9292
static double benchmark( void ) {
93+
double x[ 100 ];
9394
double elapsed;
94-
double x;
9595
double y;
9696
double t;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
x[ i ] = floor( 79.0*rand_double() );
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = floor( 79.0 * rand_double() );
102-
y = stdlib_base_binet( x );
105+
y = stdlib_base_binet( x[ i%100 ] );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

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

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

4242
tape( 'if provided `NaN`, the function returns `NaN`', function test( t ) {
4343
var v = binet( NaN );
44-
t.strictEqual( isnan( v ), true, 'returns NaN when provided a NaN' );
44+
t.strictEqual( isnan( v ), true, 'returns expected value' );
4545
t.end();
4646
});
4747

4848
tape( 'if provided `+infinity`, the function returns `NaN`', function test( t ) {
4949
var v = binet( PINF );
50-
t.strictEqual( isnan( v ), true, 'returns NaN' );
50+
t.strictEqual( isnan( v ), true, 'returns expected value' );
5151
t.end();
5252
});
5353

5454
tape( 'if provided `-infinity`, the function returns `NaN`', function test( t ) {
5555
var v = binet( NINF );
56-
t.strictEqual( isnan( v ), true, 'returns NaN' );
56+
t.strictEqual( isnan( v ), true, 'returns expected value' );
5757
t.end();
5858
});
5959

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,13 @@ bench( pkg, function benchmark( b ) {
3434
var y;
3535
var z;
3636
var i;
37-
var j;
3837

3938
x = discreteUniform( 100, 20, 70 );
40-
y = discreteUniform( x.length, 0, 20 );
39+
y = discreteUniform( 100, 0, 20 );
4140

4241
b.tic();
4342
for ( i = 0; i < b.iterations; i++ ) {
44-
j = i % x.length;
45-
z = binomcoef( x[ j ], y[ j ] );
43+
z = binomcoef( x[ i%x.length ], y[ i%y.length ] );
4644
if ( isnan( z ) ) {
4745
b.fail( 'should not return NaN' );
4846
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,13 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4343
var y;
4444
var z;
4545
var i;
46-
var j;
4746

4847
x = discreteUniform( 100, 20, 70 );
49-
y = discreteUniform( x.length, 0, 20 );
48+
y = discreteUniform( 100, 0, 20 );
5049

5150
b.tic();
5251
for ( i = 0; i < b.iterations; i++ ) {
53-
j = i % x.length;
54-
z = binomcoef( x[ j ], y[ j ] );
52+
z = binomcoef( x[ i%x.length ], y[ i%y.length ] );
5553
if ( isnan( z ) ) {
5654
b.fail( 'should not return NaN' );
5755
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,21 @@ static double rand_double( void ) {
9090
* @return elapsed time in seconds
9191
*/
9292
static double benchmark( void ) {
93+
int64_t n[ 100 ];
94+
int64_t k[ 100 ];
9395
double elapsed;
94-
int64_t n;
95-
int64_t k;
9696
double y;
9797
double t;
9898
int i;
9999

100+
for ( i = 0; i < 100; i++ ) {
101+
n[ i ] = (int64_t)round( 500.0 * rand_double() );
102+
k[ i ] = (int64_t)round( 500.0 * rand_double() );
103+
}
104+
100105
t = tic();
101106
for ( i = 0; i < ITERATIONS; i++ ) {
102-
n = (int64_t)round( 500.0 * rand_double() );
103-
k = (int64_t)round( 500.0 * rand_double() );
104-
y = stdlib_base_binomcoef( n, k );
107+
y = stdlib_base_binomcoef( n[ i%100 ], k[ i%100 ] );
105108
if ( y != y ) {
106109
printf( "should not return NaN\n" );
107110
break;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ tape( 'main export is a function', function test( t ) {
4343

4444
tape( 'the function returns `NaN` if provided `NaN` for any parameter', function test( t ) {
4545
var v = binomcoef( 3, NaN );
46-
t.strictEqual( isnan( v ), true, 'returns NaN' );
46+
t.strictEqual( isnan( v ), true, 'returns expected value' );
4747

4848
v = binomcoef( NaN, 2 );
49-
t.strictEqual( isnan( v ), true, 'returns NaN' );
49+
t.strictEqual( isnan( v ), true, 'returns expected value' );
5050

5151
t.end();
5252
});
@@ -101,7 +101,7 @@ tape( 'the function returns `NaN` if the `n` value is not an integer', function
101101
];
102102

103103
for ( i = 0; i < values.length; i++ ) {
104-
t.strictEqual( isnan( binomcoef( values[i], 2 ) ), true, 'returns NaN when provided '+values[i] );
104+
t.strictEqual( isnan( binomcoef( values[i], 2 ) ), true, 'returns expected value when provided '+values[i] );
105105
}
106106
t.end();
107107
});
@@ -122,7 +122,7 @@ tape( 'the function returns `NaN` if the `k` value is not an integer', function
122122
];
123123

124124
for ( i = 0; i < values.length; i++ ) {
125-
t.strictEqual( isnan( binomcoef( 2, values[i] ) ), true, 'returns NaN when provided '+values[i] );
125+
t.strictEqual( isnan( binomcoef( 2, values[i] ) ), true, 'returns expected value when provided '+values[i] );
126126
}
127127
t.end();
128128
});

lib/node_modules/@stdlib/math/base/special/boxcox/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 boxcox = require( './../lib' );
@@ -35,11 +35,12 @@ bench( pkg, function benchmark( b ) {
3535
var r;
3636
var i;
3737

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

0 commit comments

Comments
 (0)