Skip to content

Commit 420735c

Browse files
bench: update random value generation
PR-URL: #5467 Reviewed-by: Athan Reines <[email protected]>
1 parent 4039ae0 commit 420735c

File tree

16 files changed

+169
-144
lines changed

16 files changed

+169
-144
lines changed

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

Lines changed: 11 additions & 5 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2626
var pkg = require( './../package.json' ).name;
2727
var truncf = require( './../lib' );
@@ -41,10 +41,13 @@ bench( pkg, function benchmark( b ) {
4141
var y;
4242
var i;
4343

44+
x = uniform( 100, -500.0, 500.0, {
45+
'dtype': 'float32'
46+
});
47+
4448
b.tic();
4549
for ( i = 0; i < b.iterations; i++ ) {
46-
x = ( randu()*1000.0 ) - 500.0;
47-
y = truncf( x );
50+
y = truncf( x[ i % x.length ] );
4851
if ( isnanf( y ) ) {
4952
b.fail( 'should not return NaN' );
5053
}
@@ -62,10 +65,13 @@ bench( pkg+'::built-in', opts, function benchmark( b ) {
6265
var y;
6366
var i;
6467

68+
x = uniform( 100, -500.0, 500.0, {
69+
'dtype': 'float32'
70+
});
71+
6572
b.tic();
6673
for ( i = 0; i < b.iterations; i++ ) {
67-
x = ( randu()*1000.0 ) - 500.0;
68-
y = Math.trunc( x ); // eslint-disable-line stdlib/no-builtin-math
74+
y = Math.trunc( x[ i % x.length ] ); // eslint-disable-line stdlib/no-builtin-math
6975
if ( isnanf( y ) ) {
7076
b.fail( 'should not return NaN' );
7177
}

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

Lines changed: 6 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
@@ -43,10 +43,13 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4343
var y;
4444
var i;
4545

46+
x = uniform( 100, -500.0, 500.0, {
47+
'dtype': 'float32'
48+
});
49+
4650
b.tic();
4751
for ( i = 0; i < b.iterations; i++ ) {
48-
x = ( randu()*1000.0 ) - 500.0;
49-
y = truncf( x );
52+
y = truncf( x[ i % x.length ] );
5053
if ( isnanf( y ) ) {
5154
b.fail( 'should not return NaN' );
5255
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,17 @@ static float rand_float( void ) {
9191
static double benchmark( void ) {
9292
double elapsed;
9393
double t;
94-
float x;
94+
float x[ 100 ];
9595
float y;
9696
int i;
9797

98+
for ( i = 0; i < 100; i++ ) {
99+
x[ i ] = ( 1000.0f * rand_float() ) - 500.0f;
100+
}
101+
98102
t = tic();
99103
for ( i = 0; i < ITERATIONS; i++ ) {
100-
x = ( 1000.0f * rand_float() ) - 500.0f;
101-
y = truncf( x );
104+
y = truncf( x[ i%100 ] );
102105
if ( y != y ) {
103106
printf( "should not return NaN\n" );
104107
break;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,17 @@ static float rand_float( void ) {
9292
static double benchmark( void ) {
9393
double elapsed;
9494
double t;
95-
float x;
95+
float x[ 100 ];
9696
float y;
9797
int i;
9898

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

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,37 @@ tape( 'main export is a function', function test( t ) {
3838
});
3939

4040
tape( 'the function rounds a numeric value toward 0', function test( t ) {
41-
t.strictEqual( truncf( -4.2 ), -4.0, 'equals -4' );
42-
t.strictEqual( truncf( 9.99999 ), 9.0, 'equals 9' );
41+
t.strictEqual( truncf( -4.2 ), -4.0, 'returns expected value' );
42+
t.strictEqual( truncf( 9.99999 ), 9.0, 'returns expected value' );
4343
t.end();
4444
});
4545

4646
tape( 'if provided `+0`, the function returns `+0`', function test( t ) {
4747
var v = truncf( 0.0 );
48-
t.strictEqual( isPositiveZerof( v ), true, 'equals +0' );
48+
t.strictEqual( isPositiveZerof( v ), true, 'returns expected value' );
4949
t.end();
5050
});
5151

5252
tape( 'if provided `-0`, the function returns `-0`', function test( t ) {
5353
var v = truncf( -0.0 );
54-
t.strictEqual( isNegativeZerof( v ), true, 'returns -0' );
54+
t.strictEqual( isNegativeZerof( v ), true, 'returns expected value' );
5555
t.end();
5656
});
5757

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

6464
tape( 'the function returns `+infinity` if provided `+infinity`', function test( t ) {
6565
var v = truncf( PINF );
66-
t.strictEqual( v, PINF, 'returns +infinity' );
66+
t.strictEqual( v, PINF, 'returns expected value' );
6767
t.end();
6868
});
6969

7070
tape( 'the function returns `-infinity` if provided `-infinity`', function test( t ) {
7171
var v = truncf( NINF );
72-
t.strictEqual( v, NINF, 'returns -infinity' );
72+
t.strictEqual( v, NINF, 'returns expected value' );
7373
t.end();
7474
});

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,37 +47,37 @@ tape( 'main export is a function', opts, function test( t ) {
4747
});
4848

4949
tape( 'the function rounds a numeric value toward 0', opts, function test( t ) {
50-
t.strictEqual( truncf( -4.2 ), -4.0, 'equals -4' );
51-
t.strictEqual( truncf( 9.99999 ), 9.0, 'equals 9' );
50+
t.strictEqual( truncf( -4.2 ), -4.0, 'returns expected value' );
51+
t.strictEqual( truncf( 9.99999 ), 9.0, 'returns expected value' );
5252
t.end();
5353
});
5454

5555
tape( 'if provided `+0`, the function returns `+0`', opts, function test( t ) {
5656
var v = truncf( 0.0 );
57-
t.strictEqual( isPositiveZerof( v ), true, 'equals +0' );
57+
t.strictEqual( isPositiveZerof( v ), true, 'returns expected value' );
5858
t.end();
5959
});
6060

6161
tape( 'if provided `-0`, the function returns `-0`', opts, function test( t ) {
6262
var v = truncf( -0.0 );
63-
t.strictEqual( isNegativeZerof( v ), true, 'returns -0' );
63+
t.strictEqual( isNegativeZerof( v ), true, 'returns expected value' );
6464
t.end();
6565
});
6666

6767
tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) {
6868
var v = truncf( NaN );
69-
t.strictEqual( isnanf( v ), true, 'returns NaN' );
69+
t.strictEqual( isnanf( v ), true, 'returns expected value' );
7070
t.end();
7171
});
7272

7373
tape( 'the function returns `+infinity` if provided `+infinity`', opts, function test( t ) {
7474
var v = truncf( PINF );
75-
t.strictEqual( v, PINF, 'returns +infinity' );
75+
t.strictEqual( v, PINF, 'returns expected value' );
7676
t.end();
7777
});
7878

7979
tape( 'the function returns `-infinity` if provided `-infinity`', opts, function test( t ) {
8080
var v = truncf( NINF );
81-
t.strictEqual( v, NINF, 'returns -infinity' );
81+
t.strictEqual( v, NINF, 'returns expected value' );
8282
t.end();
8383
});

lib/node_modules/@stdlib/math/base/special/truncn/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 pkg = require( './../package.json' ).name;
2727
var truncn = require( './../lib' );
@@ -34,10 +34,11 @@ bench( pkg, function benchmark( b ) {
3434
var y;
3535
var i;
3636

37+
x = uniform( 100, -5.0e6, 5.0e6 );
38+
3739
b.tic();
3840
for ( i = 0; i < b.iterations; i++ ) {
39-
x = ( randu()*1.0e7 ) - 5.0e6;
40-
y = truncn( x, -2 );
41+
y = truncn( x[ i % x.length ], -2 );
4142
if ( isnan( y ) ) {
4243
b.fail( 'should not return NaN' );
4344
}

lib/node_modules/@stdlib/math/base/special/truncn/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 tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
@@ -43,10 +43,11 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4343
var y;
4444
var i;
4545

46+
x = uniform( 100, -5.0e6, 5.0e6 );
47+
4648
b.tic();
4749
for ( i = 0; i < b.iterations; i++ ) {
48-
x = ( randu() * 1.0e7 ) - 5.0e6;
49-
y = truncn( x, -2 );
50+
y = truncn( x[ i % x.length ], -2 );
5051
if ( isnan( y ) ) {
5152
b.fail( 'should not return NaN' );
5253
}

lib/node_modules/@stdlib/math/base/special/truncn/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 v[ 100 ];
9394
double elapsed;
9495
double t;
95-
double v;
9696
double y;
9797
int i;
9898

99+
for ( i = 0; i < 100; i++ ) {
100+
v[ i ] = ( 1.0e7 * rand_double() ) - 5.0e6;
101+
}
102+
99103
t = tic();
100104
for ( i = 0; i < ITERATIONS; i++ ) {
101-
v = ( 1.0e7 * rand_double() ) - 5.0e6;
102-
y = stdlib_base_truncn( v, -2 );
105+
y = stdlib_base_truncn( v[ i%100 ], -2 );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

0 commit comments

Comments
 (0)