Skip to content

Commit 5ae1975

Browse files
committed
bench: update benchmarks
1 parent db3b429 commit 5ae1975

File tree

4 files changed

+418
-4
lines changed

4 files changed

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

lib/node_modules/@stdlib/ndarray/map/benchmark/benchmark.js renamed to lib/node_modules/@stdlib/ndarray/map/benchmark/benchmark.1d_rowmajor.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ function createBenchmark( len, shape, xtype, ytype ) {
6262
});
6363
strides = shape2strides( shape, order );
6464
x = ndarray( xtype, xbuf, shape, strides, 0, order );
65-
6665
opts = {
6766
'dtype': ytype
6867
};
69-
7068
return benchmark;
7169

7270
/**
@@ -117,8 +115,8 @@ function main() {
117115
min = 1; // 10^min
118116
max = 6; // 10^max
119117

120-
for ( j = 0; j < ytypes.length; j++ ) {
121-
t1 = xtypes[ 0 ];
118+
for ( j = 0; j < xtypes.length; j++ ) {
119+
t1 = xtypes[ j ];
122120
t2 = ytypes[ j ];
123121
for ( i = min; i <= max; i++ ) {
124122
len = pow( 10, i );
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 sqrt = require( '@stdlib/math/base/special/sqrt' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var identity = require( '@stdlib/math/base/special/identity' );
29+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
30+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
31+
var ndarray = require( '@stdlib/ndarray/ctor' );
32+
var pkg = require( './../package.json' ).name;
33+
var map = require( './../lib' );
34+
35+
36+
// VARIABLES //
37+
38+
var xtypes = [ 'generic' ];
39+
var ytypes = [ 'float64' ];
40+
var order = 'column-major';
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+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len, shape, xtype, ytype ) {
56+
var strides;
57+
var opts;
58+
var xbuf;
59+
var x;
60+
var y;
61+
62+
xbuf = discreteUniform( len, -100, 100, {
63+
'dtype': xtype
64+
});
65+
strides = shape2strides( shape, order );
66+
x = ndarray( xtype, xbuf, shape, strides, 0, order );
67+
opts = {
68+
'dtype': ytype
69+
};
70+
return benchmark;
71+
72+
/**
73+
* Benchmark function.
74+
*
75+
* @private
76+
* @param {Benchmark} b - benchmark instance
77+
*/
78+
function benchmark( b ) {
79+
var i;
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
y = map( x, opts, identity );
84+
if ( isnan( y.data[ i%len ] ) ) {
85+
b.fail( 'should not return NaN' );
86+
}
87+
}
88+
b.toc();
89+
if ( isnan( y.data[ i%len ] ) ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
}
95+
}
96+
97+
98+
// MAIN //
99+
100+
/**
101+
* Main execution sequence.
102+
*
103+
* @private
104+
*/
105+
function main() {
106+
var len;
107+
var min;
108+
var max;
109+
var sh;
110+
var t1;
111+
var t2;
112+
var f;
113+
var i;
114+
var j;
115+
116+
min = 1; // 10^min
117+
max = 6; // 10^max
118+
119+
for ( j = 0; j < xtypes.length; j++ ) {
120+
t1 = xtypes[ j ];
121+
t2 = ytypes[ j ];
122+
for ( i = min; i <= max; i++ ) {
123+
len = pow( 10, i );
124+
125+
sh = [ len/2, 2 ];
126+
f = createBenchmark( len, sh, t1, t2 );
127+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',yorder='+order+',xtype='+t1+',ytype='+t2, f );
128+
129+
sh = [ 2, len/2 ];
130+
f = createBenchmark( len, sh, t1, t2 );
131+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',yorder='+order+',xtype='+t1+',ytype='+t2, f );
132+
133+
len = floor( sqrt( len ) );
134+
sh = [ len, len ];
135+
len *= len;
136+
f = createBenchmark( len, sh, t1, t2 );
137+
bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+order+',yorder='+order+',xtype='+t1+',ytype='+t2, f );
138+
}
139+
}
140+
}
141+
142+
main();

0 commit comments

Comments
 (0)