Skip to content

Commit d9114e0

Browse files
committed
bench: add benchmarks
1 parent 6ef72a9 commit d9114e0

2 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 real = require( '@stdlib/complex/real' );
28+
var imag = require( '@stdlib/complex/imag' );
29+
var base = require( '@stdlib/math/base/ops/cadd' );
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/base/special/cadd:value=complex_number', opts, function benchmark( b ) {
45+
var x;
46+
var y;
47+
var i;
48+
var j;
49+
50+
x = [
51+
new Complex128( 5.0, 3.0 ),
52+
new Complex128( -5.0, 3.0 ),
53+
new Complex128( 5.0, -3.0 ),
54+
new Complex128( -5.0, -3.0 ),
55+
new Complex128( 3.0, -4.0 )
56+
];
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
j = i % x.length;
61+
y = base( x[ j ], x[ j ] );
62+
if ( typeof y !== 'object' ) {
63+
b.fail( 'should return an object' );
64+
}
65+
}
66+
b.toc();
67+
if ( isnan( real( y ) ) || isnan( imag( y ) ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
});
73+
74+
// TODO: add math/special/add benchmarks
75+
76+
bench( pkg+'::mathjs:add:value=complex_number', opts, function benchmark( b ) {
77+
var x;
78+
var y;
79+
var i;
80+
var j;
81+
82+
x = [
83+
mathjs.complex( 5.0, 3.0 ),
84+
mathjs.complex( -5.0, 3.0 ),
85+
mathjs.complex( 5.0, -3.0 ),
86+
mathjs.complex( -5.0, -3.0 ),
87+
mathjs.complex( 3.0, -4.0 )
88+
];
89+
90+
b.tic();
91+
for ( i = 0; i < b.iterations; i++ ) {
92+
j = i % x.length;
93+
y = mathjs.add( x[ j ], x[ j ] );
94+
if ( typeof y !== 'object' ) {
95+
b.fail( 'should return an object' );
96+
}
97+
}
98+
b.toc();
99+
if ( isnan( mathjs.re( y ) ) || isnan( mathjs.im( y ) ) ) {
100+
b.fail( 'should not return NaN' );
101+
}
102+
b.pass( 'benchmark finished' );
103+
b.end();
104+
});
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 real = require( '@stdlib/complex/real' );
28+
var imag = require( '@stdlib/complex/imag' );
29+
var base = require( '@stdlib/math/base/ops/cmul' );
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/base/special/cmul:value=complex_number', opts, function benchmark( b ) {
45+
var x;
46+
var y;
47+
var i;
48+
var j;
49+
50+
x = [
51+
new Complex128( 5.0, 3.0 ),
52+
new Complex128( -5.0, 3.0 ),
53+
new Complex128( 5.0, -3.0 ),
54+
new Complex128( -5.0, -3.0 ),
55+
new Complex128( 3.0, -4.0 )
56+
];
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
j = i % x.length;
61+
y = base( x[ j ], x[ j ] );
62+
if ( typeof y !== 'object' ) {
63+
b.fail( 'should return an object' );
64+
}
65+
}
66+
b.toc();
67+
if ( isnan( real( y ) ) || isnan( imag( y ) ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
});
73+
74+
// TODO: add math/special/mul benchmarks
75+
76+
bench( pkg+'::mathjs:multiply:value=complex_number', opts, function benchmark( b ) {
77+
var x;
78+
var y;
79+
var i;
80+
var j;
81+
82+
x = [
83+
mathjs.complex( 5.0, 3.0 ),
84+
mathjs.complex( -5.0, 3.0 ),
85+
mathjs.complex( 5.0, -3.0 ),
86+
mathjs.complex( -5.0, -3.0 ),
87+
mathjs.complex( 3.0, -4.0 )
88+
];
89+
90+
b.tic();
91+
for ( i = 0; i < b.iterations; i++ ) {
92+
j = i % x.length;
93+
y = mathjs.multiply( x[ j ], x[ j ] );
94+
if ( typeof y !== 'object' ) {
95+
b.fail( 'should return an object' );
96+
}
97+
}
98+
b.toc();
99+
if ( isnan( mathjs.re( y ) ) || isnan( mathjs.im( y ) ) ) {
100+
b.fail( 'should not return NaN' );
101+
}
102+
b.pass( 'benchmark finished' );
103+
b.end();
104+
});

0 commit comments

Comments
 (0)