|
| 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 filledBy = require( '@stdlib/array/filled-by' ); |
| 27 | +var zeros = require( '@stdlib/array/zeros' ); |
| 28 | +var uniform = require( '@stdlib/random/base/uniform' ).factory; |
| 29 | +var strided = require( '@stdlib/math/strided/ops/add' ); |
| 30 | +var tryRequire = require( '@stdlib/utils/try-require' ); |
| 31 | +var pkg = require( './../package.json' ).name; |
| 32 | + |
| 33 | + |
| 34 | +// VARIABLES // |
| 35 | + |
| 36 | +var mathjs = tryRequire( resolve( __dirname, '..', 'node_modules', 'mathjs' ) ); |
| 37 | +var opts = { |
| 38 | + 'skip': ( mathjs instanceof Error ) |
| 39 | +}; |
| 40 | + |
| 41 | + |
| 42 | +// MAIN // |
| 43 | + |
| 44 | +bench( pkg+'::stdlib:math/strided/ops/add:value=array,dtype=generic,len=100', opts, function benchmark( b ) { |
| 45 | + var x; |
| 46 | + var y; |
| 47 | + var z; |
| 48 | + var i; |
| 49 | + |
| 50 | + x = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) ); |
| 51 | + y = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) ); |
| 52 | + z = zeros( x.length, 'generic' ); |
| 53 | + |
| 54 | + b.tic(); |
| 55 | + for ( i = 0; i < b.iterations; i++ ) { |
| 56 | + strided( x.length, 'generic', x, 1, 'generic', y, 1, 'generic', z, 1 ); |
| 57 | + if ( isnan( z[ 0 ] ) || isnan( z[ z.length-1 ] ) ) { |
| 58 | + b.fail( 'should not return NaN' ); |
| 59 | + } |
| 60 | + } |
| 61 | + b.toc(); |
| 62 | + if ( isnan( z[ 0 ] ) || isnan( z[ z.length-1 ] ) ) { |
| 63 | + b.fail( 'should not return NaN' ); |
| 64 | + } |
| 65 | + b.pass( 'benchmark finished' ); |
| 66 | + b.end(); |
| 67 | +}); |
| 68 | + |
| 69 | +// TODO: add math/ops/add benchmarks |
| 70 | + |
| 71 | +bench( pkg+'::mathjs:add:value=array,dtype=generic,len=100', opts, function benchmark( b ) { |
| 72 | + var x; |
| 73 | + var y; |
| 74 | + var z; |
| 75 | + var i; |
| 76 | + |
| 77 | + x = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) ); |
| 78 | + y = filledBy( 100, 'generic', uniform( -100.0, 100.0 ) ); |
| 79 | + |
| 80 | + b.tic(); |
| 81 | + for ( i = 0; i < b.iterations; i++ ) { |
| 82 | + z = mathjs.add( x, y ); |
| 83 | + if ( isnan( z[ 0 ] ) || isnan( z[ z.length-1 ] ) ) { |
| 84 | + b.fail( 'should not return NaN' ); |
| 85 | + } |
| 86 | + } |
| 87 | + b.toc(); |
| 88 | + if ( isnan( z[ 0 ] ) || isnan( z[ z.length-1 ] ) ) { |
| 89 | + b.fail( 'should not return NaN' ); |
| 90 | + } |
| 91 | + b.pass( 'benchmark finished' ); |
| 92 | + b.end(); |
| 93 | +}); |
0 commit comments