|
| 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 | +/* eslint-disable max-len */ |
| 20 | + |
| 21 | +'use strict'; |
| 22 | + |
| 23 | +// MODULES // |
| 24 | + |
| 25 | +var resolve = require( 'path' ).resolve; |
| 26 | +var bench = require( '@stdlib/bench' ); |
| 27 | +var pow = require( '@stdlib/math/base/special/pow' ); |
| 28 | +var floor = require( '@stdlib/math/base/special/floor' ); |
| 29 | +var numel = require( '@stdlib/ndarray/base/numel' ); |
| 30 | +var filled2dBy = require( '@stdlib/array/base/filled2d-by' ); |
| 31 | +var filledndBy = require( '@stdlib/array/base/fillednd-by' ); |
| 32 | +var unary2d = require( '@stdlib/array/base/unary2d' ); |
| 33 | +var unarynd = require( '@stdlib/array/base/unarynd' ); |
| 34 | +var zeros2d = require( '@stdlib/array/base/zeros2d' ); |
| 35 | +var zerosnd = require( '@stdlib/array/base/zerosnd' ); |
| 36 | +var array = require( '@stdlib/ndarray/array' ); |
| 37 | +var uniform = require( '@stdlib/random/base/uniform' ).factory; |
| 38 | +var base = require( '@stdlib/math/base/special/abs' ); |
| 39 | +var abs = require( '@stdlib/math/special/abs' ); |
| 40 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 41 | +var format = require( '@stdlib/string/format' ); |
| 42 | +var tryRequire = require( '@stdlib/utils/try-require' ); |
| 43 | +var pkg = require( './../package.json' ).name; |
| 44 | + |
| 45 | + |
| 46 | +// VARIABLES // |
| 47 | + |
| 48 | +var mathjs = tryRequire( resolve( __dirname, '..', 'node_modules', 'mathjs' ) ); |
| 49 | +var opts = { |
| 50 | + 'skip': ( mathjs instanceof Error ) |
| 51 | +}; |
| 52 | +var OPTS = { |
| 53 | + 'dtype': 'generic' |
| 54 | +}; |
| 55 | +var rand = uniform( -100.0, 100.0 ); |
| 56 | + |
| 57 | + |
| 58 | +// FUNCTIONS // |
| 59 | + |
| 60 | +/** |
| 61 | +* Creates a benchmark function. |
| 62 | +* |
| 63 | +* @private |
| 64 | +* @param {PositiveIntegerArray} shape - array shape |
| 65 | +* @returns {Function} benchmark function |
| 66 | +*/ |
| 67 | +function createBenchmark1( shape ) { |
| 68 | + var x = array( filled2dBy( shape, rand ), OPTS ); |
| 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 = abs( x ); |
| 84 | + if ( typeof y !== 'object' ) { |
| 85 | + b.fail( 'should return an object' ); |
| 86 | + } |
| 87 | + } |
| 88 | + b.toc(); |
| 89 | + if ( isnan( y.get( 0, 0 ) ) || isnan( y.get( shape[0]-1, shape[1]-1 ) ) ) { |
| 90 | + b.fail( 'should not return NaN' ); |
| 91 | + } |
| 92 | + b.pass( 'benchmark finished' ); |
| 93 | + b.end(); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | +* Creates a benchmark function. |
| 99 | +* |
| 100 | +* @private |
| 101 | +* @param {PositiveIntegerArray} shape - array shape |
| 102 | +* @returns {Function} benchmark function |
| 103 | +*/ |
| 104 | +function createBenchmark2( shape ) { |
| 105 | + var x = filled2dBy( shape, rand ); |
| 106 | + return benchmark; |
| 107 | + |
| 108 | + /** |
| 109 | + * Benchmark function. |
| 110 | + * |
| 111 | + * @private |
| 112 | + * @param {Benchmark} b - benchmark instance |
| 113 | + */ |
| 114 | + function benchmark( b ) { |
| 115 | + var y; |
| 116 | + var i; |
| 117 | + |
| 118 | + b.tic(); |
| 119 | + for ( i = 0; i < b.iterations; i++ ) { |
| 120 | + y = zeros2d( shape ); |
| 121 | + unary2d( [ x, y ], shape, base ); |
| 122 | + if ( typeof y !== 'object' ) { |
| 123 | + b.fail( 'should return an object' ); |
| 124 | + } |
| 125 | + } |
| 126 | + b.toc(); |
| 127 | + if ( isnan( y[ 0 ][ 0 ] ) || isnan( y[ shape[0]-1 ][ shape[1]-1 ] ) ) { |
| 128 | + b.fail( 'should not return NaN' ); |
| 129 | + } |
| 130 | + b.pass( 'benchmark finished' ); |
| 131 | + b.end(); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | +* Creates a benchmark function. |
| 137 | +* |
| 138 | +* @private |
| 139 | +* @param {PositiveIntegerArray} shape - array shape |
| 140 | +* @returns {Function} benchmark function |
| 141 | +*/ |
| 142 | +function createBenchmark3( shape ) { |
| 143 | + var x = filledndBy( shape, rand ); |
| 144 | + return benchmark; |
| 145 | + |
| 146 | + /** |
| 147 | + * Benchmark function. |
| 148 | + * |
| 149 | + * @private |
| 150 | + * @param {Benchmark} b - benchmark instance |
| 151 | + */ |
| 152 | + function benchmark( b ) { |
| 153 | + var y; |
| 154 | + var i; |
| 155 | + |
| 156 | + b.tic(); |
| 157 | + for ( i = 0; i < b.iterations; i++ ) { |
| 158 | + y = zerosnd( shape ); |
| 159 | + unarynd( [ x, y ], shape, base ); |
| 160 | + if ( typeof y !== 'object' ) { |
| 161 | + b.fail( 'should return an object' ); |
| 162 | + } |
| 163 | + } |
| 164 | + b.toc(); |
| 165 | + if ( isnan( y[ 0 ][ 0 ] ) || isnan( y[ shape[0]-1 ][ shape[1]-1 ] ) ) { |
| 166 | + b.fail( 'should not return NaN' ); |
| 167 | + } |
| 168 | + b.pass( 'benchmark finished' ); |
| 169 | + b.end(); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | +* Creates a benchmark function. |
| 175 | +* |
| 176 | +* @private |
| 177 | +* @param {PositiveIntegerArray} shape - input array shape |
| 178 | +* @returns {Function} benchmark function |
| 179 | +*/ |
| 180 | +function createBenchmark4( shape ) { |
| 181 | + var x = mathjs.matrix( filled2dBy( shape, rand ) ); |
| 182 | + return benchmark; |
| 183 | + |
| 184 | + /** |
| 185 | + * Benchmark function. |
| 186 | + * |
| 187 | + * @private |
| 188 | + * @param {Benchmark} b - benchmark instance |
| 189 | + */ |
| 190 | + function benchmark( b ) { |
| 191 | + var y; |
| 192 | + var i; |
| 193 | + |
| 194 | + b.tic(); |
| 195 | + for ( i = 0; i < b.iterations; i++ ) { |
| 196 | + y = mathjs.abs( x ); |
| 197 | + if ( typeof y !== 'object' ) { |
| 198 | + b.fail( 'should return an object' ); |
| 199 | + } |
| 200 | + } |
| 201 | + b.toc(); |
| 202 | + if ( isnan( y.get( [ 0, 0 ] ) ) || isnan( y.get( [ shape[0]-1, shape[1]-1 ] ) ) ) { |
| 203 | + b.fail( 'should not return NaN' ); |
| 204 | + } |
| 205 | + b.pass( 'benchmark finished' ); |
| 206 | + b.end(); |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | + |
| 211 | +// MAIN // |
| 212 | + |
| 213 | +/** |
| 214 | +* Main execution sequence. |
| 215 | +* |
| 216 | +* @private |
| 217 | +*/ |
| 218 | +function main() { |
| 219 | + var shape; |
| 220 | + var min; |
| 221 | + var max; |
| 222 | + var N; |
| 223 | + var f; |
| 224 | + var i; |
| 225 | + |
| 226 | + min = 1; // 10^min |
| 227 | + max = 6; // 10^max |
| 228 | + |
| 229 | + for ( i = min; i <= max; i++ ) { |
| 230 | + N = floor( pow( pow( 10, i ), 1.0/2.0 ) ); |
| 231 | + shape = [ N, N ]; |
| 232 | + |
| 233 | + f = createBenchmark1( shape ); |
| 234 | + bench( format( '%s::stdlib:math/special/abs:dtype=%s,size=%d,shape=(%s)', pkg, OPTS.dtype, numel( shape ), shape.join( ',' ) ), f ); |
| 235 | + |
| 236 | + f = createBenchmark2( shape ); |
| 237 | + bench( format( '%s::stdlib:array/base/unary2d:dtype=%s,size=%d,shape=(%s)', pkg, OPTS.dtype, numel( shape ), shape.join( ',' ) ), f ); |
| 238 | + |
| 239 | + f = createBenchmark3( shape ); |
| 240 | + bench( format( '%s::stdlib:array/base/unarynd:dtype=%s,size=%d,shape=(%s)', pkg, OPTS.dtype, numel( shape ), shape.join( ',' ) ), f ); |
| 241 | + |
| 242 | + f = createBenchmark4( shape ); |
| 243 | + bench( format( '%s::mathjs:abs:dtype=%s,size=%d,shape=(%s)', pkg, OPTS.dtype, numel( shape ), shape.join( ',' ) ), opts, f ); |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +main(); |
0 commit comments