Skip to content

Commit 0bd3d46

Browse files
bench: update random value generation
PR-URL: #5471 Co-authored-by: Karan Anand <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 00fa462 commit 0bd3d46

File tree

12 files changed

+76
-55
lines changed

12 files changed

+76
-55
lines changed

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
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 Int32Array = require( '@stdlib/array/int32' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2626
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var pkg = require( './../package.json' ).name;
2828
var TRIBONACCI = require( './../lib/tribonacci.json' );
@@ -36,10 +36,11 @@ bench( pkg, function benchmark( b ) {
3636
var y;
3737
var i;
3838

39+
x = discreteUniform( 100, 0, 63 );
40+
3941
b.tic();
4042
for ( i = 0; i < b.iterations; i++ ) {
41-
x = floor( randu()*64.0 );
42-
y = tribonacci( x );
43+
y = tribonacci( x[ i % x.length ] );
4344
if ( isnan( y ) ) {
4445
b.fail( 'should not return NaN' );
4546
}
@@ -57,10 +58,11 @@ bench( pkg+'::table', function benchmark( b ) {
5758
var y;
5859
var i;
5960

61+
x = discreteUniform( 100, 0, 63 );
62+
6063
b.tic();
6164
for ( i = 0; i < b.iterations; i++ ) {
62-
x = floor( randu()*64.0 );
63-
y = TRIBONACCI[ x ];
65+
y = TRIBONACCI[ x[ i % x.length ] ];
6466
if ( isnan( y ) ) {
6567
b.fail( 'should not return NaN' );
6668
}
@@ -88,10 +90,11 @@ bench( pkg+'::naive_recursion', function benchmark( b ) {
8890
return tribonacci( n-1 ) + tribonacci( n-2 ) + tribonacci( n-3 );
8991
}
9092

93+
x = discreteUniform( 100, 0, 10 );
94+
9195
b.tic();
9296
for ( i = 0; i < b.iterations; i++ ) {
93-
x = floor( randu()*10.0 ); // limit upper bound
94-
y = tribonacci( x );
97+
y = tribonacci( x[ i % x.length ] );
9598
if ( isnan( y ) ) {
9699
b.fail( 'should not return NaN' );
97100
}
@@ -111,7 +114,7 @@ bench( pkg+'::recursion_memoized', function benchmark( b ) {
111114
var y;
112115
var i;
113116

114-
arr = new Array( 64 );
117+
arr = new Int32Array( 64 );
115118
arr[ 0 ] = 0;
116119
arr[ 1 ] = 0;
117120
arr[ 2 ] = 1;
@@ -125,10 +128,11 @@ bench( pkg+'::recursion_memoized', function benchmark( b ) {
125128
return arr[ n ];
126129
}
127130

131+
x = discreteUniform( 100, 0, 10 );
132+
128133
b.tic();
129134
for ( i = 0; i < b.iterations; i++ ) {
130-
x = floor( randu()*10.0 ); // limit upper bound
131-
y = tribonacci( x );
135+
y = tribonacci( x[ i % x.length ] );
132136
if ( isnan( y ) ) {
133137
b.fail( 'should not return NaN' );
134138
}
@@ -150,7 +154,7 @@ bench( pkg+'::naive_iterative', function benchmark( b ) {
150154
var arr;
151155
var i;
152156

153-
arr = new Array( n+1 );
157+
arr = new Int32Array( n+1 );
154158
arr[ 0 ] = 0;
155159
arr[ 1 ] = 0;
156160
arr[ 2 ] = 1;
@@ -160,10 +164,11 @@ bench( pkg+'::naive_iterative', function benchmark( b ) {
160164
return arr[ n ];
161165
}
162166

167+
x = discreteUniform( 100, 0, 63 );
168+
163169
b.tic();
164170
for ( i = 0; i < b.iterations; i++ ) {
165-
x = floor( randu()*64.0 );
166-
y = tribonacci( x );
171+
y = tribonacci( x[ i % x.length ] );
167172
if ( isnan( y ) ) {
168173
b.fail( 'should not return NaN' );
169174
}
@@ -200,10 +205,11 @@ bench( pkg+'::iterative', function benchmark( b ) {
200205
return c;
201206
}
202207

208+
x = discreteUniform( 100, 0, 63 );
209+
203210
b.tic();
204211
for ( i = 0; i < b.iterations; i++ ) {
205-
x = floor( randu()*64.0 );
206-
y = tribonacci( x );
212+
y = tribonacci( x[ i % x.length ] );
207213
if ( isnan( y ) ) {
208214
b.fail( 'should not return NaN' );
209215
}
@@ -223,7 +229,7 @@ bench( pkg+'::iterative_memoized', function benchmark( b ) {
223229
var y;
224230
var i;
225231

226-
arr = new Array( 64 );
232+
arr = new Int32Array( 64 );
227233
arr[ 0 ] = 0;
228234
arr[ 1 ] = 0;
229235
arr[ 2 ] = 1;
@@ -240,10 +246,11 @@ bench( pkg+'::iterative_memoized', function benchmark( b ) {
240246
return arr[ n ];
241247
}
242248

249+
x = discreteUniform( 100, 0, 63 );
250+
243251
b.tic();
244252
for ( i = 0; i < b.iterations; i++ ) {
245-
x = floor( randu()*64.0 );
246-
y = tribonacci( x );
253+
y = tribonacci( x[ i % x.length ] );
247254
if ( isnan( y ) ) {
248255
b.fail( 'should not return NaN' );
249256
}

lib/node_modules/@stdlib/math/base/special/tribonacci/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, 63 );
47+
4748
b.tic();
4849
for ( i = 0; i < b.iterations; i++ ) {
49-
x = floor( randu()*64.0 );
50-
y = tribonacci( x );
50+
y = tribonacci( x[ i % x.length ] );
5151
if ( isnan( y ) ) {
5252
b.fail( 'should not return NaN' );
5353
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,18 @@ int tribonacci( int n ) {
115115
*/
116116
static double benchmark( void ) {
117117
double elapsed;
118+
int x[ 100 ];
118119
double t;
119-
int x;
120120
int y;
121121
int i;
122122

123+
for ( i = 0; i < 100; i++ ) {
124+
x[ i ] = (int)floor( 64.0*rand_double() );
125+
}
126+
123127
t = tic();
124128
for ( i = 0; i < ITERATIONS; i++ ) {
125-
x = (int)floor( 64.0*rand_double() );
126-
y = tribonacci( x );
129+
y = tribonacci( x[ i%100 ] );
127130
if ( y < 0 ) {
128131
printf( "should return a nonnegative integer\n" );
129132
break;

lib/node_modules/@stdlib/math/base/special/tribonacci/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+
int32_t x[ 100 ];
9394
double elapsed;
94-
int32_t x;
9595
double t;
9696
double y;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
x[ i ] = (int32_t)floor( 40.0*rand_double() );
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = (int32_t)floor( 40.0*rand_double() );
102-
y = stdlib_base_tribonacci( x );
105+
y = stdlib_base_tribonacci( x[ i%100 ] );
103106
if ( y < 0 ) {
104107
printf( "should return a nonnegative integer\n" );
105108
break;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,30 @@ tape( 'if provided a negative number, the function returns `NaN`', function test
4343
var v;
4444
var i;
4545

46-
t.strictEqual( isnan( tribonacci( -3.14 ) ), true, 'returns NaN' );
46+
t.strictEqual( isnan( tribonacci( -3.14 ) ), true, 'returns expected value' );
4747

4848
for ( i = -1; i > -100; i-- ) {
4949
v = tribonacci( i );
50-
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
50+
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
5151
}
5252
t.end();
5353
});
5454

5555
tape( 'if provided positive infinity, the function returns `NaN`', function test( t ) {
5656
var v = tribonacci( PINF );
57-
t.strictEqual( isnan( v ), true, 'returns NaN when provided +infinity' );
57+
t.strictEqual( isnan( v ), true, 'returns expected value' );
5858
t.end();
5959
});
6060

6161
tape( 'if provided `NaN`, the function returns `NaN`', function test( t ) {
6262
var v = tribonacci( NaN );
63-
t.strictEqual( isnan( v ), true, 'returns NaN when provided a NaN' );
63+
t.strictEqual( isnan( v ), true, 'returns expected value' );
6464
t.end();
6565
});
6666

6767
tape( 'if provided a non-integer, the function returns `NaN`', function test( t ) {
6868
var v = tribonacci( 3.14 );
69-
t.strictEqual( isnan( v ), true, 'returns NaN' );
69+
t.strictEqual( isnan( v ), true, 'returns expected value' );
7070
t.end();
7171
});
7272

@@ -85,7 +85,7 @@ tape( 'if provided nonnegative integers greater than `63`, the function returns
8585
var v;
8686
for ( i = 64; i < 500; i++ ) {
8787
v = tribonacci( i );
88-
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
88+
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
8989
}
9090
t.end();
9191
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ tape( 'if provided a negative number, the function returns `NaN`', opts, functio
5353

5454
for ( i = -1; i > -100; i-- ) {
5555
v = tribonacci( i );
56-
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
56+
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
5757
}
5858
t.end();
5959
});
@@ -73,7 +73,7 @@ tape( 'if provided nonnegative integers greater than `63`, the function returns
7373
var v;
7474
for ( i = 64; i < 500; i++ ) {
7575
v = tribonacci( i );
76-
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
76+
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
7777
}
7878
t.end();
7979
});

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

Lines changed: 4 additions & 3 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;
@@ -35,10 +35,11 @@ bench( pkg, function benchmark( b ) {
3535
var y;
3636
var i;
3737

38+
x = uniform( 100, EPS, 100.0 );
39+
3840
b.tic();
3941
for ( i = 0; i < b.iterations; i++ ) {
40-
x = ( randu()*100.0 ) + EPS;
41-
y = trigamma( x );
42+
y = trigamma( x[ i % x.length ] );
4243
if ( isnan( y ) ) {
4344
b.fail( 'should not return NaN' );
4445
}

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

Lines changed: 4 additions & 3 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 EPS = require( '@stdlib/constants/float64/eps' );
2828
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -44,10 +44,11 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4444
var y;
4545
var i;
4646

47+
x = uniform( 100, EPS, 100.0 );
48+
4749
b.tic();
4850
for ( i = 0; i < b.iterations; i++ ) {
49-
x = ( randu() * 100.0 ) + EPS;
50-
y = trigamma( x );
51+
y = trigamma( x[ i % x.length ] );
5152
if ( isnan( y ) ) {
5253
b.fail( 'should not return NaN' );
5354
}

lib/node_modules/@stdlib/math/base/special/trigamma/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 ] = ( 100.0 * rand_double() );
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
x = ( 100.0 * rand_double() );
102-
y = stdlib_base_trigamma( x );
105+
y = stdlib_base_trigamma( x[ i%100 ] );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

lib/node_modules/@stdlib/math/base/special/trigamma/benchmark/cpp/boost/benchmark.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ double tic() {
8585
* @return elapsed time in seconds
8686
*/
8787
double benchmark() {
88+
double x[ 100 ];
8889
double elapsed;
89-
double x;
9090
double y;
9191
double t;
9292
int i;
@@ -97,10 +97,13 @@ double benchmark() {
9797
// Define a uniform distribution for generating pseudorandom numbers as "doubles" between a minimum value (inclusive) and a maximum value (exclusive):
9898
uniform_real_distribution<> randu( 0.1, 100.0 );
9999

100+
for ( i = 0; i < 100; i++ ) {
101+
x[ i ] = randu( rng );
102+
}
103+
100104
t = tic();
101105
for ( i = 0; i < ITERATIONS; i++ ) {
102-
x = randu( rng );
103-
y = boost::math::trigamma( x );
106+
y = boost::math::trigamma( x[ i%100 ] );
104107
if ( y != y ) {
105108
printf( "should not return NaN\n" );
106109
break;

0 commit comments

Comments
 (0)