Skip to content

Commit 39b5912

Browse files
committed
bench: add benchmarks
1 parent a462630 commit 39b5912

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 strided = require( '@stdlib/math/strided/ops/add' );
30+
var tryRequire = require( '@stdlib/utils/try-require' );
31+
var pkg = require( './../package.json' ).name;
32+
33+
34+
// VARIABLES //
35+
36+
var mathjs = tryRequire( resolve( __dirname, '..', 'node_modules', 'mathjs' ) );
37+
var opts = {
38+
'skip': ( mathjs instanceof Error )
39+
};
40+
41+
42+
// MAIN //
43+
44+
bench( pkg+'::stdlib:math/strided/ops/add:value=array,dtype=generic,len=100', opts, function benchmark( b ) {
45+
var x;
46+
var y;
47+
var z;
48+
var i;
49+
50+
x = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) );
51+
y = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) );
52+
z = zeros( x.length, 'generic' );
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
strided( x.length, 'generic', x, 1, 'generic', y, 1, 'generic', z, 1 );
57+
if ( isnan( z[ 0 ] ) || isnan( z[ z.length-1 ] ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( z[ 0 ] ) || isnan( z[ z.length-1 ] ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
});
68+
69+
// TODO: add math/ops/add benchmarks
70+
71+
bench( pkg+'::mathjs:add:value=array,dtype=generic,len=100', opts, function benchmark( b ) {
72+
var x;
73+
var y;
74+
var z;
75+
var i;
76+
77+
x = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) );
78+
y = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) );
79+
80+
b.tic();
81+
for ( i = 0; i < b.iterations; i++ ) {
82+
z = mathjs.add( x, y );
83+
if ( isnan( z[ 0 ] ) || isnan( z[ z.length-1 ] ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
}
87+
b.toc();
88+
if ( isnan( z[ 0 ] ) || isnan( z[ z.length-1 ] ) ) {
89+
b.fail( 'should not return NaN' );
90+
}
91+
b.pass( 'benchmark finished' );
92+
b.end();
93+
});
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 uniform = require( '@stdlib/random/base/uniform' ).factory;
28+
var base = require( '@stdlib/math/base/special/erf' );
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/erf:value=number', opts, function benchmark( b ) {
44+
var x;
45+
var y;
46+
var i;
47+
48+
x = filledBy( 100, uniform( -100.0, 100.0 ) );
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
y = base( x[ i%x.length ] );
53+
if ( y !== y ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnan( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});
64+
65+
// TODO: add math/special/erf benchmarks
66+
67+
bench( pkg+'::mathjs:erf:value=number', opts, function benchmark( b ) {
68+
var x;
69+
var y;
70+
var i;
71+
72+
x = filledBy( 100, uniform( -100.0, 100.0 ) );
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
y = mathjs.erf( x[ i%x.length ] );
77+
if ( y !== y ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( y ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
});

0 commit comments

Comments
 (0)