Skip to content

Commit d3bf998

Browse files
hrshyakgryte
andauthored
bench: update random value generation
PR-URL: #6269 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 975de6a commit d3bf998

File tree

12 files changed

+96
-70
lines changed

12 files changed

+96
-70
lines changed

lib/node_modules/@stdlib/math/base/special/fibonacci-index/benchmark/benchmark.js

Lines changed: 7 additions & 5 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2727
var fibonacci = require( '@stdlib/math/base/special/fibonacci' );
2828
var pkg = require( './../package.json' ).name;
@@ -37,14 +37,16 @@ bench( pkg, function benchmark( b ) {
3737
var y;
3838
var i;
3939

40-
FN = new Array( 76 );
40+
FN = zeros( 79 );
4141
for ( i = 3; i < 79; i++ ) {
4242
FN[ i ] = fibonacci( i );
4343
}
44+
45+
x = discreteUniform( 100, 3, 78 );
46+
4447
b.tic();
4548
for ( i = 0; i < b.iterations; i++ ) {
46-
x = floor( (randu()*75.0) + 3.0 );
47-
y = fibonacciIndex( FN[ x ] );
49+
y = fibonacciIndex( FN[ x[ i%x.length ] ] );
4850
if ( isnan( y ) ) {
4951
b.fail( 'should not return NaN' );
5052
}

lib/node_modules/@stdlib/math/base/special/fibonacci-index/benchmark/benchmark.native.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
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' );
26+
var zeros = require( '@stdlib/array/base/zeros' );
2727
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2828
var fibonacci = require( '@stdlib/math/base/special/fibonacci' );
2929
var tryRequire = require( '@stdlib/utils/try-require' );
@@ -46,14 +46,16 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4646
var y;
4747
var i;
4848

49-
FN = new Array( 76 );
49+
FN = zeros( 79 );
5050
for ( i = 3; i < 79; i++ ) {
5151
FN[ i ] = fibonacci( i );
5252
}
53+
54+
x = discreteUniform( 100, 3, 78 );
55+
5356
b.tic();
5457
for ( i = 0; i < b.iterations; i++ ) {
55-
x = floor( ( randu() * 75.0 ) + 3.0 );
56-
y = fibonacciIndex( FN[ x ] );
58+
y = fibonacciIndex( FN[ x[ i%x.length ] ] );
5759
if ( isnan( y ) ) {
5860
b.fail( 'should not return NaN' );
5961
}

lib/node_modules/@stdlib/math/base/special/fibonacci-index/benchmark/c/benchmark.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,18 @@ int fibonacci_index( int F ) {
102102
static double benchmark( void ) {
103103
double elapsed;
104104
double t;
105-
int x;
105+
int x[ 100 ];
106106
int y;
107107
int i;
108108

109+
for ( i = 0; i < 100; i++ ) {
110+
x[ i ] = (int)floor( (100000.0*rand_double()) + 2.0 );
111+
}
112+
109113
t = tic();
110114
for ( i = 0; i < ITERATIONS; i++ ) {
111115
// note: using actual Fibonacci numbers is not important
112-
x = (int)floor( (100000.0*rand_double()) + 2.0 );
113-
y = fibonacci_index( x );
116+
y = fibonacci_index( x[ i%100 ] );
114117
if ( y < 0 ) {
115118
printf( "should return a nonnegative integer\n" );
116119
break;

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

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

lib/node_modules/@stdlib/math/base/special/fibonacci-index/test/test.js

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

3838
tape( 'if provided `NaN`, the function returns `NaN`', function test( t ) {
3939
var n = fibonacciIndex( NaN );
40-
t.strictEqual( isnan( n ), true, 'returns NaN when provided a NaN' );
40+
t.strictEqual( isnan( n ), true, 'returns expected value' );
4141
t.end();
4242
});
4343

4444
tape( 'if provided `+infinity`, the function returns `NaN`', function test( t ) {
4545
var n = fibonacciIndex( PINF );
46-
t.strictEqual( isnan( n ), true, 'returns NaN' );
46+
t.strictEqual( isnan( n ), true, 'returns expected value' );
4747
t.end();
4848
});
4949

5050
tape( 'if provided a number less than or equal to 1, the function returns `NaN`', function test( t ) {
5151
var n;
5252
var i;
5353

54-
t.strictEqual( isnan( fibonacciIndex( -3.14 ) ), true, 'returns NaN' );
54+
t.strictEqual( isnan( fibonacciIndex( -3.14 ) ), true, 'returns expected value' );
5555

5656
for ( i = 1; i > -100; i-- ) {
5757
n = fibonacciIndex( i );
58-
t.strictEqual( isnan( n ), true, 'returns NaN when provided ' + i );
58+
t.strictEqual( isnan( n ), true, 'returns expected value when provided ' + i );
5959
}
6060
t.end();
6161
});
6262

6363
tape( 'if provided a non-integer, the function returns `NaN`', function test( t ) {
6464
var n = fibonacciIndex( 3.14 );
65-
t.strictEqual( isnan( n ), true, 'returns NaN' );
65+
t.strictEqual( isnan( n ), true, 'returns expected value' );
6666
t.end();
6767
});
6868

lib/node_modules/@stdlib/math/base/special/fibonacci-index/test/test.native.js

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

4747
tape( 'if provided `NaN`, the function returns `NaN`', opts, function test( t ) {
4848
var n = fibonacciIndex( NaN );
49-
t.strictEqual( isnan( n ), true, 'returns NaN when provided a NaN' );
49+
t.strictEqual( isnan( n ), true, 'returns expected value' );
5050
t.end();
5151
});
5252

5353
tape( 'if provided `+infinity`, the function returns `NaN`', opts, function test( t ) {
5454
var n = fibonacciIndex( PINF );
55-
t.strictEqual( isnan( n ), true, 'returns NaN' );
55+
t.strictEqual( isnan( n ), true, 'returns expected value' );
5656
t.end();
5757
});
5858

5959
tape( 'if provided a number less than or equal to 1, the function returns `NaN`', opts, function test( t ) {
6060
var n;
6161
var i;
6262

63-
t.strictEqual( isnan( fibonacciIndex( -3.14 ) ), true, 'returns NaN' );
63+
t.strictEqual( isnan( fibonacciIndex( -3.14 ) ), true, 'returns expected value' );
6464

6565
for ( i = 1; i > -100; i-- ) {
6666
n = fibonacciIndex( i );
67-
t.strictEqual( isnan( n ), true, 'returns NaN when provided ' + i );
67+
t.strictEqual( isnan( n ), true, 'returns expected value when provided ' + i );
6868
}
6969
t.end();
7070
});
7171

7272
tape( 'if provided a non-integer, the function returns `NaN`', opts, function test( t ) {
7373
var n = fibonacciIndex( 3.14 );
74-
t.strictEqual( isnan( n ), true, 'returns NaN' );
74+
t.strictEqual( isnan( n ), true, 'returns expected value' );
7575
t.end();
7676
});
7777

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

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
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 round = require( '@stdlib/math/base/special/round' );
2726
var sqrt = require( '@stdlib/math/base/special/sqrt' );
2827
var pow = require( '@stdlib/math/base/special/pow' );
28+
var zeros = require( '@stdlib/array/base/zeros' );
2929
var isnan = require( '@stdlib/math/base/assert/is-nan' );
3030
var PHI = require( '@stdlib/constants/float64/phi' );
3131
var pkg = require( './../package.json' ).name;
@@ -45,10 +45,11 @@ bench( pkg, function benchmark( b ) {
4545
var y;
4646
var i;
4747

48+
x = discreteUniform( 100, 0, 78 );
49+
4850
b.tic();
4951
for ( i = 0; i < b.iterations; i++ ) {
50-
x = floor( randu()*79.0 );
51-
y = fibonacci( x );
52+
y = fibonacci( x[ i%x.length ] );
5253
if ( isnan( y ) ) {
5354
b.fail( 'should not return NaN' );
5455
}
@@ -70,10 +71,11 @@ bench( pkg+'::analytic', function benchmark( b ) {
7071
return round( pow( PHI, n ) / SQRT_5 );
7172
}
7273

74+
x = discreteUniform( 100, 0, 78 );
75+
7376
b.tic();
7477
for ( i = 0; i < b.iterations; i++ ) {
75-
x = floor( randu()*79.0 );
76-
y = fibonacci( x );
78+
y = fibonacci( x[ i%x.length ] );
7779
if ( isnan( y ) ) {
7880
b.fail( 'should not return NaN' );
7981
}
@@ -91,10 +93,11 @@ bench( pkg+'::table', function benchmark( b ) {
9193
var y;
9294
var i;
9395

96+
x = discreteUniform( 100, 0, 78 );
97+
9498
b.tic();
9599
for ( i = 0; i < b.iterations; i++ ) {
96-
x = floor( randu()*79.0 );
97-
y = FIBONACCI[ x ];
100+
y = FIBONACCI[ x[ i%x.length ] ];
98101
if ( isnan( y ) ) {
99102
b.fail( 'should not return NaN' );
100103
}
@@ -119,10 +122,11 @@ bench( pkg+'::naive_recursion', function benchmark( b ) {
119122
return fibonacci( n-1 ) + fibonacci( n-2 );
120123
}
121124

125+
x = discreteUniform( 100, 0, 40 ); // limit upper bound
126+
122127
b.tic();
123128
for ( i = 0; i < b.iterations; i++ ) {
124-
x = floor( randu()*40.0 ); // limit upper bound
125-
y = fibonacci( x );
129+
y = fibonacci( x[ i%x.length ] );
126130
if ( isnan( y ) ) {
127131
b.fail( 'should not return NaN' );
128132
}
@@ -142,7 +146,7 @@ bench( pkg+'::recursion_memoized', function benchmark( b ) {
142146
var y;
143147
var i;
144148

145-
arr = new Array( 79 );
149+
arr = zeros( 79 );
146150
arr[ 0 ] = 0;
147151
arr[ 1 ] = 1;
148152
arr[ 2 ] = 1;
@@ -156,10 +160,11 @@ bench( pkg+'::recursion_memoized', function benchmark( b ) {
156160
return arr[ n ];
157161
}
158162

163+
x = discreteUniform( 100, 0, 40 ); // limit upper bound
164+
159165
b.tic();
160166
for ( i = 0; i < b.iterations; i++ ) {
161-
x = floor( randu()*40.0 ); // limit upper bound
162-
y = fibonacci( x );
167+
y = fibonacci( x[ i%x.length ] );
163168
if ( isnan( y ) ) {
164169
b.fail( 'should not return NaN' );
165170
}
@@ -181,7 +186,7 @@ bench( pkg+'::naive_iterative', function benchmark( b ) {
181186
var arr;
182187
var i;
183188

184-
arr = new Array( n+1 );
189+
arr = zeros( n+1 );
185190
arr[ 0 ] = 0;
186191
arr[ 1 ] = 1;
187192
arr[ 2 ] = 1;
@@ -191,10 +196,11 @@ bench( pkg+'::naive_iterative', function benchmark( b ) {
191196
return arr[ n ];
192197
}
193198

199+
x = discreteUniform( 100, 0, 78 );
200+
194201
b.tic();
195202
for ( i = 0; i < b.iterations; i++ ) {
196-
x = floor( randu()*79.0 );
197-
y = fibonacci( x );
203+
y = fibonacci( x[ i%x.length ] );
198204
if ( isnan( y ) ) {
199205
b.fail( 'should not return NaN' );
200206
}
@@ -228,10 +234,11 @@ bench( pkg+'::iterative', function benchmark( b ) {
228234
return b;
229235
}
230236

237+
x = discreteUniform( 100, 0, 78 );
238+
231239
b.tic();
232240
for ( i = 0; i < b.iterations; i++ ) {
233-
x = floor( randu()*79.0 );
234-
y = fibonacci( x );
241+
y = fibonacci( x[ i%x.length ] );
235242
if ( isnan( y ) ) {
236243
b.fail( 'should not return NaN' );
237244
}
@@ -251,7 +258,7 @@ bench( pkg+'::iterative_memoized', function benchmark( b ) {
251258
var y;
252259
var i;
253260

254-
arr = new Array( 79 );
261+
arr = zeros( 79 );
255262
arr[ 0 ] = 0;
256263
arr[ 1 ] = 1;
257264
arr[ 2 ] = 1;
@@ -268,10 +275,11 @@ bench( pkg+'::iterative_memoized', function benchmark( b ) {
268275
return arr[ n ];
269276
}
270277

278+
x = discreteUniform( 100, 0, 78 );
279+
271280
b.tic();
272281
for ( i = 0; i < b.iterations; i++ ) {
273-
x = floor( randu()*79.0 );
274-
y = fibonacci( x );
282+
y = fibonacci( x[ i%x.length ] );
275283
if ( isnan( y ) ) {
276284
b.fail( 'should not return NaN' );
277285
}
@@ -314,10 +322,11 @@ bench( pkg+'::iterative_doubling', function benchmark( b ) {
314322
return a + b;
315323
}
316324

325+
x = discreteUniform( 100, 0, 78 );
326+
317327
b.tic();
318328
for ( i = 0; i < b.iterations; i++ ) {
319-
x = floor( randu()*79.0 );
320-
y = fibonacci( x );
329+
y = fibonacci( x[ i%x.length ] );
321330
if ( isnan( y ) ) {
322331
b.fail( 'should not return NaN' );
323332
}
@@ -337,7 +346,7 @@ bench( pkg+'::iterative_doubling_memoized', function benchmark( b ) {
337346
var y;
338347
var i;
339348

340-
arr = new Array( 79 );
349+
arr = zeros( 79 );
341350
arr[ 0 ] = 0;
342351
arr[ 1 ] = 1;
343352
arr[ 2 ] = 1;
@@ -377,10 +386,11 @@ bench( pkg+'::iterative_doubling_memoized', function benchmark( b ) {
377386
return a + b;
378387
}
379388

389+
x = discreteUniform( 100, 0, 78 );
390+
380391
b.tic();
381392
for ( i = 0; i < b.iterations; i++ ) {
382-
x = floor( randu()*79.0 );
383-
y = fibonacci( x );
393+
y = fibonacci( x[ i%x.length ] );
384394
if ( isnan( y ) ) {
385395
b.fail( 'should not return NaN' );
386396
}

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

0 commit comments

Comments
 (0)