|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2023 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 resolve = require( 'path' ).resolve; |
| 24 | +var bench = require( '@stdlib/bench' ); |
| 25 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 26 | +var filled2dBy = require( '@stdlib/array/base/filled2d-by' ); |
| 27 | +var filledndBy = require( '@stdlib/array/base/fillednd-by' ); |
| 28 | +var unary2d = require( '@stdlib/array/base/unary2d' ); |
| 29 | +var unarynd = require( '@stdlib/array/base/unarynd' ); |
| 30 | +var zeros2d = require( '@stdlib/array/base/zeros2d' ); |
| 31 | +var zerosnd = require( '@stdlib/array/base/zerosnd' ); |
| 32 | +var ndmap = require( '@stdlib/ndarray/map' ); |
| 33 | +var ndzeros = require( '@stdlib/ndarray/zeros' ); |
| 34 | +var ndunary = require( '@stdlib/ndarray/base/unary' ); |
| 35 | +var uniform = require( '@stdlib/random/base/uniform' ).factory; |
| 36 | +var randu = require( '@stdlib/random/uniform' ); |
| 37 | +var base = require( '@stdlib/math/base/special/sqrt' ); |
| 38 | +var tryRequire = require( '@stdlib/utils/try-require' ); |
| 39 | +var pkg = require( './../package.json' ).name; |
| 40 | + |
| 41 | + |
| 42 | +// VARIABLES // |
| 43 | + |
| 44 | +var mathjs = tryRequire( resolve( __dirname, '..', 'node_modules', 'mathjs' ) ); |
| 45 | +var opts = { |
| 46 | + 'skip': ( mathjs instanceof Error ) |
| 47 | +}; |
| 48 | + |
| 49 | + |
| 50 | +// MAIN // |
| 51 | + |
| 52 | +bench( pkg+'::stdlib:array/base/unary2d:value=nested_array,dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) { |
| 53 | + var sh; |
| 54 | + var x; |
| 55 | + var y; |
| 56 | + var i; |
| 57 | + |
| 58 | + sh = [ 100, 100 ]; |
| 59 | + x = filled2dBy( sh, uniform( 0.0, 100.0 ) ); |
| 60 | + |
| 61 | + b.tic(); |
| 62 | + for ( i = 0; i < b.iterations; i++ ) { |
| 63 | + y = zeros2d( sh ); |
| 64 | + unary2d( [ x, y ], sh, base ); |
| 65 | + if ( isnan( y[ 0 ][ 0 ] ) || isnan( y[ 99 ][ 99 ] ) ) { |
| 66 | + b.fail( 'should not return NaN' ); |
| 67 | + } |
| 68 | + } |
| 69 | + b.toc(); |
| 70 | + if ( isnan( y[ 1 ][ 1 ] ) || isnan( y[ 98 ][ 98 ] ) ) { |
| 71 | + b.fail( 'should not return NaN' ); |
| 72 | + } |
| 73 | + b.pass( 'benchmark finished' ); |
| 74 | + b.end(); |
| 75 | +}); |
| 76 | + |
| 77 | +bench( pkg+'::stdlib:array/base/unarynd:value=nested_array,dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) { |
| 78 | + var sh; |
| 79 | + var x; |
| 80 | + var y; |
| 81 | + var i; |
| 82 | + |
| 83 | + sh = [ 100, 100 ]; |
| 84 | + x = filledndBy( sh, uniform( 0.0, 100.0 ) ); |
| 85 | + |
| 86 | + b.tic(); |
| 87 | + for ( i = 0; i < b.iterations; i++ ) { |
| 88 | + y = zerosnd( sh ); |
| 89 | + unarynd( [ x, y ], sh, base ); |
| 90 | + if ( isnan( y[ 0 ][ 0 ] ) || isnan( y[ 99 ][ 99 ] ) ) { |
| 91 | + b.fail( 'should not return NaN' ); |
| 92 | + } |
| 93 | + } |
| 94 | + b.toc(); |
| 95 | + if ( isnan( y[ 1 ][ 1 ] ) || isnan( y[ 98 ][ 98 ] ) ) { |
| 96 | + b.fail( 'should not return NaN' ); |
| 97 | + } |
| 98 | + b.pass( 'benchmark finished' ); |
| 99 | + b.end(); |
| 100 | +}); |
| 101 | + |
| 102 | +bench( pkg+'::stdlib:ndarray/base/unary:value=ndarray,dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) { |
| 103 | + var opts; |
| 104 | + var sh; |
| 105 | + var x; |
| 106 | + var y; |
| 107 | + var i; |
| 108 | + |
| 109 | + sh = [ 100, 100 ]; |
| 110 | + opts = { |
| 111 | + 'dtype': 'generic' |
| 112 | + }; |
| 113 | + x = randu( sh, 0.0, 100.0, opts ); |
| 114 | + |
| 115 | + b.tic(); |
| 116 | + for ( i = 0; i < b.iterations; i++ ) { |
| 117 | + y = ndzeros( sh, opts ); |
| 118 | + ndunary( [ x, y ], base ); |
| 119 | + if ( isnan( y.get( 0, 0 ) ) || isnan( y.get( 99, 99 ) ) ) { |
| 120 | + b.fail( 'should not return NaN' ); |
| 121 | + } |
| 122 | + } |
| 123 | + b.toc(); |
| 124 | + if ( isnan( y.get( 1, 1 ) ) || isnan( y.get( 98, 98) ) ) { |
| 125 | + b.fail( 'should not return NaN' ); |
| 126 | + } |
| 127 | + b.pass( 'benchmark finished' ); |
| 128 | + b.end(); |
| 129 | +}); |
| 130 | + |
| 131 | +bench( pkg+'::stdlib:ndarray/map:value=ndarray,dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) { |
| 132 | + var sh; |
| 133 | + var x; |
| 134 | + var y; |
| 135 | + var i; |
| 136 | + |
| 137 | + sh = [ 100, 100 ]; |
| 138 | + x = randu( sh, 0.0, 100.0, { |
| 139 | + 'dtype': 'generic' |
| 140 | + }); |
| 141 | + |
| 142 | + b.tic(); |
| 143 | + for ( i = 0; i < b.iterations; i++ ) { |
| 144 | + y = ndmap( x, base ); |
| 145 | + if ( isnan( y.get( 0, 0 ) ) || isnan( y.get( 99, 99 ) ) ) { |
| 146 | + b.fail( 'should not return NaN' ); |
| 147 | + } |
| 148 | + } |
| 149 | + b.toc(); |
| 150 | + if ( isnan( y.get( 1, 1 ) ) || isnan( y.get( 98, 98) ) ) { |
| 151 | + b.fail( 'should not return NaN' ); |
| 152 | + } |
| 153 | + b.pass( 'benchmark finished' ); |
| 154 | + b.end(); |
| 155 | +}); |
| 156 | + |
| 157 | +bench( pkg+'::mathjs:sqrt:value=matrix,dtype=generic,size=10000,shape=(100,100)', opts, function benchmark( b ) { |
| 158 | + var buf; |
| 159 | + var x; |
| 160 | + var y; |
| 161 | + var i; |
| 162 | + |
| 163 | + buf = mathjs.random( [ 100, 100 ], 0, 100 ); |
| 164 | + x = mathjs.matrix( buf, 'dense', 'number' ); |
| 165 | + |
| 166 | + b.tic(); |
| 167 | + for ( i = 0; i < b.iterations; i++ ) { |
| 168 | + y = x.map( base ); |
| 169 | + if ( typeof y !== 'object' ) { |
| 170 | + b.fail( 'should return an object' ); |
| 171 | + } |
| 172 | + } |
| 173 | + b.toc(); |
| 174 | + if ( isnan( y.get( [ 0, 0 ] ) ) || isnan( y.get( [ 99, 99 ] ) ) ) { |
| 175 | + b.fail( 'should not return NaN' ); |
| 176 | + } |
| 177 | + b.pass( 'benchmark finished' ); |
| 178 | + b.end(); |
| 179 | +}); |
0 commit comments