|
| 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