|
| 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 bench = require( '@stdlib/bench' ); |
| 24 | +var isComplex64Array = require( '@stdlib/assert/is-complex64array' ); |
| 25 | +var pow = require( '@stdlib/math/base/special/pow' ); |
| 26 | +var Complex64 = require( '@stdlib/complex/float32' ); |
| 27 | +var realf = require( '@stdlib/complex/realf' ); |
| 28 | +var imagf = require( '@stdlib/complex/imagf' ); |
| 29 | +var pkg = require( './../package.json' ).name; |
| 30 | +var Complex64Array = require( './../lib' ); |
| 31 | + |
| 32 | + |
| 33 | +// FUNCTIONS // |
| 34 | + |
| 35 | +/** |
| 36 | +* Predicate function. |
| 37 | +* |
| 38 | +* @private |
| 39 | +* @param {Complex64} value - array element |
| 40 | +* @param {NonNegativeInteger} idx - array element index |
| 41 | +* @param {Complex64Array} arr - array instance |
| 42 | +* @returns {boolean} boolean indicating whether a value passes a test |
| 43 | +*/ |
| 44 | +function predicate( value ) { |
| 45 | + return ( realf( value ) === imagf( value ) ); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | +* Creates a benchmark function. |
| 50 | +* |
| 51 | +* @private |
| 52 | +* @param {PositiveInteger} len - array length |
| 53 | +* @returns {Function} benchmark function |
| 54 | +*/ |
| 55 | +function createBenchmark( len ) { |
| 56 | + var arr; |
| 57 | + var i; |
| 58 | + |
| 59 | + arr = []; |
| 60 | + for ( i = 0; i < len; i++ ) { |
| 61 | + arr.push( new Complex64( i, i ) ); |
| 62 | + } |
| 63 | + arr = new Complex64Array( arr ); |
| 64 | + |
| 65 | + return benchmark; |
| 66 | + |
| 67 | + /** |
| 68 | + * Benchmark function. |
| 69 | + * |
| 70 | + * @private |
| 71 | + * @param {Benchmark} b - benchmark instance |
| 72 | + */ |
| 73 | + function benchmark( b ) { |
| 74 | + var out; |
| 75 | + var i; |
| 76 | + |
| 77 | + b.tic(); |
| 78 | + for ( i = 0; i < b.iterations; i++ ) { |
| 79 | + out = arr.filter( predicate ); |
| 80 | + if ( typeof out !== 'object' ) { |
| 81 | + b.fail( 'should return an object' ); |
| 82 | + } |
| 83 | + } |
| 84 | + b.toc(); |
| 85 | + if ( !isComplex64Array( out ) ) { |
| 86 | + b.fail( 'should return a Complex64Array' ); |
| 87 | + } |
| 88 | + b.pass( 'benchmark finished' ); |
| 89 | + b.end(); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +// MAIN // |
| 95 | + |
| 96 | +/** |
| 97 | +* Main execution sequence. |
| 98 | +* |
| 99 | +* @private |
| 100 | +*/ |
| 101 | +function main() { |
| 102 | + var len; |
| 103 | + var min; |
| 104 | + var max; |
| 105 | + var f; |
| 106 | + var i; |
| 107 | + |
| 108 | + min = 1; // 10^min |
| 109 | + max = 6; // 10^max |
| 110 | + |
| 111 | + for ( i = min; i <= max; i++ ) { |
| 112 | + len = pow( 10, i ); |
| 113 | + f = createBenchmark( len ); |
| 114 | + bench( pkg+':filter:len='+len, f ); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +main(); |
0 commit comments