Skip to content

Commit cdf7b40

Browse files
authored
chore: add benchmark.js
Signed-off-by: Shabareesh Shetty <[email protected]>
1 parent 70871bf commit cdf7b40

File tree

1 file changed

+107
-0
lines changed
  • lib/node_modules/@stdlib/blas/ext/base/dnancusumkbn2/benchmark

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 bench = require( '@stdlib/bench' );
24+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
25+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
27+
var zeros = require( '@stdlib/array/zeros' );
28+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
29+
var pow = require( '@stdlib/math/base/special/pow' );
30+
var Float64Array = require( '@stdlib/array/float64' );
31+
var pkg = require( './../package.json' ).name;
32+
var dnancusumkbn2 = require( './../lib/dnancusumkbn2.js' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Returns a random number.
39+
*
40+
* @private
41+
* @returns {number} random number
42+
*/
43+
function rand() {
44+
if ( bernoulli( 0.5 ) < 1 ) {
45+
return discreteUniform( -10.0, 10.0 );
46+
}
47+
return NaN;
48+
}
49+
50+
/**
51+
* Creates a benchmark function.
52+
*
53+
* @private
54+
* @param {PositiveInteger} len - array length
55+
* @returns {Function} benchmark function
56+
*/
57+
function createBenchmark( len ) {
58+
var x = filledarrayBy( len, 'float64', rand );
59+
var y = zeros( len );
60+
return benchmark;
61+
62+
function benchmark( b ) {
63+
var v;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
v = dnancusumkbn2( x.length, 0.0, x, 1, y, 1 );
69+
if ( isnan( v[ i%len ] ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( v[ i%len ] ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( pkg+':len='+len, f );
104+
}
105+
}
106+
107+
main();

0 commit comments

Comments
 (0)