Skip to content

Commit 9b2d78d

Browse files
bench: refactor random value generation
PR-URL: #5465 Co-authored-by: stdlib-bot <noreply@stdlib.io> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Karan Anand <anandkaranubc@users.noreply.github.com>
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
}

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

Lines changed: 5 additions & 4 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/base/randu' );
25+
var uniform = require( '@stdlib/random/array/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;
@@ -44,11 +44,12 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4444
var r;
4545
var i;
4646

47+
x = uniform( 100, 1.0, 11.0 );
48+
y = uniform( 100, 1.0, 11.0 );
49+
4750
b.tic();
4851
for ( i = 0; i < b.iterations; i++ ) {
49-
x = ( randu() * 10.0 ) + 1.0;
50-
y = ( randu() * 10.0 ) + 1.0;
51-
r = boxcox( x, y );
52+
r = boxcox( x[ i%x.length ], y[ i%y.length ] );
5253
if ( isnan( r ) ) {
5354
b.fail( 'should not return NaN' );
5455
}

lib/node_modules/@stdlib/math/base/special/boxcox/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+
double x[ 100 ];
94+
double y[ 100 ];
9395
double elapsed;
94-
double x;
95-
double y;
9696
double r;
9797
double t;
9898
int i;
9999

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ tape( 'the function returns `NaN` if provided `NaN`', function test( t ) {
4949
var b;
5050

5151
b = boxcox( NaN, 1.0 );
52-
t.equal( isnan( b ), true, 'returns NaN' );
52+
t.equal( isnan( b ), true, 'returns expected value' );
5353

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

5757
b = boxcox( NaN, NaN );
58-
t.equal( isnan( b ), true, 'returns NaN' );
58+
t.equal( isnan( b ), true, 'returns expected value' );
5959

6060
t.end();
6161
});
@@ -87,7 +87,7 @@ tape( 'the function returns `NaN` if `x` is negative', function test( t ) {
8787
x = -1.0 * ( (randu()*100.0) + 1.0 );
8888
y = randu() * 10.0;
8989
b = boxcox( x, y );
90-
t.equal( isnan( b ), true, 'returns NaN' );
90+
t.equal( isnan( b ), true, 'returns expected value' );
9191
}
9292
t.end();
9393
});

lib/node_modules/@stdlib/math/base/special/boxcox/test/test.native.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) {
5858
var b;
5959

6060
b = boxcox( NaN, 1.0 );
61-
t.equal( isnan( b ), true, 'returns NaN' );
61+
t.equal( isnan( b ), true, 'returns expected value' );
6262

6363
b = boxcox( 1.0, NaN );
64-
t.equal( isnan( b ), true, 'returns NaN' );
64+
t.equal( isnan( b ), true, 'returns expected value' );
6565

6666
b = boxcox( NaN, NaN );
67-
t.equal( isnan( b ), true, 'returns NaN' );
67+
t.equal( isnan( b ), true, 'returns expected value' );
6868

6969
t.end();
7070
});
@@ -96,7 +96,7 @@ tape( 'the function returns `NaN` if `x` is negative', opts, function test( t )
9696
x = -1.0 * ( (randu()*100.0) + 1.0 );
9797
y = randu() * 10.0;
9898
b = boxcox( x, y );
99-
t.equal( isnan( b ), true, 'returns NaN' );
99+
t.equal( isnan( b ), true, 'returns expected value' );
100100
}
101101
t.end();
102102
});

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

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

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

Lines changed: 5 additions & 4 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/base/randu' );
25+
var uniform = require( '@stdlib/random/array/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;
@@ -44,11 +44,12 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4444
var r;
4545
var i;
4646

47+
x = uniform( 100, 0.0, 10.0 );
48+
y = uniform( 100, 0.0, 10.0 );
49+
4750
b.tic();
4851
for ( i = 0; i < b.iterations; i++ ) {
49-
x = randu() * 10.0;
50-
y = randu() * 10.0;
51-
r = boxcox1p( x, y );
52+
r = boxcox1p( x[ i%x.length ], y[ i%y.length ] );
5253
if ( isnan( r ) ) {
5354
b.fail( 'should not return NaN' );
5455
}

0 commit comments

Comments
 (0)