|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2018 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 randu = require( '@stdlib/random/base/randu' ); |
| 25 | +var floorf = require( '@stdlib/math/base/special/floorf' ); |
| 26 | +var roundf = require( '@stdlib/math/base/special/roundf' ); |
| 27 | +var pow = require( '@stdlib/math/base/special/pow' ); |
| 28 | +var absf = require( '@stdlib/math/base/special/absf' ); |
| 29 | +var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); |
| 30 | +var PHI = require( '@stdlib/constants/float32/phi' ); |
| 31 | +var pkg = require( './../package.json' ).name; |
| 32 | +var NEGALUCAS = require( './../lib/negalucas.json' ); |
| 33 | +var negalucasf = require( './../lib' ); |
| 34 | + |
| 35 | + |
| 36 | +// MAIN // |
| 37 | + |
| 38 | +bench( pkg, function benchmark( b ) { |
| 39 | + var x; |
| 40 | + var y; |
| 41 | + var i; |
| 42 | + |
| 43 | + b.tic(); |
| 44 | + for ( i = 0; i < b.iterations; i++ ) { |
| 45 | + x = -floorf( randu() * 34.0 ); |
| 46 | + y = negalucasf( x ); |
| 47 | + if ( isnanf( y ) ) { |
| 48 | + b.fail( 'should not return NaN' ); |
| 49 | + } |
| 50 | + } |
| 51 | + b.toc(); |
| 52 | + if ( isnanf( y ) ) { |
| 53 | + b.fail( 'should not return NaN' ); |
| 54 | + } |
| 55 | + b.pass( 'benchmark finished' ); |
| 56 | + b.end(); |
| 57 | +}); |
| 58 | + |
| 59 | +bench( pkg+'::analytic', function benchmark( b ) { |
| 60 | + var x; |
| 61 | + var y; |
| 62 | + var i; |
| 63 | + |
| 64 | + function negalucasf( n ) { |
| 65 | + var an = absf( n ); |
| 66 | + return pow( -1.0, an ) * roundf( pow( PHI, an ) ); |
| 67 | + } |
| 68 | + |
| 69 | + b.tic(); |
| 70 | + for ( i = 0; i < b.iterations; i++ ) { |
| 71 | + x = -floorf( randu() * 34.0 ); |
| 72 | + y = negalucasf( x ); |
| 73 | + if ( isnanf( y ) ) { |
| 74 | + b.fail( 'should not return NaN' ); |
| 75 | + } |
| 76 | + } |
| 77 | + b.toc(); |
| 78 | + if ( isnanf( y ) ) { |
| 79 | + b.fail( 'should not return NaN' ); |
| 80 | + } |
| 81 | + b.pass( 'benchmark finished' ); |
| 82 | + b.end(); |
| 83 | +}); |
| 84 | + |
| 85 | +bench( pkg+'::table', function benchmark( b ) { |
| 86 | + var x; |
| 87 | + var y; |
| 88 | + var i; |
| 89 | + |
| 90 | + b.tic(); |
| 91 | + for ( i = 0; i < b.iterations; i++ ) { |
| 92 | + x = -floorf( randu() * 34.0 ); |
| 93 | + y = NEGALUCAS[ absf( x ) ]; |
| 94 | + if ( isnanf( y ) ) { |
| 95 | + b.fail( 'should not return NaN' ); |
| 96 | + } |
| 97 | + } |
| 98 | + b.toc(); |
| 99 | + if ( isnanf( y ) ) { |
| 100 | + b.fail( 'should not return NaN' ); |
| 101 | + } |
| 102 | + b.pass( 'benchmark finished' ); |
| 103 | + b.end(); |
| 104 | +}); |
| 105 | + |
| 106 | +bench( pkg+'::naive_recursion', function benchmark( b ) { |
| 107 | + var x; |
| 108 | + var y; |
| 109 | + var i; |
| 110 | + |
| 111 | + function negalucasf( n ) { |
| 112 | + if ( n === 0 ) { |
| 113 | + return 2; |
| 114 | + } |
| 115 | + if ( n === -1 ) { |
| 116 | + return -1; |
| 117 | + } |
| 118 | + return negalucasf( n+2 ) - negalucasf( n+1 ); |
| 119 | + } |
| 120 | + |
| 121 | + b.tic(); |
| 122 | + for ( i = 0; i < b.iterations; i++ ) { |
| 123 | + x = -floorf( randu() * 20.0 ); // limit lower bound |
| 124 | + y = negalucasf( x ); |
| 125 | + if ( isnanf( y ) ) { |
| 126 | + b.fail( 'should not return NaN' ); |
| 127 | + } |
| 128 | + } |
| 129 | + b.toc(); |
| 130 | + if ( isnanf( y ) ) { |
| 131 | + b.fail( 'should not return NaN' ); |
| 132 | + } |
| 133 | + b.pass( 'benchmark finished' ); |
| 134 | + b.end(); |
| 135 | +}); |
| 136 | + |
| 137 | +bench( pkg+'::recursion_memoized', function benchmark( b ) { |
| 138 | + var arr; |
| 139 | + var N; |
| 140 | + var x; |
| 141 | + var y; |
| 142 | + var i; |
| 143 | + |
| 144 | + arr = new Array( 35 ); |
| 145 | + arr[ 0 ] = 2; |
| 146 | + arr[ 1 ] = -1; |
| 147 | + N = 1; |
| 148 | + |
| 149 | + function negalucasf( n ) { |
| 150 | + var an = absf( n ); |
| 151 | + if ( an <= N ) { |
| 152 | + return arr[ an ]; |
| 153 | + } |
| 154 | + arr[ an ] = negalucasf( n+2 ) - negalucasf( n+1 ); |
| 155 | + return arr[ an ]; |
| 156 | + } |
| 157 | + |
| 158 | + b.tic(); |
| 159 | + for ( i = 0; i < b.iterations; i++ ) { |
| 160 | + x = -floorf( randu() * 20.0 ); // limit lower bound |
| 161 | + y = negalucasf( x ); |
| 162 | + if ( isnanf( y ) ) { |
| 163 | + b.fail( 'should not return NaN' ); |
| 164 | + } |
| 165 | + } |
| 166 | + b.toc(); |
| 167 | + if ( isnanf( y ) ) { |
| 168 | + b.fail( 'should not return NaN' ); |
| 169 | + } |
| 170 | + b.pass( 'benchmark finished' ); |
| 171 | + b.end(); |
| 172 | +}); |
| 173 | + |
| 174 | +bench( pkg+'::naive_iterative', function benchmark( b ) { |
| 175 | + var x; |
| 176 | + var y; |
| 177 | + var i; |
| 178 | + |
| 179 | + function negalucasf( n ) { |
| 180 | + var arr; |
| 181 | + var an; |
| 182 | + var i; |
| 183 | + |
| 184 | + an = absf( n ); |
| 185 | + |
| 186 | + arr = new Array( an+1 ); |
| 187 | + arr[ 0 ] = 2; |
| 188 | + arr[ 1 ] = -1; |
| 189 | + for ( i = 2; i <= an; i++ ) { |
| 190 | + arr[ i ] = arr[ i-2 ] - arr[ i-1 ]; |
| 191 | + } |
| 192 | + return arr[ an ]; |
| 193 | + } |
| 194 | + |
| 195 | + b.tic(); |
| 196 | + for ( i = 0; i < b.iterations; i++ ) { |
| 197 | + x = -floorf( randu()*77.0 ); |
| 198 | + y = negalucasf( x ); |
| 199 | + if ( isnanf( y ) ) { |
| 200 | + b.fail( 'should not return NaN' ); |
| 201 | + } |
| 202 | + } |
| 203 | + b.toc(); |
| 204 | + if ( isnanf( y ) ) { |
| 205 | + b.fail( 'should not return NaN' ); |
| 206 | + } |
| 207 | + b.pass( 'benchmark finished' ); |
| 208 | + b.end(); |
| 209 | +}); |
| 210 | + |
| 211 | +bench( pkg+'::iterative', function benchmark( b ) { |
| 212 | + var x; |
| 213 | + var y; |
| 214 | + var i; |
| 215 | + |
| 216 | + function negalucasf( n ) { |
| 217 | + var an; |
| 218 | + var a; |
| 219 | + var b; |
| 220 | + var c; |
| 221 | + var i; |
| 222 | + |
| 223 | + an = absf( n ); |
| 224 | + |
| 225 | + a = 2; |
| 226 | + b = -1; |
| 227 | + for ( i = 2; i <= an; i++ ) { |
| 228 | + c = a - b; |
| 229 | + a = b; |
| 230 | + b = c; |
| 231 | + } |
| 232 | + return b; |
| 233 | + } |
| 234 | + |
| 235 | + b.tic(); |
| 236 | + for ( i = 0; i < b.iterations; i++ ) { |
| 237 | + x = -floorf( randu()*77.0 ); |
| 238 | + y = negalucasf( x ); |
| 239 | + if ( isnanf( y ) ) { |
| 240 | + b.fail( 'should not return NaN' ); |
| 241 | + } |
| 242 | + } |
| 243 | + b.toc(); |
| 244 | + if ( isnanf( y ) ) { |
| 245 | + b.fail( 'should not return NaN' ); |
| 246 | + } |
| 247 | + b.pass( 'benchmark finished' ); |
| 248 | + b.end(); |
| 249 | +}); |
| 250 | + |
| 251 | +bench( pkg+'::iterative_memoized', function benchmark( b ) { |
| 252 | + var arr; |
| 253 | + var N; |
| 254 | + var x; |
| 255 | + var y; |
| 256 | + var i; |
| 257 | + |
| 258 | + arr = new Array( 77 ); |
| 259 | + arr[ 0 ] = 2; |
| 260 | + arr[ 1 ] = -1; |
| 261 | + N = 2; |
| 262 | + |
| 263 | + function negalucasf( n ) { |
| 264 | + var an; |
| 265 | + var i; |
| 266 | + |
| 267 | + an = absf( n ); |
| 268 | + if ( an > N ) { |
| 269 | + for ( i = N; i <= an; i++ ) { |
| 270 | + arr[ i ] = arr[ i-2 ] - arr[ i-1 ]; |
| 271 | + } |
| 272 | + N = an; |
| 273 | + } |
| 274 | + return arr[ an ]; |
| 275 | + } |
| 276 | + |
| 277 | + b.tic(); |
| 278 | + for ( i = 0; i < b.iterations; i++ ) { |
| 279 | + x = -floorf( randu()*77.0 ); |
| 280 | + y = negalucasf( x ); |
| 281 | + if ( isnanf( y ) ) { |
| 282 | + b.fail( 'should not return NaN' ); |
| 283 | + } |
| 284 | + } |
| 285 | + b.toc(); |
| 286 | + if ( isnanf( y ) ) { |
| 287 | + b.fail( 'should not return NaN' ); |
| 288 | + } |
| 289 | + b.pass( 'benchmark finished' ); |
| 290 | + b.end(); |
| 291 | +}); |
0 commit comments