|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2022 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 Complex128 = require( '@stdlib/complex/float64' ); |
| 26 | +var Complex64 = require( '@stdlib/complex/float32' ); |
| 27 | +var pkg = require( './../package.json' ).name; |
| 28 | +var scalar2ndarray = require( './../lib' ); |
| 29 | + |
| 30 | + |
| 31 | +// MAIN // |
| 32 | + |
| 33 | +bench( pkg+':dtype=float64', function benchmark( b ) { |
| 34 | + var x; |
| 35 | + var i; |
| 36 | + |
| 37 | + b.tic(); |
| 38 | + for ( i = 0; i < b.iterations; i++ ) { |
| 39 | + x = scalar2ndarray( i, 'float64' ); |
| 40 | + if ( x.length !== 1 ) { |
| 41 | + b.fail( 'should have length 1' ); |
| 42 | + } |
| 43 | + } |
| 44 | + b.toc(); |
| 45 | + if ( !isndarrayLike( x ) ) { |
| 46 | + b.fail( 'should return an ndarray' ); |
| 47 | + } |
| 48 | + b.pass( 'benchmark finished' ); |
| 49 | + b.end(); |
| 50 | +}); |
| 51 | + |
| 52 | +bench( pkg+':dtype=float32', function benchmark( b ) { |
| 53 | + var x; |
| 54 | + var i; |
| 55 | + |
| 56 | + b.tic(); |
| 57 | + for ( i = 0; i < b.iterations; i++ ) { |
| 58 | + x = scalar2ndarray( i, 'float32' ); |
| 59 | + if ( x.length !== 1 ) { |
| 60 | + b.fail( 'should have length 1' ); |
| 61 | + } |
| 62 | + } |
| 63 | + b.toc(); |
| 64 | + if ( !isndarrayLike( x ) ) { |
| 65 | + b.fail( 'should return an ndarray' ); |
| 66 | + } |
| 67 | + b.pass( 'benchmark finished' ); |
| 68 | + b.end(); |
| 69 | +}); |
| 70 | + |
| 71 | +bench( pkg+':dtype=complex128', function benchmark( b ) { |
| 72 | + var x; |
| 73 | + var v; |
| 74 | + var i; |
| 75 | + |
| 76 | + v = new Complex128( 1.0, 2.0 ); |
| 77 | + |
| 78 | + b.tic(); |
| 79 | + for ( i = 0; i < b.iterations; i++ ) { |
| 80 | + x = scalar2ndarray( v, 'complex128' ); |
| 81 | + if ( x.length !== 1 ) { |
| 82 | + b.fail( 'should have length 1' ); |
| 83 | + } |
| 84 | + } |
| 85 | + b.toc(); |
| 86 | + if ( !isndarrayLike( x ) ) { |
| 87 | + b.fail( 'should return an ndarray' ); |
| 88 | + } |
| 89 | + b.pass( 'benchmark finished' ); |
| 90 | + b.end(); |
| 91 | +}); |
| 92 | + |
| 93 | +bench( pkg+':dtype=complex64', function benchmark( b ) { |
| 94 | + var x; |
| 95 | + var v; |
| 96 | + var i; |
| 97 | + |
| 98 | + v = new Complex64( 1.0, 2.0 ); |
| 99 | + |
| 100 | + b.tic(); |
| 101 | + for ( i = 0; i < b.iterations; i++ ) { |
| 102 | + x = scalar2ndarray( v, 'complex64' ); |
| 103 | + if ( x.length !== 1 ) { |
| 104 | + b.fail( 'should have length 1' ); |
| 105 | + } |
| 106 | + } |
| 107 | + b.toc(); |
| 108 | + if ( !isndarrayLike( x ) ) { |
| 109 | + b.fail( 'should return an ndarray' ); |
| 110 | + } |
| 111 | + b.pass( 'benchmark finished' ); |
| 112 | + b.end(); |
| 113 | +}); |
| 114 | + |
| 115 | +bench( pkg+':dtype=int32', function benchmark( b ) { |
| 116 | + var x; |
| 117 | + var i; |
| 118 | + |
| 119 | + b.tic(); |
| 120 | + for ( i = 0; i < b.iterations; i++ ) { |
| 121 | + x = scalar2ndarray( i, 'int32' ); |
| 122 | + if ( x.length !== 1 ) { |
| 123 | + b.fail( 'should have length 1' ); |
| 124 | + } |
| 125 | + } |
| 126 | + b.toc(); |
| 127 | + if ( !isndarrayLike( x ) ) { |
| 128 | + b.fail( 'should return an ndarray' ); |
| 129 | + } |
| 130 | + b.pass( 'benchmark finished' ); |
| 131 | + b.end(); |
| 132 | +}); |
| 133 | + |
| 134 | +bench( pkg+':dtype=uint32', function benchmark( b ) { |
| 135 | + var x; |
| 136 | + var i; |
| 137 | + |
| 138 | + b.tic(); |
| 139 | + for ( i = 0; i < b.iterations; i++ ) { |
| 140 | + x = scalar2ndarray( i, 'uint32' ); |
| 141 | + if ( x.length !== 1 ) { |
| 142 | + b.fail( 'should have length 1' ); |
| 143 | + } |
| 144 | + } |
| 145 | + b.toc(); |
| 146 | + if ( !isndarrayLike( x ) ) { |
| 147 | + b.fail( 'should return an ndarray' ); |
| 148 | + } |
| 149 | + b.pass( 'benchmark finished' ); |
| 150 | + b.end(); |
| 151 | +}); |
| 152 | + |
| 153 | +bench( pkg+':dtype=int16', function benchmark( b ) { |
| 154 | + var x; |
| 155 | + var i; |
| 156 | + |
| 157 | + b.tic(); |
| 158 | + for ( i = 0; i < b.iterations; i++ ) { |
| 159 | + x = scalar2ndarray( i, 'int16' ); |
| 160 | + if ( x.length !== 1 ) { |
| 161 | + b.fail( 'should have length 1' ); |
| 162 | + } |
| 163 | + } |
| 164 | + b.toc(); |
| 165 | + if ( !isndarrayLike( x ) ) { |
| 166 | + b.fail( 'should return an ndarray' ); |
| 167 | + } |
| 168 | + b.pass( 'benchmark finished' ); |
| 169 | + b.end(); |
| 170 | +}); |
| 171 | + |
| 172 | +bench( pkg+':dtype=uint16', function benchmark( b ) { |
| 173 | + var x; |
| 174 | + var i; |
| 175 | + |
| 176 | + b.tic(); |
| 177 | + for ( i = 0; i < b.iterations; i++ ) { |
| 178 | + x = scalar2ndarray( i, 'uint16' ); |
| 179 | + if ( x.length !== 1 ) { |
| 180 | + b.fail( 'should have length 1' ); |
| 181 | + } |
| 182 | + } |
| 183 | + b.toc(); |
| 184 | + if ( !isndarrayLike( x ) ) { |
| 185 | + b.fail( 'should return an ndarray' ); |
| 186 | + } |
| 187 | + b.pass( 'benchmark finished' ); |
| 188 | + b.end(); |
| 189 | +}); |
| 190 | + |
| 191 | +bench( pkg+':dtype=int8', function benchmark( b ) { |
| 192 | + var x; |
| 193 | + var i; |
| 194 | + |
| 195 | + b.tic(); |
| 196 | + for ( i = 0; i < b.iterations; i++ ) { |
| 197 | + x = scalar2ndarray( i, 'int8' ); |
| 198 | + if ( x.length !== 1 ) { |
| 199 | + b.fail( 'should have length 1' ); |
| 200 | + } |
| 201 | + } |
| 202 | + b.toc(); |
| 203 | + if ( !isndarrayLike( x ) ) { |
| 204 | + b.fail( 'should return an ndarray' ); |
| 205 | + } |
| 206 | + b.pass( 'benchmark finished' ); |
| 207 | + b.end(); |
| 208 | +}); |
| 209 | + |
| 210 | +bench( pkg+':dtype=uint8', function benchmark( b ) { |
| 211 | + var x; |
| 212 | + var i; |
| 213 | + |
| 214 | + b.tic(); |
| 215 | + for ( i = 0; i < b.iterations; i++ ) { |
| 216 | + x = scalar2ndarray( i, 'uint8' ); |
| 217 | + if ( x.length !== 1 ) { |
| 218 | + b.fail( 'should have length 1' ); |
| 219 | + } |
| 220 | + } |
| 221 | + b.toc(); |
| 222 | + if ( !isndarrayLike( x ) ) { |
| 223 | + b.fail( 'should return an ndarray' ); |
| 224 | + } |
| 225 | + b.pass( 'benchmark finished' ); |
| 226 | + b.end(); |
| 227 | +}); |
| 228 | + |
| 229 | +bench( pkg+':dtype=uint8c', function benchmark( b ) { |
| 230 | + var x; |
| 231 | + var i; |
| 232 | + |
| 233 | + b.tic(); |
| 234 | + for ( i = 0; i < b.iterations; i++ ) { |
| 235 | + x = scalar2ndarray( i, 'uint8c' ); |
| 236 | + if ( x.length !== 1 ) { |
| 237 | + b.fail( 'should have length 1' ); |
| 238 | + } |
| 239 | + } |
| 240 | + b.toc(); |
| 241 | + if ( !isndarrayLike( x ) ) { |
| 242 | + b.fail( 'should return an ndarray' ); |
| 243 | + } |
| 244 | + b.pass( 'benchmark finished' ); |
| 245 | + b.end(); |
| 246 | +}); |
| 247 | + |
| 248 | +bench( pkg+':dtype=generic', function benchmark( b ) { |
| 249 | + var x; |
| 250 | + var i; |
| 251 | + |
| 252 | + b.tic(); |
| 253 | + for ( i = 0; i < b.iterations; i++ ) { |
| 254 | + x = scalar2ndarray( i, 'generic' ); |
| 255 | + if ( x.length !== 1 ) { |
| 256 | + b.fail( 'should have length 1' ); |
| 257 | + } |
| 258 | + } |
| 259 | + b.toc(); |
| 260 | + if ( !isndarrayLike( x ) ) { |
| 261 | + b.fail( 'should return an ndarray' ); |
| 262 | + } |
| 263 | + b.pass( 'benchmark finished' ); |
| 264 | + b.end(); |
| 265 | +}); |
0 commit comments