|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' ); |
| 26 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 27 | +var pow = require( '@stdlib/math/base/special/pow' ); |
| 28 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 29 | +var tryRequire = require( '@stdlib/utils/try-require' ); |
| 30 | +var pkg = require( './../package.json' ).name; |
| 31 | + |
| 32 | + |
| 33 | +// VARIABLES // |
| 34 | + |
| 35 | +var dcusum = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ); |
| 36 | +var opts = { |
| 37 | + 'skip': ( dcusum instanceof Error ) |
| 38 | +}; |
| 39 | + |
| 40 | + |
| 41 | +// FUNCTIONS // |
| 42 | + |
| 43 | +/** |
| 44 | +* Creates a benchmark function. |
| 45 | +* |
| 46 | +* @private |
| 47 | +* @param {PositiveInteger} len - array length |
| 48 | +* @returns {Function} benchmark function |
| 49 | +*/ |
| 50 | +function createBenchmark( len ) { |
| 51 | + var x; |
| 52 | + var y; |
| 53 | + var i; |
| 54 | + |
| 55 | + x = new Float64Array( len ); |
| 56 | + y = new Float64Array( len ); |
| 57 | + for ( i = 0; i < x.length; i++ ) { |
| 58 | + x[ i ] = ( randu()*20.0 ) - 10.0; |
| 59 | + } |
| 60 | + return benchmark; |
| 61 | + |
| 62 | + function benchmark( b ) { |
| 63 | + var v; |
| 64 | + var i; |
| 65 | + |
| 66 | + for ( i = 0; i < len; i++ ) { |
| 67 | + y[ i ] = 0.0; |
| 68 | + } |
| 69 | + b.tic(); |
| 70 | + for ( i = 0; i < b.iterations; i++ ) { |
| 71 | + x[ 0 ] += 1.0; |
| 72 | + v = dcusum( x.length, 0.0, x, 1, 0, y, 1, 0 ); |
| 73 | + if ( isnan( v[ i%len ] ) ) { |
| 74 | + b.fail( 'should not return NaN' ); |
| 75 | + } |
| 76 | + } |
| 77 | + b.toc(); |
| 78 | + if ( isnan( v[ i%len ] ) ) { |
| 79 | + b.fail( 'should not return NaN' ); |
| 80 | + } |
| 81 | + b.pass( 'benchmark finished' ); |
| 82 | + b.end(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +// MAIN // |
| 88 | + |
| 89 | +/** |
| 90 | +* Main execution sequence. |
| 91 | +* |
| 92 | +* @private |
| 93 | +*/ |
| 94 | +function main() { |
| 95 | + var len; |
| 96 | + var min; |
| 97 | + var max; |
| 98 | + var f; |
| 99 | + var i; |
| 100 | + |
| 101 | + min = 1; // 10^min |
| 102 | + max = 6; // 10^max |
| 103 | + |
| 104 | + for ( i = min; i <= max; i++ ) { |
| 105 | + len = pow( 10, i ); |
| 106 | + f = createBenchmark( len ); |
| 107 | + bench( pkg+'::native:ndarray:len='+len, opts, f ); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +main(); |
0 commit comments