Skip to content

Commit c0c736b

Browse files
kgrytesaurabhraghuvanshii
authored andcommitted
bench: add 1d 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 4f639ac commit c0c736b

File tree

2 files changed

+246
-0
lines changed

2 files changed

+246
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var add = require( '@stdlib/math/base/ops/add' );
28+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
29+
var pkg = require( './../package.json' ).name;
30+
var accumulateUnary = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var types = [ 'float64' ];
36+
var order = 'column-major';
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - ndarray length
46+
* @param {NonNegativeIntegerArray} shape - ndarray shape
47+
* @param {string} xtype - input ndarray data type
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len, shape, xtype ) {
51+
var x = uniform( len, -100.0, 100.0, {
52+
'dtype': xtype
53+
});
54+
x = {
55+
'dtype': xtype,
56+
'data': x,
57+
'shape': shape,
58+
'strides': shape2strides( shape, order ),
59+
'offset': 0,
60+
'order': order
61+
};
62+
return benchmark;
63+
64+
/**
65+
* Benchmark function.
66+
*
67+
* @private
68+
* @param {Benchmark} b - benchmark instance
69+
*/
70+
function benchmark( b ) {
71+
var out;
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
out = accumulateUnary( x, 0.0, add );
77+
if ( isnan( out ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( out ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var len;
100+
var min;
101+
var max;
102+
var sh;
103+
var t1;
104+
var f;
105+
var i;
106+
var j;
107+
108+
min = 1; // 10^min
109+
max = 6; // 10^max
110+
111+
for ( j = 0; j < types.length; j++ ) {
112+
t1 = types[ j ];
113+
for ( i = min; i <= max; i++ ) {
114+
len = pow( 10, i );
115+
116+
sh = [ len ];
117+
f = createBenchmark( len, sh, t1 );
118+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
119+
}
120+
}
121+
}
122+
123+
main();
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var add = require( '@stdlib/math/base/ops/add' );
28+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
29+
var pkg = require( './../package.json' ).name;
30+
var accumulateUnary = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var types = [ 'float64' ];
36+
var order = 'row-major';
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - ndarray length
46+
* @param {NonNegativeIntegerArray} shape - ndarray shape
47+
* @param {string} xtype - input ndarray data type
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len, shape, xtype ) {
51+
var x = uniform( len, -100.0, 100.0, {
52+
'dtype': xtype
53+
});
54+
x = {
55+
'dtype': xtype,
56+
'data': x,
57+
'shape': shape,
58+
'strides': shape2strides( shape, order ),
59+
'offset': 0,
60+
'order': order
61+
};
62+
return benchmark;
63+
64+
/**
65+
* Benchmark function.
66+
*
67+
* @private
68+
* @param {Benchmark} b - benchmark instance
69+
*/
70+
function benchmark( b ) {
71+
var out;
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
out = accumulateUnary( x, 0.0, add );
77+
if ( isnan( out ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( out ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var len;
100+
var min;
101+
var max;
102+
var sh;
103+
var t1;
104+
var f;
105+
var i;
106+
var j;
107+
108+
min = 1; // 10^min
109+
max = 6; // 10^max
110+
111+
for ( j = 0; j < types.length; j++ ) {
112+
t1 = types[ j ];
113+
for ( i = min; i <= max; i++ ) {
114+
len = pow( 10, i );
115+
116+
sh = [ len ];
117+
f = createBenchmark( len, sh, t1 );
118+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',xtype='+t1, f );
119+
}
120+
}
121+
}
122+
123+
main();

0 commit comments

Comments
 (0)