Skip to content

Commit b385506

Browse files
bench: refactor benchmarks and update test messages in math/base/special/abs2
PR-URL: #4972 Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent b1448e4 commit b385506

File tree

6 files changed

+39
-30
lines changed

6 files changed

+39
-30
lines changed

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

Lines changed: 7 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 randu = require( '@stdlib/random/array/uniform' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var pkg = require( './../package.json' ).name;
2727
var abs2 = require( './../lib' );
@@ -34,10 +34,11 @@ bench( pkg, function benchmark( b ) {
3434
var y;
3535
var i;
3636

37+
x = randu( 100, -500.0, 500.0 );
38+
3739
b.tic();
3840
for ( i = 0; i < b.iterations; i++ ) {
39-
x = ( randu()*1000.0 ) - 500.0;
40-
y = abs2( x );
41+
y = abs2( x[ i % x.length ] );
4142
if ( isnan( y ) ) {
4243
b.fail( 'should not return NaN' );
4344
}
@@ -55,10 +56,11 @@ bench( pkg+'::built-in', function benchmark( b ) {
5556
var y;
5657
var i;
5758

59+
x = randu( 100, -500.0, 500.0 );
60+
5861
b.tic();
5962
for ( i = 0; i < b.iterations; i++ ) {
60-
x = ( randu()*1000.0 ) - 500.0;
61-
y = x * x;
63+
y = x[ i % x.length ] * x[ i % x.length ];
6264
if ( isnan( y ) ) {
6365
b.fail( 'should not return NaN' );
6466
}

lib/node_modules/@stdlib/math/base/special/abs2/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 randu = 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 = randu( 100, -500.0, 500.0 );
47+
4648
b.tic();
4749
for ( i = 0; i < b.iterations; i++ ) {
48-
x = ( randu()*1000.0 ) - 500.0;
49-
y = abs2( x );
50+
y = abs2( x[ i % x.length ] );
5051
if ( isnan( y ) ) {
5152
b.fail( 'should not return NaN' );
5253
}

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

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

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

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

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

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

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

4040
tape( 'the function computes the squared absolute value of a number', function test( t ) {
41-
t.strictEqual( abs2( -2.0 ), 4.0, 'negative number' );
42-
t.strictEqual( abs2( 3.0 ), 9.0, 'positive number' );
43-
t.strictEqual( abs2( 0.0 ), 0.0, 'zero' );
44-
t.strictEqual( abs2( -PI ), PI*PI, 'pi' );
41+
t.strictEqual( abs2( -2.0 ), 4.0, 'returns expected value' );
42+
t.strictEqual( abs2( 3.0 ), 9.0, 'returns expected value' );
43+
t.strictEqual( abs2( 0.0 ), 0.0, 'returns expected value' );
44+
t.strictEqual( abs2( -PI ), PI*PI, 'returns expected value' );
4545
t.end();
4646
});
4747

4848
tape( 'the function computes the squared absolute value of negative zero', function test( t ) {
49-
t.strictEqual( isPositiveZero( abs2( -0.0 ) ), true, 'returns positive zero' );
49+
t.strictEqual( isPositiveZero( abs2( -0.0 ) ), true, 'returns expected value' );
5050
t.end();
5151
});
5252

5353
tape( 'the function computes the squared absolute value of infinity', function test( t ) {
54-
t.strictEqual( abs2( PINF ), PINF, 'returns +infinity' );
55-
t.strictEqual( abs2( NINF ), PINF, 'returns +infinity' );
54+
t.strictEqual( abs2( PINF ), PINF, 'returns expected value' );
55+
t.strictEqual( abs2( NINF ), PINF, 'returns expected value' );
5656
t.end();
5757
});
5858

5959
tape( 'if provided `NaN`, the function returns `NaN`', function test( t ) {
6060
var v = abs2( NaN );
61-
t.strictEqual( isnan( v ), true, 'returns NaN' );
61+
t.strictEqual( isnan( v ), true, 'returns expected value' );
6262
t.end();
6363
});

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

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

4949
tape( 'the function computes the squared absolute value of a number', opts, function test( t ) {
50-
t.strictEqual( abs2( -2.0 ), 4.0, 'negative number' );
51-
t.strictEqual( abs2( 3.0 ), 9.0, 'positive number' );
52-
t.strictEqual( abs2( 0.0 ), 0.0, 'zero' );
53-
t.strictEqual( abs2( -PI ), PI*PI, 'pi' );
50+
t.strictEqual( abs2( -2.0 ), 4.0, 'returns expected value' );
51+
t.strictEqual( abs2( 3.0 ), 9.0, 'returns expected value' );
52+
t.strictEqual( abs2( 0.0 ), 0.0, 'returns expected value' );
53+
t.strictEqual( abs2( -PI ), PI*PI, 'returns expected value' );
5454
t.end();
5555
});
5656

5757
tape( 'the function computes the squared absolute value of negative zero', opts, function test( t ) {
58-
t.strictEqual( isPositiveZero( abs2( -0.0 ) ), true, 'returns positive zero' );
58+
t.strictEqual( isPositiveZero( abs2( -0.0 ) ), true, 'returns expected value' );
5959
t.end();
6060
});
6161

6262
tape( 'the function computes the squared absolute value of infinity', opts, function test( t ) {
63-
t.strictEqual( abs2( PINF ), PINF, 'returns +infinity' );
64-
t.strictEqual( abs2( NINF ), PINF, 'returns +infinity' );
63+
t.strictEqual( abs2( PINF ), PINF, 'returns expected value' );
64+
t.strictEqual( abs2( NINF ), PINF, 'returns expected value' );
6565
t.end();
6666
});
6767

6868
tape( 'if provided `NaN`, the function returns `NaN`', opts, function test( t ) {
6969
var v = abs2( NaN );
70-
t.strictEqual( isnan( v ), true, 'returns NaN' );
70+
t.strictEqual( isnan( v ), true, 'returns expected value' );
7171
t.end();
7272
});

0 commit comments

Comments
 (0)