Skip to content

Commit 682c947

Browse files
committed
bench: add benchmarks
1 parent 95aeb15 commit 682c947

15 files changed

+1252
-2
lines changed

docs/migration-guides/mathjs/benchmark/benchmark.abs.number.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var bench = require( '@stdlib/bench' );
2525
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2626
var filledBy = require( '@stdlib/array/filled-by' );
2727
var uniform = require( '@stdlib/random/base/uniform' ).factory;
28-
var baseAbs = require( '@stdlib/math/base/special/abs' );
28+
var base = require( '@stdlib/math/base/special/abs' );
2929
var abs = require( '@stdlib/math/special/abs' );
3030
var tryRequire = require( '@stdlib/utils/try-require' );
3131
var pkg = require( './../package.json' ).name;
@@ -50,7 +50,7 @@ bench( pkg+'::stdlib:math/base/special/abs:value=number', opts, function benchma
5050

5151
b.tic();
5252
for ( i = 0; i < b.iterations; i++ ) {
53-
y = baseAbs( x[ i%x.length ] );
53+
y = base( x[ i%x.length ] );
5454
if ( y !== y ) {
5555
b.fail( 'should not return NaN' );
5656
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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/ops/add' );
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/ops/add:value=number', opts, function benchmark( b ) {
44+
var x;
45+
var y;
46+
var z;
47+
var i;
48+
var j;
49+
50+
x = filledBy( 100, uniform( -100.0, 100.0 ) );
51+
y = filledBy( 100, uniform( -100.0, 100.0 ) );
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
j = i % x.length;
56+
z = base( x[ j ], y[ j ] );
57+
if ( z !== z ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( z ) ) {
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=number', opts, function benchmark( b ) {
72+
var x;
73+
var y;
74+
var z;
75+
var i;
76+
var j;
77+
78+
x = filledBy( 100, uniform( -100.0, 100.0 ) );
79+
y = filledBy( 100, uniform( -100.0, 100.0 ) );
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
j = i % x.length;
84+
z = mathjs.add( x[ j ], y[ j ] );
85+
if ( z !== z ) {
86+
b.fail( 'should not return NaN' );
87+
}
88+
}
89+
b.toc();
90+
if ( isnan( z ) ) {
91+
b.fail( 'should not return NaN' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
});
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/cbrt' );
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/cbrt:value=number', opts, function benchmark( b ) {
44+
var x;
45+
var y;
46+
var i;
47+
48+
x = filledBy( 100, uniform( 0.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/cbrt benchmarks
66+
67+
bench( pkg+'::mathjs:cbrt:value=number', opts, function benchmark( b ) {
68+
var x;
69+
var y;
70+
var i;
71+
72+
x = filledBy( 100, uniform( 0.0, 100.0 ) );
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
y = mathjs.cbrt( 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+
});
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/ceil' );
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/ceil: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/ceil benchmarks
66+
67+
bench( pkg+'::mathjs:ceil: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.ceil( 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+
});
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/exp' );
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/exp: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/exp benchmarks
66+
67+
bench( pkg+'::mathjs:exp: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.exp( 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)