Skip to content

Commit 47722bb

Browse files
committed
Add benchmarks and update example code
1 parent 976ff3e commit 47722bb

File tree

9 files changed

+122
-26
lines changed

9 files changed

+122
-26
lines changed

lib/node_modules/@stdlib/utils/timeit/README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,10 @@ var join = require( 'path' ).join;
257257
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
258258
var timeit = require( '@stdlib/utils/timeit' );
259259

260-
var before;
261-
var code;
262-
var opts;
260+
var before = readFileSync( join( __dirname, 'examples', 'before.txt' ), 'utf8' );
261+
var code = readFileSync( join( __dirname, 'examples', 'code.txt' ), 'utf8' );
263262

264-
before = readFileSync( join( __dirname, 'examples', 'before.txt' ), 'utf8' );
265-
code = readFileSync( join( __dirname, 'examples', 'code.txt' ), 'utf8' );
266-
267-
opts = {
263+
var opts = {
268264
'iterations': 1e6,
269265
'repeats': 5,
270266
'before': before
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
var hypot = require( '@stdlib/math/base/special/hypot' );
3+
var randu = require( '@stdlib/random/base/randu' );
4+
5+
var x;
6+
var y;
7+
var h;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var hypot = require( '@stdlib/math/base/special/hypot' );
27+
var pkg = require( './../package.json' ).name;
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var z;
36+
var i;
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
x = ( randu()*100.0 ) - 50.0;
41+
y = ( randu()*100.0 ) - 50.0;
42+
z = hypot( x, y );
43+
if ( isnan( z ) ) {
44+
b.fail( 'should not return NaN' );
45+
}
46+
}
47+
b.toc();
48+
if ( isnan( z ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
var join = require( 'path' ).join;
22+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
23+
var timeit = require( './../lib' );
24+
25+
var before = readFileSync( join( __dirname, 'before.txt' ), 'utf8' );
26+
var code = readFileSync( join( __dirname, 'code.txt' ), 'utf8' );
27+
28+
var opts = {
29+
'iterations': 1e6,
30+
'repeats': 3,
31+
'before': before
32+
};
33+
34+
timeit( code, opts, done );
35+
36+
function done( error, results ) {
37+
if ( error ) {
38+
throw error;
39+
}
40+
console.dir( results );
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
x = randu()*100.0 - 50.0;
3+
y = randu()*100.0 - 50.0;
4+
5+
h = hypot( x, y );
6+
7+
if ( h !== h || h < 0.0 || h > 100.0 ) {
8+
throw new Error( 'Something went wrong!' );
9+
}

lib/node_modules/@stdlib/utils/timeit/examples/async.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@
2020

2121
var timeit = require( './../lib' );
2222

23-
var opts;
24-
var code;
25-
26-
opts = {
23+
var opts = {
2724
'iterations': 1e2,
2825
'repeats': 5,
2926
'asynchronous': true
3027
};
3128

32-
code = '';
29+
var code = '';
3330
code += 'var err;';
3431
code += 'var x = Math.pow( Math.random(), 3 );';
3532
code += 'if ( x !== x ) {';

lib/node_modules/@stdlib/utils/timeit/examples/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ var join = require( 'path' ).join;
2222
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
2323
var timeit = require( './../lib' );
2424

25-
var before;
26-
var code;
27-
var opts;
25+
var before = readFileSync( join( __dirname, 'before.txt' ), 'utf8' );
26+
var code = readFileSync( join( __dirname, 'code.txt' ), 'utf8' );
2827

29-
before = readFileSync( join( __dirname, 'before.txt' ), 'utf8' );
30-
code = readFileSync( join( __dirname, 'code.txt' ), 'utf8' );
31-
32-
opts = {
28+
var opts = {
3329
'iterations': 1e6,
3430
'repeats': 5,
3531
'before': before

lib/node_modules/@stdlib/utils/timeit/examples/iterations.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ var join = require( 'path' ).join;
2222
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
2323
var timeit = require( './../lib' );
2424

25-
var before;
26-
var code;
27-
var opts;
25+
var before = readFileSync( join( __dirname, 'before.txt' ), 'utf8' );
26+
var code = readFileSync( join( __dirname, 'code.txt' ), 'utf8' );
2827

29-
before = readFileSync( join( __dirname, 'before.txt' ), 'utf8' );
30-
code = readFileSync( join( __dirname, 'code.txt' ), 'utf8' );
31-
32-
opts = {
28+
var opts = {
3329
'iterations': null,
3430
'repeats': 3,
3531
'before': before

lib/node_modules/@stdlib/utils/timeit/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"main": "./lib",
2020
"browser": "./lib/browser.js",
2121
"directories": {
22+
"benchmark": "./benchmark",
2223
"doc": "./docs",
2324
"example": "./examples",
2425
"lib": "./lib",

0 commit comments

Comments
 (0)