Skip to content

Commit b1448e4

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

File tree

8 files changed

+56
-38
lines changed

8 files changed

+56
-38
lines changed

lib/node_modules/@stdlib/math/base/special/absf/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 isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pkg = require( './../package.json' ).name;
2727
var absf = 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 = absf( x );
43+
y = absf( x[ i % x.length ] );
4144
if ( isnan( 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 = Math.abs( x ); // eslint-disable-line stdlib/no-builtin-math
67+
y = Math.abs( x[ i % x.length ] ); // eslint-disable-line stdlib/no-builtin-math
6268
if ( isnan( y ) ) {
6369
b.fail( 'should not return NaN' );
6470
}

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

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,19 @@ static float rand_float( void ) {
8989
* @return elapsed time in seconds
9090
*/
9191
static double benchmark( void ) {
92+
float x[ 100 ];
9293
double elapsed;
9394
double t;
94-
float x;
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 = fabsf( x );
104+
y = fabsf( x[ i % 100 ] );
102105
if ( y != y ) {
103106
printf( "should not return NaN\n" );
104107
break;

lib/node_modules/@stdlib/math/base/special/absf/benchmark/c/cephes/benchmark.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,19 @@ static float rand_float( void ) {
9494
* @return elapsed time in seconds
9595
*/
9696
static double benchmark( void ) {
97+
float x[ 100 ];
9798
double elapsed;
9899
double t;
99-
float x;
100100
float y;
101101
int i;
102102

103+
for ( i = 0; i < 100; i++ ) {
104+
x[ i ] = ( 1000.0f*rand_float() ) - 500.0f;
105+
}
106+
103107
t = tic();
104108
for ( i = 0; i < ITERATIONS; i++ ) {
105-
x = ( 1000.0f*rand_float() ) - 500.0f;
106-
y = fabsf( x );
109+
y = fabsf( x[ i % 100 ] );
107110
if ( y != y ) {
108111
printf( "should not return NaN\n" );
109112
break;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,19 @@ static float rand_float( void ) {
9191
* @return elapsed time in seconds
9292
*/
9393
static double benchmark( void ) {
94+
float x[ 100 ];
9495
double elapsed;
9596
double t;
96-
float x;
9797
float y;
9898
int i;
9999

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

lib/node_modules/@stdlib/math/base/special/absf/test/test.abs.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 absolute value of a number', function test( t ) {
40-
t.equal( absf( -2.0 ), 2.0, 'negative number' );
41-
t.equal( absf( 3.0 ), 3.0, 'positive number' );
42-
t.equal( absf( 0.0 ), 0.0, 'zero' );
40+
t.equal( absf( -2.0 ), 2.0, 'returns expected value' );
41+
t.equal( absf( 3.0 ), 3.0, 'returns expected value' );
42+
t.equal( absf( 0.0 ), 0.0, 'returns expected value' );
4343
t.end();
4444
});
4545

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

5151
tape( 'the function computes the absolute value of infinity', function test( t ) {
52-
t.equal( absf( PINF ), PINF, 'returns +infinity' );
53-
t.equal( absf( NINF ), PINF, 'returns +infinity' );
52+
t.equal( absf( PINF ), PINF, 'returns expected value' );
53+
t.equal( absf( 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 = absf( NaN );
59-
t.equal( isnanf( v ), true, 'returns NaN' );
59+
t.equal( isnanf( v ), true, 'returns expected value' );
6060
t.end();
6161
});

lib/node_modules/@stdlib/math/base/special/absf/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 absolute value of a number', function test( t ) {
40-
t.equal( absf( -2.0 ), 2.0, 'negative number' );
41-
t.equal( absf( 3.0 ), 3.0, 'positive number' );
42-
t.equal( absf( 0.0 ), 0.0, 'zero' );
40+
t.equal( absf( -2.0 ), 2.0, 'returns expected value' );
41+
t.equal( absf( 3.0 ), 3.0, 'returns expected value' );
42+
t.equal( absf( 0.0 ), 0.0, 'returns expected value' );
4343
t.end();
4444
});
4545

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

5151
tape( 'the function computes the absolute value of infinity', function test( t ) {
52-
t.equal( absf( PINF ), PINF, 'returns +infinity' );
53-
t.equal( absf( NINF ), PINF, 'returns +infinity' );
52+
t.equal( absf( PINF ), PINF, 'returns expected value' );
53+
t.equal( absf( 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 = absf( NaN );
59-
t.equal( isnanf( v ), true, 'returns NaN' );
59+
t.equal( isnanf( v ), true, 'returns expected value' );
6060
t.end();
6161
});

lib/node_modules/@stdlib/math/base/special/absf/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 absolute value of a number', opts, function test( t ) {
49-
t.equal( absf( -2.0 ), 2.0, 'negative number' );
50-
t.equal( absf( 3.0 ), 3.0, 'positive number' );
51-
t.equal( absf( 0.0 ), 0.0, 'zero' );
49+
t.equal( absf( -2.0 ), 2.0, 'returns expected value' );
50+
t.equal( absf( 3.0 ), 3.0, 'returns expected value' );
51+
t.equal( absf( 0.0 ), 0.0, 'returns expected value' );
5252
t.end();
5353
});
5454

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

6060
tape( 'the function computes the absolute value of infinity', opts, function test( t ) {
61-
t.equal( absf( PINF ), PINF, 'returns +infinity' );
62-
t.equal( absf( NINF ), PINF, 'returns +infinity' );
61+
t.equal( absf( PINF ), PINF, 'returns expected value' );
62+
t.equal( absf( 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 = absf( NaN );
68-
t.equal( isnanf( v ), true, 'returns NaN' );
68+
t.equal( isnanf( v ), true, 'returns expected value' );
6969
t.end();
7070
});

0 commit comments

Comments
 (0)