Skip to content

Commit bdfbecb

Browse files
committed
feat: add benchmarks
1 parent 7e38ef1 commit bdfbecb

19 files changed

+1528
-40
lines changed

lib/node_modules/@stdlib/ndarray/base/eye/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ The function accepts the following arguments:
8181
## Notes
8282

8383
- If the provided [data type][@stdlib/ndarray/dtypes] is complex, the function returns an ndarray whose kth diagonal is filled with complex numbers whose real component equals one and imaginary component equals zero
84-
- If `k` is greater than the number of columns or less than number of negative rows, the function returns an array filled with zeros
84+
- If `k` is greater than the number of columns or less than the number of negative rows, the function returns an array filled with zeros
8585

8686
</section>
8787

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var format = require( '@stdlib/string/format' );
26+
var pkg = require( './../package.json' ).name;
27+
var eye = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( format( '%s:dtype=float64', pkg ), function benchmark( b ) {
33+
var arr;
34+
var i;
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
arr = eye( 'float64', 0, 0, 0, 'row-major' );
38+
if ( arr.length !== 0 ) {
39+
b.fail( 'should have length 0' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isndarrayLike( arr ) ) {
44+
b.fail( 'should return an ndarray' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) {
51+
var arr;
52+
var i;
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
arr = eye( 'float32', 0, 0, 0, 'row-major' );
56+
if ( arr.length !== 0 ) {
57+
b.fail( 'should have length 0' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isndarrayLike( arr ) ) {
62+
b.fail( 'should return an ndarray' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
67+
68+
bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) {
69+
var arr;
70+
var i;
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
arr = eye( 'complex128', 0, 0, 0, 'row-major' );
74+
if ( arr.length !== 0 ) {
75+
b.fail( 'should have length 0' );
76+
}
77+
}
78+
b.toc();
79+
if ( !isndarrayLike( arr ) ) {
80+
b.fail( 'should return an ndarray' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
});
85+
86+
bench( format( '%s:dtype=complex64', pkg ), function benchmark( b ) {
87+
var arr;
88+
var i;
89+
b.tic();
90+
for ( i = 0; i < b.iterations; i++ ) {
91+
arr = eye( 'complex64', 0, 0, 0, 'row-major' );
92+
if ( arr.length !== 0 ) {
93+
b.fail( 'should have length 0' );
94+
}
95+
}
96+
b.toc();
97+
if ( !isndarrayLike( arr ) ) {
98+
b.fail( 'should return an ndarray' );
99+
}
100+
b.pass( 'benchmark finished' );
101+
b.end();
102+
});
103+
104+
bench( format( '%s:dtype=int32', pkg ), function benchmark( b ) {
105+
var arr;
106+
var i;
107+
b.tic();
108+
for ( i = 0; i < b.iterations; i++ ) {
109+
arr = eye( 'int32', 0, 0, 0, 'row-major' );
110+
if ( arr.length !== 0 ) {
111+
b.fail( 'should have length 0' );
112+
}
113+
}
114+
b.toc();
115+
if ( !isndarrayLike( arr ) ) {
116+
b.fail( 'should return an ndarray' );
117+
}
118+
b.pass( 'benchmark finished' );
119+
b.end();
120+
});
121+
122+
bench( format( '%s:dtype=uint32', pkg ), function benchmark( b ) {
123+
var arr;
124+
var i;
125+
b.tic();
126+
for ( i = 0; i < b.iterations; i++ ) {
127+
arr = eye( 'uint32', 0, 0, 0, 'row-major' );
128+
if ( arr.length !== 0 ) {
129+
b.fail( 'should have length 0' );
130+
}
131+
}
132+
b.toc();
133+
if ( !isndarrayLike( arr ) ) {
134+
b.fail( 'should return an ndarray' );
135+
}
136+
b.pass( 'benchmark finished' );
137+
b.end();
138+
});
139+
140+
bench( format( '%s:dtype=int16', pkg ), function benchmark( b ) {
141+
var arr;
142+
var i;
143+
b.tic();
144+
for ( i = 0; i < b.iterations; i++ ) {
145+
arr = eye( 'int16', 0, 0, 0, 'row-major' );
146+
if ( arr.length !== 0 ) {
147+
b.fail( 'should have length 0' );
148+
}
149+
}
150+
b.toc();
151+
if ( !isndarrayLike( arr ) ) {
152+
b.fail( 'should return an ndarray' );
153+
}
154+
b.pass( 'benchmark finished' );
155+
b.end();
156+
});
157+
158+
bench( format( '%s:dtype=uint16', pkg ), function benchmark( b ) {
159+
var arr;
160+
var i;
161+
b.tic();
162+
for ( i = 0; i < b.iterations; i++ ) {
163+
arr = eye( 'uint16', 0, 0, 0, 'row-major' );
164+
if ( arr.length !== 0 ) {
165+
b.fail( 'should have length 0' );
166+
}
167+
}
168+
b.toc();
169+
if ( !isndarrayLike( arr ) ) {
170+
b.fail( 'should return an ndarray' );
171+
}
172+
b.pass( 'benchmark finished' );
173+
b.end();
174+
});
175+
176+
bench( format( '%s:dtype=int8', pkg ), function benchmark( b ) {
177+
var arr;
178+
var i;
179+
b.tic();
180+
for ( i = 0; i < b.iterations; i++ ) {
181+
arr = eye( 'int8', 0, 0, 0, 'row-major' );
182+
if ( arr.length !== 0 ) {
183+
b.fail( 'should have length 0' );
184+
}
185+
}
186+
b.toc();
187+
if ( !isndarrayLike( arr ) ) {
188+
b.fail( 'should return an ndarray' );
189+
}
190+
b.pass( 'benchmark finished' );
191+
b.end();
192+
});
193+
194+
bench( format( '%s:dtype=uint8', pkg ), function benchmark( b ) {
195+
var arr;
196+
var i;
197+
b.tic();
198+
for ( i = 0; i < b.iterations; i++ ) {
199+
arr = eye( 'uint8', 0, 0, 0, 'row-major' );
200+
if ( arr.length !== 0 ) {
201+
b.fail( 'should have length 0' );
202+
}
203+
}
204+
b.toc();
205+
if ( !isndarrayLike( arr ) ) {
206+
b.fail( 'should return an ndarray' );
207+
}
208+
b.pass( 'benchmark finished' );
209+
b.end();
210+
});
211+
212+
bench( format( '%s:dtype=uint8c', pkg ), function benchmark( b ) {
213+
var arr;
214+
var i;
215+
b.tic();
216+
for ( i = 0; i < b.iterations; i++ ) {
217+
arr = eye( 'uint8c', 0, 0, 0, 'row-major' );
218+
if ( arr.length !== 0 ) {
219+
b.fail( 'should have length 0' );
220+
}
221+
}
222+
b.toc();
223+
if ( !isndarrayLike( arr ) ) {
224+
b.fail( 'should return an ndarray' );
225+
}
226+
b.pass( 'benchmark finished' );
227+
b.end();
228+
});
229+
230+
bench( format( '%s:dtype=bool', pkg ), function benchmark( b ) {
231+
var arr;
232+
var i;
233+
b.tic();
234+
for ( i = 0; i < b.iterations; i++ ) {
235+
arr = eye( 'bool', 0, 0, 0, 'row-major' );
236+
if ( arr.length !== 0 ) {
237+
b.fail( 'should have length 0' );
238+
}
239+
}
240+
b.toc();
241+
if ( !isndarrayLike( arr ) ) {
242+
b.fail( 'should return an ndarray' );
243+
}
244+
b.pass( 'benchmark finished' );
245+
b.end();
246+
});
247+
248+
bench( format( '%s:dtype=generic', pkg ), function benchmark( b ) {
249+
var arr;
250+
var i;
251+
b.tic();
252+
for ( i = 0; i < b.iterations; i++ ) {
253+
arr = eye( 'generic', 0, 0, 0, 'row-major' );
254+
if ( arr.length !== 0 ) {
255+
b.fail( 'should have length 0' );
256+
}
257+
}
258+
b.toc();
259+
if ( !isndarrayLike( arr ) ) {
260+
b.fail( 'should return an ndarray' );
261+
}
262+
b.pass( 'benchmark finished' );
263+
b.end();
264+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var eye = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
return benchmark;
42+
43+
/**
44+
* Benchmark function.
45+
*
46+
* @private
47+
* @param {Benchmark} b - benchmark instance
48+
*/
49+
function benchmark( b ) {
50+
var arr;
51+
var i;
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
arr = eye( 'bool', len, len, 0, 'row-major' );
56+
if ( arr.length !== pow( len, 2 ) ) {
57+
b.fail( 'unexpected length' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isndarrayLike( arr ) ) {
62+
b.fail( 'should return an ndarray' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
}
67+
}
68+
69+
70+
// MAIN //
71+
72+
/**
73+
* Main execution sequence.
74+
*
75+
* @private
76+
*/
77+
function main() {
78+
var len;
79+
var min;
80+
var max;
81+
var f;
82+
var i;
83+
84+
min = 1; // 10^min
85+
max = 3; // 10^max
86+
87+
for ( i = min; i <= max; i++ ) {
88+
len = pow( 10, i );
89+
f = createBenchmark( len );
90+
bench( format( '%s:dtype=bool,size=%d', pkg, len ), f );
91+
}
92+
}
93+
94+
main();

0 commit comments

Comments
 (0)