Skip to content

Commit c713f5a

Browse files
committed
bench: add 1d and 2d 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 ceb856d commit c713f5a

File tree

2 files changed

+312
-0
lines changed

2 files changed

+312
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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 bench = require( '@stdlib/bench' );
24+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var add = require( '@stdlib/number/float64/base/add' );
28+
var filledarray = require( '@stdlib/array/filled' );
29+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
30+
var orders = require( '@stdlib/ndarray/orders' );
31+
var pkg = require( './../package.json' ).name;
32+
var binary = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var TYPES = [
38+
'float64'
39+
];
40+
var ORDERS = orders();
41+
42+
43+
// FUNCTIONS //
44+
45+
/**
46+
* Creates a benchmark function.
47+
*
48+
* @private
49+
* @param {PositiveInteger} len - ndarray length
50+
* @param {NonNegativeIntegerArray} shape - ndarray shape
51+
* @param {string} xtype - input ndarray data type
52+
* @param {string} ytype - output ndarray data type
53+
* @param {string} order - memory layout
54+
* @returns {Function} benchmark function
55+
*/
56+
function createBenchmark( len, shape, xtype, ytype, order ) {
57+
var x;
58+
var y;
59+
60+
x = discreteUniform( len, -100, 100, {
61+
'dtype': xtype
62+
});
63+
y = filledarray( 0.0, len, ytype );
64+
65+
x = {
66+
'dtype': xtype,
67+
'data': x,
68+
'shape': shape,
69+
'strides': shape2strides( shape, order ),
70+
'offset': 0,
71+
'order': order
72+
};
73+
y = {
74+
'dtype': ytype,
75+
'data': y,
76+
'shape': shape,
77+
'strides': shape2strides( shape, order ),
78+
'offset': 0,
79+
'order': order
80+
};
81+
return benchmark;
82+
83+
/**
84+
* Benchmark function.
85+
*
86+
* @private
87+
* @param {Benchmark} b - benchmark instance
88+
*/
89+
function benchmark( b ) {
90+
var i;
91+
92+
b.tic();
93+
for ( i = 0; i < b.iterations; i++ ) {
94+
binary( [ x, x, y ], add );
95+
if ( isnan( y.data[ i%len ] ) ) {
96+
b.fail( 'should not return NaN' );
97+
}
98+
}
99+
b.toc();
100+
if ( isnan( y.data[ i%len ] ) ) {
101+
b.fail( 'should not return NaN' );
102+
}
103+
b.pass( 'benchmark finished' );
104+
b.end();
105+
}
106+
}
107+
108+
109+
// MAIN //
110+
111+
/**
112+
* Main execution sequence.
113+
*
114+
* @private
115+
*/
116+
function main() {
117+
var len;
118+
var min;
119+
var max;
120+
var ord;
121+
var sh;
122+
var t1;
123+
var t2;
124+
var f;
125+
var i;
126+
var j;
127+
var k;
128+
129+
min = 1; // 10^min
130+
max = 6; // 10^max
131+
132+
for ( k = 0; k < ORDERS.length; k++ ) {
133+
ord = ORDERS[ k ];
134+
for ( j = 0; j < TYPES.length; j++ ) {
135+
t1 = TYPES[ j ];
136+
t2 = TYPES[ j ];
137+
for ( i = min; i <= max; i++ ) {
138+
len = pow( 10, i );
139+
140+
sh = [ len ];
141+
f = createBenchmark( len, sh, t1, t2, ord );
142+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f );
143+
}
144+
}
145+
}
146+
}
147+
148+
main();
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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 bench = require( '@stdlib/bench' );
24+
var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var sqrt = require( '@stdlib/math/base/special/sqrt' );
29+
var floor = require( '@stdlib/math/base/special/floor' );
30+
var add = require( '@stdlib/number/float64/base/add' );
31+
var filledarray = require( '@stdlib/array/filled' );
32+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
33+
var orders = require( '@stdlib/ndarray/orders' );
34+
var pkg = require( './../package.json' ).name;
35+
var binary = require( './../lib/2d.js' );
36+
37+
38+
// VARIABLES //
39+
40+
var TYPES = [
41+
'float64'
42+
];
43+
var ORDERS = orders();
44+
45+
46+
// FUNCTIONS //
47+
48+
/**
49+
* Creates a benchmark function.
50+
*
51+
* @private
52+
* @param {PositiveInteger} len - ndarray length
53+
* @param {NonNegativeIntegerArray} shape - ndarray shape
54+
* @param {string} xtype - input ndarray data type
55+
* @param {string} ytype - output ndarray data type
56+
* @param {string} order - memory layout
57+
* @returns {Function} benchmark function
58+
*/
59+
function createBenchmark( len, shape, xtype, ytype, order ) {
60+
var isrm;
61+
var x;
62+
var y;
63+
64+
isrm = isRowMajor( order );
65+
66+
x = discreteUniform( len, -100, 100, {
67+
'dtype': xtype
68+
});
69+
y = filledarray( 0.0, len, ytype );
70+
71+
x = {
72+
'dtype': xtype,
73+
'data': x,
74+
'shape': shape,
75+
'strides': shape2strides( shape, order ),
76+
'offset': 0,
77+
'order': order
78+
};
79+
y = {
80+
'dtype': ytype,
81+
'data': y,
82+
'shape': shape,
83+
'strides': shape2strides( shape, order ),
84+
'offset': 0,
85+
'order': order
86+
};
87+
return benchmark;
88+
89+
/**
90+
* Benchmark function.
91+
*
92+
* @private
93+
* @param {Benchmark} b - benchmark instance
94+
*/
95+
function benchmark( b ) {
96+
var i;
97+
98+
b.tic();
99+
for ( i = 0; i < b.iterations; i++ ) {
100+
binary( x, x, y, isrm, add );
101+
if ( isnan( y.data[ i%len ] ) ) {
102+
b.fail( 'should not return NaN' );
103+
}
104+
}
105+
b.toc();
106+
if ( isnan( y.data[ i%len ] ) ) {
107+
b.fail( 'should not return NaN' );
108+
}
109+
b.pass( 'benchmark finished' );
110+
b.end();
111+
}
112+
}
113+
114+
115+
// MAIN //
116+
117+
/**
118+
* Main execution sequence.
119+
*
120+
* @private
121+
*/
122+
function main() {
123+
var len;
124+
var min;
125+
var max;
126+
var ord;
127+
var sh;
128+
var t1;
129+
var t2;
130+
var f;
131+
var i;
132+
var j;
133+
var k;
134+
135+
min = 1; // 10^min
136+
max = 6; // 10^max
137+
138+
for ( k = 0; k < ORDERS.length; k++ ) {
139+
ord = ORDERS[ k ];
140+
for ( j = 0; j < TYPES.length; j++ ) {
141+
t1 = TYPES[ j ];
142+
t2 = TYPES[ j ];
143+
for ( i = min; i <= max; i++ ) {
144+
len = pow( 10, i );
145+
146+
sh = [ len/2, 2 ];
147+
f = createBenchmark( len, sh, t1, t2, ord );
148+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f );
149+
150+
sh = [ 2, len/2 ];
151+
f = createBenchmark( len, sh, t1, t2, ord );
152+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f );
153+
154+
len = floor( sqrt( len ) );
155+
sh = [ len, len ];
156+
len *= len;
157+
f = createBenchmark( len, sh, t1, t2, ord );
158+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f );
159+
}
160+
}
161+
}
162+
}
163+
164+
main();

0 commit comments

Comments
 (0)