Skip to content

Commit da13191

Browse files
committed
bench: add benchmarks
1 parent 43a4b63 commit da13191

File tree

7 files changed

+722
-0
lines changed

7 files changed

+722
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var filledBy = require( '@stdlib/array/filled-by' );
27+
var zeros = require( '@stdlib/array/zeros' );
28+
var uniform = require( '@stdlib/random/base/uniform' ).factory;
29+
var dabs = require( '@stdlib/math/strided/special/dabs' );
30+
var abs = require( '@stdlib/math/special/abs' );
31+
var tryRequire = require( '@stdlib/utils/try-require' );
32+
var pkg = require( './../package.json' ).name;
33+
34+
35+
// VARIABLES //
36+
37+
var mathjs = tryRequire( resolve( __dirname, '..', 'node_modules', 'mathjs' ) );
38+
var opts = {
39+
'skip': ( mathjs instanceof Error )
40+
};
41+
42+
43+
// MAIN //
44+
45+
bench( pkg+'::stdlib:math/strided/special/dabs:value=array,dtype=float64,len=100', opts, function benchmark( b ) {
46+
var x;
47+
var y;
48+
var i;
49+
50+
x = filledBy( 100, 'float64', uniform( -100.0, 100.0 ) );
51+
y = zeros( x.length, 'float64' );
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
dabs( x.length, x, 1, y, 1 );
56+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
}
60+
b.toc();
61+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
67+
68+
bench( pkg+'::stdlib:math/special/abs:value=array,dtype=float64,len=100', opts, function benchmark( b ) {
69+
var x;
70+
var y;
71+
var i;
72+
73+
x = filledBy( 100, 'float64', uniform( -100.0, 100.0 ) );
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
y = abs( x );
78+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
}
82+
b.toc();
83+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
});
89+
90+
// NOTE: Math.js does not seem to accept typed arrays for element-wise functions
91+
bench( pkg+'::mathjs:abs:value=array,dtype=float64,len=100', {
92+
'skip': true
93+
}, function benchmark( b ) {
94+
var x;
95+
var y;
96+
var i;
97+
98+
x = filledBy( 100, 'float64', uniform( -100.0, 100.0 ) );
99+
100+
b.tic();
101+
for ( i = 0; i < b.iterations; i++ ) {
102+
y = mathjs.abs( x );
103+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
104+
b.fail( 'should not return NaN' );
105+
}
106+
}
107+
b.toc();
108+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
109+
b.fail( 'should not return NaN' );
110+
}
111+
b.pass( 'benchmark finished' );
112+
b.end();
113+
});
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var filledBy = require( '@stdlib/array/filled-by' );
27+
var zeros = require( '@stdlib/array/zeros' );
28+
var uniform = require( '@stdlib/random/base/uniform' ).factory;
29+
var stridedAbs = require( '@stdlib/math/strided/special/abs' );
30+
var abs = require( '@stdlib/math/special/abs' );
31+
var tryRequire = require( '@stdlib/utils/try-require' );
32+
var pkg = require( './../package.json' ).name;
33+
34+
35+
// VARIABLES //
36+
37+
var mathjs = tryRequire( resolve( __dirname, '..', 'node_modules', 'mathjs' ) );
38+
var opts = {
39+
'skip': ( mathjs instanceof Error )
40+
};
41+
42+
43+
// MAIN //
44+
45+
bench( pkg+'::stdlib:math/strided/special/abs:value=array,dtype=generic,len=100', opts, function benchmark( b ) {
46+
var x;
47+
var y;
48+
var i;
49+
50+
x = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) );
51+
y = zeros( x.length, 'generic' );
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
stridedAbs( x.length, 'generic', x, 1, 'generic', y, 1 );
56+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
}
60+
b.toc();
61+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
67+
68+
bench( pkg+'::stdlib:math/special/abs:value=array,dtype=generic,len=100', opts, function benchmark( b ) {
69+
var x;
70+
var y;
71+
var i;
72+
73+
x = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) );
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
y = abs( x );
78+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
}
82+
b.toc();
83+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
});
89+
90+
bench( pkg+'::mathjs:abs:value=array,dtype=generic,len=100', opts, function benchmark( b ) {
91+
var x;
92+
var y;
93+
var i;
94+
95+
x = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) );
96+
97+
b.tic();
98+
for ( i = 0; i < b.iterations; i++ ) {
99+
y = mathjs.abs( x );
100+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
101+
b.fail( 'should not return NaN' );
102+
}
103+
}
104+
b.toc();
105+
if ( isnan( y[ 0 ] ) || isnan( y[ y.length-1 ] ) ) {
106+
b.fail( 'should not return NaN' );
107+
}
108+
b.pass( 'benchmark finished' );
109+
b.end();
110+
});
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var Complex128 = require( '@stdlib/complex/float64' );
27+
var baseCabs = require( '@stdlib/math/base/special/cabs' );
28+
var abs = require( '@stdlib/math/special/abs' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var mathjs = tryRequire( resolve( __dirname, '..', 'node_modules', 'mathjs' ) );
36+
var opts = {
37+
'skip': ( mathjs instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( pkg+'::stdlib:math/base/special/cabs:value=complex_number', opts, function benchmark( b ) {
44+
var x;
45+
var y;
46+
var i;
47+
48+
x = [
49+
new Complex128( 5.0, 3.0 ),
50+
new Complex128( -5.0, 3.0 ),
51+
new Complex128( 5.0, -3.0 ),
52+
new Complex128( -5.0, -3.0 ),
53+
new Complex128( 3.0, -4.0 )
54+
];
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
y = baseCabs( x[ i%x.length ] );
59+
if ( y !== y ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnan( y ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});
70+
71+
bench( pkg+'::stdlib:math/special/abs:value=complex_number', {
72+
'skip': true
73+
}, function benchmark( b ) { // FIXME: update once complex supported in top-level abs
74+
var x;
75+
var y;
76+
var i;
77+
78+
x = [
79+
new Complex128( 5.0, 3.0 ),
80+
new Complex128( -5.0, 3.0 ),
81+
new Complex128( 5.0, -3.0 ),
82+
new Complex128( -5.0, -3.0 ),
83+
new Complex128( 3.0, -4.0 )
84+
];
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
y = abs( x[ i%x.length ] );
89+
if ( y !== y ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
}
93+
b.toc();
94+
if ( isnan( y ) ) {
95+
b.fail( 'should not return NaN' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
});
100+
101+
bench( pkg+'::mathjs:abs:value=complex_number', opts, function benchmark( b ) {
102+
var x;
103+
var y;
104+
var i;
105+
106+
x = [
107+
mathjs.complex( 5.0, 3.0 ),
108+
mathjs.complex( -5.0, 3.0 ),
109+
mathjs.complex( 5.0, -3.0 ),
110+
mathjs.complex( -5.0, -3.0 ),
111+
mathjs.complex( 3.0, -4.0 )
112+
];
113+
114+
b.tic();
115+
for ( i = 0; i < b.iterations; i++ ) {
116+
y = mathjs.abs( x[ i%x.length ] );
117+
if ( y !== y ) {
118+
b.fail( 'should not return NaN' );
119+
}
120+
}
121+
b.toc();
122+
if ( isnan( y ) ) {
123+
b.fail( 'should not return NaN' );
124+
}
125+
b.pass( 'benchmark finished' );
126+
b.end();
127+
});

0 commit comments

Comments
 (0)