Skip to content

Commit e9f9513

Browse files
authored
bench: update random value generation
PR-URL: #6430 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent 2decebf commit e9f9513

File tree

19 files changed

+135
-104
lines changed

19 files changed

+135
-104
lines changed

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

Lines changed: 29 additions & 21 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var zeros = require( '@stdlib/array/base/zeros' );
2626
var round = require( '@stdlib/math/base/special/round' );
2727
var sqrt = require( '@stdlib/math/base/special/sqrt' );
2828
var pow = require( '@stdlib/math/base/special/pow' );
@@ -46,10 +46,11 @@ bench( pkg, function benchmark( b ) {
4646
var y;
4747
var i;
4848

49+
x = discreteUniform( 100, -78, 0 );
50+
4951
b.tic();
5052
for ( i = 0; i < b.iterations; i++ ) {
51-
x = -floor( randu()*79.0 );
52-
y = negafibonacci( x );
53+
y = negafibonacci( x[ i%x.length ] );
5354
if ( isnan( y ) ) {
5455
b.fail( 'should not return NaN' );
5556
}
@@ -72,10 +73,11 @@ bench( pkg+'::analytic', function benchmark( b ) {
7273
return pow( -1.0, an+1 ) * round( pow( PHI, an ) / SQRT_5 );
7374
}
7475

76+
x = discreteUniform( 100, -78, 0 );
77+
7578
b.tic();
7679
for ( i = 0; i < b.iterations; i++ ) {
77-
x = -floor( randu()*79.0 );
78-
y = negafibonacci( x );
80+
y = negafibonacci( x[ i%x.length ] );
7981
if ( isnan( y ) ) {
8082
b.fail( 'should not return NaN' );
8183
}
@@ -93,10 +95,11 @@ bench( pkg+'::table', function benchmark( b ) {
9395
var y;
9496
var i;
9597

98+
x = discreteUniform( 100, -78, 0 );
99+
96100
b.tic();
97101
for ( i = 0; i < b.iterations; i++ ) {
98-
x = -floor( randu()*79.0 );
99-
y = NEGAFIBONACCI[ abs( x ) ];
102+
y = NEGAFIBONACCI[ abs( x[ i%x.length ] ) ];
100103
if ( isnan( y ) ) {
101104
b.fail( 'should not return NaN' );
102105
}
@@ -124,10 +127,11 @@ bench( pkg+'::naive_recursion', function benchmark( b ) {
124127
return negafibonacci( n+2 ) - negafibonacci( n+1 );
125128
}
126129

130+
x = discreteUniform( 100, -40, 0 );
131+
127132
b.tic();
128133
for ( i = 0; i < b.iterations; i++ ) {
129-
x = -floor( randu()*40.0 ); // limit lower bound
130-
y = negafibonacci( x );
134+
y = negafibonacci( x[ i%x.length ] );
131135
if ( isnan( y ) ) {
132136
b.fail( 'should not return NaN' );
133137
}
@@ -147,7 +151,7 @@ bench( pkg+'::recursion_memoized', function benchmark( b ) {
147151
var y;
148152
var i;
149153

150-
arr = new Array( 79 );
154+
arr = zeros( 79 );
151155
arr[ 0 ] = 0;
152156
arr[ 1 ] = 1;
153157
arr[ 2 ] = -1;
@@ -162,10 +166,11 @@ bench( pkg+'::recursion_memoized', function benchmark( b ) {
162166
return arr[ an ];
163167
}
164168

169+
x = discreteUniform( 100, -40, 0 );
170+
165171
b.tic();
166172
for ( i = 0; i < b.iterations; i++ ) {
167-
x = -floor( randu()*40.0 ); // limit lower bound
168-
y = negafibonacci( x );
173+
y = negafibonacci( x[ i%x.length ] );
169174
if ( isnan( y ) ) {
170175
b.fail( 'should not return NaN' );
171176
}
@@ -190,7 +195,7 @@ bench( pkg+'::naive_iterative', function benchmark( b ) {
190195

191196
an = abs( n );
192197

193-
arr = new Array( an+1 );
198+
arr = zeros( an+1 );
194199
arr[ 0 ] = 0;
195200
arr[ 1 ] = 1;
196201
arr[ 2 ] = -1;
@@ -200,10 +205,11 @@ bench( pkg+'::naive_iterative', function benchmark( b ) {
200205
return arr[ an ];
201206
}
202207

208+
x = discreteUniform( 100, -40, 0 );
209+
203210
b.tic();
204211
for ( i = 0; i < b.iterations; i++ ) {
205-
x = -floor( randu()*79.0 );
206-
y = negafibonacci( x );
212+
y = negafibonacci( x[ i%x.length ] );
207213
if ( isnan( y ) ) {
208214
b.fail( 'should not return NaN' );
209215
}
@@ -240,10 +246,11 @@ bench( pkg+'::iterative', function benchmark( b ) {
240246
return b;
241247
}
242248

249+
x = discreteUniform( 100, -78, 0 );
250+
243251
b.tic();
244252
for ( i = 0; i < b.iterations; i++ ) {
245-
x = -floor( randu()*79.0 );
246-
y = negafibonacci( x );
253+
y = negafibonacci( x[ i%x.length ] );
247254
if ( isnan( y ) ) {
248255
b.fail( 'should not return NaN' );
249256
}
@@ -263,7 +270,7 @@ bench( pkg+'::iterative_memoized', function benchmark( b ) {
263270
var y;
264271
var i;
265272

266-
arr = new Array( 79 );
273+
arr = zeros( 79 );
267274
arr[ 0 ] = 0;
268275
arr[ 1 ] = 1;
269276
arr[ 2 ] = -1;
@@ -283,10 +290,11 @@ bench( pkg+'::iterative_memoized', function benchmark( b ) {
283290
return arr[ an ];
284291
}
285292

293+
x = discreteUniform( 100, -78, 0 );
294+
286295
b.tic();
287296
for ( i = 0; i < b.iterations; i++ ) {
288-
x = -floor( randu()*79.0 );
289-
y = negafibonacci( x );
297+
y = negafibonacci( x[ i%x.length ] );
290298
if ( isnan( y ) ) {
291299
b.fail( 'should not return NaN' );
292300
}

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

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,17 @@ int negafibonacci( int n ) {
107107
static double benchmark( void ) {
108108
double elapsed;
109109
double t;
110-
int x;
110+
int x[ 100 ];
111111
int y;
112112
int i;
113113

114+
for ( i = 0; i < 100; i++ ) {
115+
x[ i ] = (int)floor( 40.0*rand_double() );
116+
}
117+
114118
t = tic();
115119
for ( i = 0; i < ITERATIONS; i++ ) {
116-
x = (int)floor( 40.0*rand_double() );
117-
y = negafibonacci( -x );
120+
y = negafibonacci( -x[ i%100 ] );
118121
if ( y == 7 ) {
119122
printf( "should not return 7\n" );
120123
break;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,18 @@ static double rand_double( void ) {
9191
*/
9292
static double benchmark( void ) {
9393
double elapsed;
94-
int32_t x;
94+
int32_t x[ 100 ];
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_negafibonacci( -x );
105+
y = stdlib_base_negafibonacci( -x[ i%100 ] );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,30 @@ tape( 'if provided a positive number, the function returns `NaN`', function test
4646
var v;
4747
var i;
4848

49-
t.strictEqual( isnan( negafibonacci( 3.14 ) ), true, 'returns NaN' );
49+
t.strictEqual( isnan( negafibonacci( 3.14 ) ), true, 'returns expected value' );
5050

5151
for ( i = 1; i < 100; i++ ) {
5252
v = negafibonacci( i );
53-
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
53+
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
5454
}
5555
t.end();
5656
});
5757

5858
tape( 'if provided negative infinity, the function returns `NaN`', function test( t ) {
5959
var v = negafibonacci( NINF );
60-
t.strictEqual( isnan( v ), true, 'returns NaN when provided -infinity' );
60+
t.strictEqual( isnan( v ), true, 'returns expected value' );
6161
t.end();
6262
});
6363

6464
tape( 'if provided `NaN`, the function returns `NaN`', function test( t ) {
6565
var v = negafibonacci( NaN );
66-
t.strictEqual( isnan( v ), true, 'returns NaN when provided a NaN' );
66+
t.strictEqual( isnan( v ), true, 'returns expected value' );
6767
t.end();
6868
});
6969

7070
tape( 'if provided a non-integer, the function returns `NaN`', function test( t ) {
7171
var v = negafibonacci( -3.14 );
72-
t.strictEqual( isnan( v ), true, 'returns NaN' );
72+
t.strictEqual( isnan( v ), true, 'returns expected value' );
7373
t.end();
7474
});
7575

@@ -100,7 +100,7 @@ tape( 'if provided nonpositive integers less than `-78`, the function returns `N
100100
var v;
101101
for ( i = -79; i > -500; i-- ) {
102102
v = negafibonacci( i );
103-
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
103+
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
104104
}
105105
t.end();
106106
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ tape( 'if provided a positive number, the function returns `NaN`', opts, functio
5454
var v;
5555
var i;
5656

57-
t.strictEqual( isnan( negafibonacci( 3.14 ) ), true, 'returns NaN' );
57+
t.strictEqual( isnan( negafibonacci( 3.14 ) ), true, 'returns expected value' );
5858

5959
for ( i = 1; i < 100; i++ ) {
6060
v = negafibonacci( i );
61-
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
61+
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
6262
}
6363
t.end();
6464
});
@@ -90,7 +90,7 @@ tape( 'if provided nonpositive integers less than `-78`, the function returns `N
9090
var v;
9191
for ( i = -79; i > -500; i-- ) {
9292
v = negafibonacci( i );
93-
t.strictEqual( isnan( v ), true, 'returns NaN when provided ' + i );
93+
t.strictEqual( isnan( v ), true, 'returns expected value when provided ' + i );
9494
}
9595
t.end();
9696
});

0 commit comments

Comments
 (0)