|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); |
| 25 | +var zeros = require( './../../zeros' ); |
| 26 | +var pkg = require( './../package.json' ).name; |
| 27 | +var ndarrayWith = require( './../lib' ); |
| 28 | + |
| 29 | + |
| 30 | +// MAIN // |
| 31 | + |
| 32 | +bench( pkg+':ndims=1', function benchmark( b ) { |
| 33 | + var v; |
| 34 | + var x; |
| 35 | + var i; |
| 36 | + |
| 37 | + x = zeros( [ 5 ], { |
| 38 | + 'dtype': 'float64' |
| 39 | + }); |
| 40 | + |
| 41 | + b.tic(); |
| 42 | + for ( i = 0; i < b.iterations; i++ ) { |
| 43 | + v = ndarrayWith( x, [ 4 ], 5 ); |
| 44 | + if ( typeof v !== 'object' ) { |
| 45 | + b.fail( 'should return an ndarray' ); |
| 46 | + } |
| 47 | + } |
| 48 | + b.toc(); |
| 49 | + if ( !isndarrayLike( v ) ) { |
| 50 | + b.fail( 'should return an ndarray' ); |
| 51 | + } |
| 52 | + b.pass( 'benchmark finished' ); |
| 53 | + b.end(); |
| 54 | +}); |
| 55 | + |
| 56 | +bench( pkg+':ndims=2', function benchmark( b ) { |
| 57 | + var v; |
| 58 | + var x; |
| 59 | + var i; |
| 60 | + |
| 61 | + x = zeros( [ 2, 2 ], { |
| 62 | + 'dtype': 'float64' |
| 63 | + }); |
| 64 | + |
| 65 | + b.tic(); |
| 66 | + for ( i = 0; i < b.iterations; i++ ) { |
| 67 | + v = ndarrayWith( x, [ 1, 1 ], 5 ); |
| 68 | + if ( typeof v !== 'object' ) { |
| 69 | + b.fail( 'should return an ndarray' ); |
| 70 | + } |
| 71 | + } |
| 72 | + b.toc(); |
| 73 | + if ( !isndarrayLike( v ) ) { |
| 74 | + b.fail( 'should return an ndarray' ); |
| 75 | + } |
| 76 | + b.pass( 'benchmark finished' ); |
| 77 | + b.end(); |
| 78 | +}); |
| 79 | + |
| 80 | +bench( pkg+':ndims=3', function benchmark( b ) { |
| 81 | + var v; |
| 82 | + var x; |
| 83 | + var i; |
| 84 | + |
| 85 | + x = zeros( [ 2, 2, 2 ], { |
| 86 | + 'dtype': 'float64' |
| 87 | + }); |
| 88 | + |
| 89 | + b.tic(); |
| 90 | + for ( i = 0; i < b.iterations; i++ ) { |
| 91 | + v = ndarrayWith( x, [ 1, 1, 1 ], 5 ); |
| 92 | + if ( typeof v !== 'object' ) { |
| 93 | + b.fail( 'should return an ndarray' ); |
| 94 | + } |
| 95 | + } |
| 96 | + b.toc(); |
| 97 | + if ( !isndarrayLike( v ) ) { |
| 98 | + b.fail( 'should return an ndarray' ); |
| 99 | + } |
| 100 | + b.pass( 'benchmark finished' ); |
| 101 | + b.end(); |
| 102 | +}); |
| 103 | + |
| 104 | +bench( pkg+':ndims=4', function benchmark( b ) { |
| 105 | + var v; |
| 106 | + var x; |
| 107 | + var i; |
| 108 | + |
| 109 | + x = zeros( [ 2, 2, 2, 2 ], { |
| 110 | + 'dtype': 'float64' |
| 111 | + }); |
| 112 | + |
| 113 | + b.tic(); |
| 114 | + for ( i = 0; i < b.iterations; i++ ) { |
| 115 | + v = ndarrayWith( x, [ 1, 1, 1, 1 ], 5 ); |
| 116 | + if ( typeof v !== 'object' ) { |
| 117 | + b.fail( 'should return an ndarray' ); |
| 118 | + } |
| 119 | + } |
| 120 | + b.toc(); |
| 121 | + if ( !isndarrayLike( v ) ) { |
| 122 | + b.fail( 'should return an ndarray' ); |
| 123 | + } |
| 124 | + b.pass( 'benchmark finished' ); |
| 125 | + b.end(); |
| 126 | +}); |
| 127 | + |
| 128 | +bench( pkg+':ndims=5', function benchmark( b ) { |
| 129 | + var v; |
| 130 | + var x; |
| 131 | + var i; |
| 132 | + |
| 133 | + x = zeros( [ 2, 2, 2, 2, 2 ], { |
| 134 | + 'dtype': 'float64' |
| 135 | + }); |
| 136 | + |
| 137 | + b.tic(); |
| 138 | + for ( i = 0; i < b.iterations; i++ ) { |
| 139 | + v = ndarrayWith( x, [ 1, 1, 1, 1, 1 ], 5 ); |
| 140 | + if ( typeof v !== 'object' ) { |
| 141 | + b.fail( 'should return an ndarray' ); |
| 142 | + } |
| 143 | + } |
| 144 | + b.toc(); |
| 145 | + if ( !isndarrayLike( v ) ) { |
| 146 | + b.fail( 'should return an ndarray' ); |
| 147 | + } |
| 148 | + b.pass( 'benchmark finished' ); |
| 149 | + b.end(); |
| 150 | +}); |
0 commit comments