Skip to content

Commit b0abce9

Browse files
bench: refactor benchmarks and update test messages in math/base/special/abs2f
PR-URL: #5258 Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent c15d420 commit b0abce9

File tree

6 files changed

+43
-28
lines changed

6 files changed

+43
-28
lines changed

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

37+
x = uniform( 100, -500.0, 500.0, {
38+
'dtype': 'float32'
39+
});
40+
3741
b.tic();
3842
for ( i = 0; i < b.iterations; i++ ) {
39-
x = ( randu()*1000.0 ) - 500.0;
40-
y = abs2f( x );
43+
y = abs2f( x[ i % x.length ] );
4144
if ( isnanf( y ) ) {
4245
b.fail( 'should not return NaN' );
4346
}
@@ -55,10 +58,13 @@ bench( pkg+'::built-in', function benchmark( b ) {
5558
var y;
5659
var i;
5760

61+
x = uniform( 100, -500.0, 500.0, {
62+
'dtype': 'float32'
63+
});
64+
5865
b.tic();
5966
for ( i = 0; i < b.iterations; i++ ) {
60-
x = ( randu()*1000.0 ) - 500.0;
61-
y = x * x;
67+
y = x[ i % x.length ] * x[ i % x.length ];
6268
if ( isnanf( y ) ) {
6369
b.fail( 'should not return NaN' );
6470
}

lib/node_modules/@stdlib/math/base/special/abs2f/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 = abs2f( x );
52+
y = abs2f( x[ i % x.length ] );
5053
if ( isnanf( y ) ) {
5154
b.fail( 'should not return NaN' );
5255
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,19 @@ float abs2f( float x ) {
9999
* @return elapsed time in seconds
100100
*/
101101
static double benchmark( void ) {
102+
float x[ 100 ];
102103
double elapsed;
103104
double t;
104-
float x;
105105
float y;
106106
int i;
107107

108+
for ( i = 0; i < 100; i++ ) {
109+
x[ i ] = ( 1000.0f*rand_float() ) - 500.0f;
110+
}
111+
108112
t = tic();
109113
for ( i = 0; i < ITERATIONS; i++ ) {
110-
x = ( 1000.0f*rand_float() ) - 500.0f;
111-
y = abs2f( x );
114+
y = abs2f( x[ i % 100 ] );
112115
if ( y != y ) {
113116
printf( "should not return NaN\n" );
114117
break;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,19 @@ static float rand_float( void ) {
9090
* @return elapsed time in seconds
9191
*/
9292
static double benchmark( void ) {
93+
float x[ 100 ];
9394
double elapsed;
9495
double t;
95-
float x;
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_abs2f( x );
105+
y = stdlib_base_abs2f( x[ i % 100 ] );
103106
if ( y != y ) {
104107
printf( "should not return NaN\n" );
105108
break;

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

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

3939
tape( 'the function computes the squared absolute value of a number', function test( t ) {
40-
t.strictEqual( abs2f( -2.0 ), 4.0, 'negative number' );
41-
t.strictEqual( abs2f( 3.0 ), 9.0, 'positive number' );
42-
t.strictEqual( abs2f( 0.0 ), 0.0, 'zero' );
40+
t.strictEqual( abs2f( -2.0 ), 4.0, 'returns expected value' );
41+
t.strictEqual( abs2f( 3.0 ), 9.0, 'returns expected value' );
42+
t.strictEqual( abs2f( 0.0 ), 0.0, 'returns expected value' );
4343
t.end();
4444
});
4545

4646
tape( 'the function computes the squared absolute value of negative zero', function test( t ) {
47-
t.strictEqual( isPositiveZerof( abs2f( -0.0 ) ), true, 'returns positive zero' );
47+
t.strictEqual( isPositiveZerof( abs2f( -0.0 ) ), true, 'returns expected value' );
4848
t.end();
4949
});
5050

5151
tape( 'the function computes the squared absolute value of infinity', function test( t ) {
52-
t.strictEqual( abs2f( PINF ), PINF, 'returns +infinity' );
53-
t.strictEqual( abs2f( NINF ), PINF, 'returns +infinity' );
52+
t.strictEqual( abs2f( PINF ), PINF, 'returns expected value' );
53+
t.strictEqual( abs2f( NINF ), PINF, 'returns expected value' );
5454
t.end();
5555
});
5656

5757
tape( 'if provided `NaN`, the function returns `NaN`', function test( t ) {
5858
var v = abs2f( NaN );
59-
t.strictEqual( isnanf( v ), true, 'returns NaN' );
59+
t.strictEqual( isnanf( v ), true, 'returns expected value' );
6060
t.end();
6161
});

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

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

4848
tape( 'the function computes the squared absolute value of a number', opts, function test( t ) {
49-
t.strictEqual( abs2f( -2.0 ), 4.0, 'negative number' );
50-
t.strictEqual( abs2f( 3.0 ), 9.0, 'positive number' );
51-
t.strictEqual( abs2f( 0.0 ), 0.0, 'zero' );
49+
t.strictEqual( abs2f( -2.0 ), 4.0, 'returns expected value' );
50+
t.strictEqual( abs2f( 3.0 ), 9.0, 'returns expected value' );
51+
t.strictEqual( abs2f( 0.0 ), 0.0, 'returns expected value' );
5252
t.end();
5353
});
5454

5555
tape( 'the function computes the squared absolute value of negative zero', opts, function test( t ) {
56-
t.strictEqual( isPositiveZerof( abs2f( -0.0 ) ), true, 'returns positive zero' );
56+
t.strictEqual( isPositiveZerof( abs2f( -0.0 ) ), true, 'returns expected value' );
5757
t.end();
5858
});
5959

6060
tape( 'the function computes the squared absolute value of infinity', opts, function test( t ) {
61-
t.strictEqual( abs2f( PINF ), PINF, 'returns +infinity' );
62-
t.strictEqual( abs2f( NINF ), PINF, 'returns +infinity' );
61+
t.strictEqual( abs2f( PINF ), PINF, 'returns expected value' );
62+
t.strictEqual( abs2f( NINF ), PINF, 'returns expected value' );
6363
t.end();
6464
});
6565

6666
tape( 'if provided `NaN`, the function returns `NaN`', opts, function test( t ) {
6767
var v = abs2f( NaN );
68-
t.strictEqual( isnanf( v ), true, 'returns NaN' );
68+
t.strictEqual( isnanf( v ), true, 'returns expected value' );
6969
t.end();
7070
});

0 commit comments

Comments
 (0)