|
| 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 Complex128 = require( '@stdlib/complex/float64' ); |
| 27 | +var base = require( '@stdlib/math/base/special/cabs2' ); |
| 28 | +var tryRequire = require( '@stdlib/utils/try-require' ); |
| 29 | +var pkg = require( './../package.json' ).name; |
| 30 | + |
| 31 | + |
| 32 | +// VARIABLES // |
| 33 | + |
| 34 | +var mathjs = tryRequire( resolve( __dirname, '..', 'node_modules', 'mathjs' ) ); |
| 35 | +var opts = { |
| 36 | + 'skip': ( mathjs instanceof Error ) |
| 37 | +}; |
| 38 | + |
| 39 | + |
| 40 | +// MAIN // |
| 41 | + |
| 42 | +bench( pkg+'::stdlib:math/base/special/cabs2:value=complex_number', opts, function benchmark( b ) { |
| 43 | + var x; |
| 44 | + var y; |
| 45 | + var i; |
| 46 | + |
| 47 | + x = [ |
| 48 | + new Complex128( 5.0, 3.0 ), |
| 49 | + new Complex128( -5.0, 3.0 ), |
| 50 | + new Complex128( 5.0, -3.0 ), |
| 51 | + new Complex128( -5.0, -3.0 ), |
| 52 | + new Complex128( 3.0, -4.0 ) |
| 53 | + ]; |
| 54 | + |
| 55 | + b.tic(); |
| 56 | + for ( i = 0; i < b.iterations; i++ ) { |
| 57 | + y = base( x[ i%x.length ] ); |
| 58 | + if ( y !== y ) { |
| 59 | + b.fail( 'should not return NaN' ); |
| 60 | + } |
| 61 | + } |
| 62 | + b.toc(); |
| 63 | + if ( isnan( y ) ) { |
| 64 | + b.fail( 'should not return NaN' ); |
| 65 | + } |
| 66 | + b.pass( 'benchmark finished' ); |
| 67 | + b.end(); |
| 68 | +}); |
| 69 | + |
| 70 | +// TODO: add math/special/abs2 benchmarks |
| 71 | + |
| 72 | +bench( pkg+'::mathjs:square:value=complex_number', opts, function benchmark( b ) { |
| 73 | + var x; |
| 74 | + var y; |
| 75 | + var i; |
| 76 | + |
| 77 | + x = [ |
| 78 | + mathjs.complex( 5.0, 3.0 ), |
| 79 | + mathjs.complex( -5.0, 3.0 ), |
| 80 | + mathjs.complex( 5.0, -3.0 ), |
| 81 | + mathjs.complex( -5.0, -3.0 ), |
| 82 | + mathjs.complex( 3.0, -4.0 ) |
| 83 | + ]; |
| 84 | + |
| 85 | + b.tic(); |
| 86 | + for ( i = 0; i < b.iterations; i++ ) { |
| 87 | + y = mathjs.square( x[ i%x.length ] ); |
| 88 | + if ( y !== y ) { |
| 89 | + b.fail( 'should not return NaN' ); |
| 90 | + } |
| 91 | + } |
| 92 | + b.toc(); |
| 93 | + if ( isnan( y ) ) { |
| 94 | + b.fail( 'should not return NaN' ); |
| 95 | + } |
| 96 | + b.pass( 'benchmark finished' ); |
| 97 | + b.end(); |
| 98 | +}); |
0 commit comments