Skip to content

Commit 44d5d94

Browse files
committed
bench: add benchmarks
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 5ff9b29 commit 44d5d94

File tree

2 files changed

+293
-0
lines changed

2 files changed

+293
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 ndarray = require( '@stdlib/ndarray/ctor' );
26+
var array = require( '@stdlib/ndarray/array' );
27+
var uniform = require( '@stdlib/random/array/uniform' );
28+
var tryRequire = require( '@stdlib/utils/try-require' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var mathjs = tryRequire( resolve( __dirname, '..', 'node_modules', 'mathjs' ) );
35+
var opts = {
36+
'skip': ( mathjs instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( pkg+'::stdlib:ndarray/ctor:dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) {
43+
var buf;
44+
var x;
45+
var i;
46+
47+
buf = uniform( 100*100, -10.0, 10.0, {
48+
'dtype': 'generic'
49+
});
50+
51+
b.tic();
52+
for ( i = 0; i < b.iterations; i++ ) {
53+
x = new ndarray( 'generic', buf, [ 100, 100 ], [ 100, 1 ], 0, 'row-major' );
54+
if ( typeof x !== 'object' ) {
55+
b.fail( 'should return an object' );
56+
}
57+
}
58+
b.toc();
59+
if ( typeof x !== 'object' ) {
60+
b.fail( 'should return an object' );
61+
}
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
});
65+
66+
bench( pkg+'::stdlib:ndarray/array:dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) {
67+
var buf;
68+
var x;
69+
var i;
70+
71+
buf = uniform( 100*100, -10.0, 10.0, {
72+
'dtype': 'generic'
73+
});
74+
75+
b.tic();
76+
for ( i = 0; i < b.iterations; i++ ) {
77+
x = array( buf, {
78+
'shape': [ 100, 100 ],
79+
'dtype': 'generic',
80+
'flatten': false
81+
});
82+
if ( typeof x !== 'object' ) {
83+
b.fail( 'should return an object' );
84+
}
85+
}
86+
b.toc();
87+
if ( typeof x !== 'object' ) {
88+
b.fail( 'should return an object' );
89+
}
90+
b.pass( 'benchmark finished' );
91+
b.end();
92+
});
93+
94+
bench( pkg+'::mathjs:matrix:dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) {
95+
var buf;
96+
var x;
97+
var i;
98+
99+
buf = mathjs.random( [ 100, 100 ] );
100+
101+
b.tic();
102+
for ( i = 0; i < b.iterations; i++ ) {
103+
x = mathjs.matrix( buf, 'dense', 'number' );
104+
if ( typeof x !== 'object' ) {
105+
b.fail( 'should return an object' );
106+
}
107+
}
108+
b.toc();
109+
if ( typeof x !== 'object' ) {
110+
b.fail( 'should return an object' );
111+
}
112+
b.pass( 'benchmark finished' );
113+
b.end();
114+
});
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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 filled2dBy = require( '@stdlib/array/base/filled2d-by' );
27+
var filledndBy = require( '@stdlib/array/base/fillednd-by' );
28+
var unary2d = require( '@stdlib/array/base/unary2d' );
29+
var unarynd = require( '@stdlib/array/base/unarynd' );
30+
var zeros2d = require( '@stdlib/array/base/zeros2d' );
31+
var zerosnd = require( '@stdlib/array/base/zerosnd' );
32+
var ndmap = require( '@stdlib/ndarray/map' );
33+
var ndzeros = require( '@stdlib/ndarray/zeros' );
34+
var ndunary = require( '@stdlib/ndarray/base/unary' );
35+
var uniform = require( '@stdlib/random/base/uniform' ).factory;
36+
var randu = require( '@stdlib/random/uniform' );
37+
var base = require( '@stdlib/math/base/special/sqrt' );
38+
var tryRequire = require( '@stdlib/utils/try-require' );
39+
var pkg = require( './../package.json' ).name;
40+
41+
42+
// VARIABLES //
43+
44+
var mathjs = tryRequire( resolve( __dirname, '..', 'node_modules', 'mathjs' ) );
45+
var opts = {
46+
'skip': ( mathjs instanceof Error )
47+
};
48+
49+
50+
// MAIN //
51+
52+
bench( pkg+'::stdlib:array/base/unary2d:value=nested_array,dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) {
53+
var sh;
54+
var x;
55+
var y;
56+
var i;
57+
58+
sh = [ 100, 100 ];
59+
x = filled2dBy( sh, uniform( 0.0, 100.0 ) );
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
y = zeros2d( sh );
64+
unary2d( [ x, y ], sh, base );
65+
if ( isnan( y[ 0 ][ 0 ] ) || isnan( y[ 99 ][ 99 ] ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( y[ 1 ][ 1 ] ) || isnan( y[ 98 ][ 98 ] ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
76+
77+
bench( pkg+'::stdlib:array/base/unarynd:value=nested_array,dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) {
78+
var sh;
79+
var x;
80+
var y;
81+
var i;
82+
83+
sh = [ 100, 100 ];
84+
x = filledndBy( sh, uniform( 0.0, 100.0 ) );
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
y = zerosnd( sh );
89+
unarynd( [ x, y ], sh, base );
90+
if ( isnan( y[ 0 ][ 0 ] ) || isnan( y[ 99 ][ 99 ] ) ) {
91+
b.fail( 'should not return NaN' );
92+
}
93+
}
94+
b.toc();
95+
if ( isnan( y[ 1 ][ 1 ] ) || isnan( y[ 98 ][ 98 ] ) ) {
96+
b.fail( 'should not return NaN' );
97+
}
98+
b.pass( 'benchmark finished' );
99+
b.end();
100+
});
101+
102+
bench( pkg+'::stdlib:ndarray/base/unary:value=ndarray,dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) {
103+
var opts;
104+
var sh;
105+
var x;
106+
var y;
107+
var i;
108+
109+
sh = [ 100, 100 ];
110+
opts = {
111+
'dtype': 'generic'
112+
};
113+
x = randu( sh, 0.0, 100.0, opts );
114+
115+
b.tic();
116+
for ( i = 0; i < b.iterations; i++ ) {
117+
y = ndzeros( sh, opts );
118+
ndunary( [ x, y ], base );
119+
if ( isnan( y.get( 0, 0 ) ) || isnan( y.get( 99, 99 ) ) ) {
120+
b.fail( 'should not return NaN' );
121+
}
122+
}
123+
b.toc();
124+
if ( isnan( y.get( 1, 1 ) ) || isnan( y.get( 98, 98) ) ) {
125+
b.fail( 'should not return NaN' );
126+
}
127+
b.pass( 'benchmark finished' );
128+
b.end();
129+
});
130+
131+
bench( pkg+'::stdlib:ndarray/map:value=ndarray,dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) {
132+
var sh;
133+
var x;
134+
var y;
135+
var i;
136+
137+
sh = [ 100, 100 ];
138+
x = randu( sh, 0.0, 100.0, {
139+
'dtype': 'generic'
140+
});
141+
142+
b.tic();
143+
for ( i = 0; i < b.iterations; i++ ) {
144+
y = ndmap( x, base );
145+
if ( isnan( y.get( 0, 0 ) ) || isnan( y.get( 99, 99 ) ) ) {
146+
b.fail( 'should not return NaN' );
147+
}
148+
}
149+
b.toc();
150+
if ( isnan( y.get( 1, 1 ) ) || isnan( y.get( 98, 98) ) ) {
151+
b.fail( 'should not return NaN' );
152+
}
153+
b.pass( 'benchmark finished' );
154+
b.end();
155+
});
156+
157+
bench( pkg+'::mathjs:sqrt:value=matrix,dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) {
158+
var buf;
159+
var x;
160+
var y;
161+
var i;
162+
163+
buf = mathjs.random( [ 100, 100 ], 0, 100 );
164+
x = mathjs.matrix( buf, 'dense', 'number' );
165+
166+
b.tic();
167+
for ( i = 0; i < b.iterations; i++ ) {
168+
y = x.map( base );
169+
if ( typeof y !== 'object' ) {
170+
b.fail( 'should return an object' );
171+
}
172+
}
173+
b.toc();
174+
if ( isnan( y.get( [ 0, 0 ] ) ) || isnan( y.get( [ 99, 99 ] ) ) ) {
175+
b.fail( 'should not return NaN' );
176+
}
177+
b.pass( 'benchmark finished' );
178+
b.end();
179+
});

0 commit comments

Comments
 (0)