Time a snippet.
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we’ve built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
Time a snippet.
bash
npm install @stdlib/utils-timeit
script
tag without installation and bundlers, use the [ES Module][es-module] available on the [esm
][esm-url] branch (see [README][esm-readme]).deno
][deno-url] branch (see [README][deno-readme] for usage intructions).umd
][umd-url] branch (see [README][umd-readme]).javascript
var timeit = require( '@stdlib/utils-timeit' );
javascript
var code = 'var x = Math.pow( Math.random(), 3 );';
code += 'if ( x !== x ) {';
code += 'throw new Error( \'Something went wrong.\' );';
code += '}';
timeit( code, done );
function done( error, results ) {
if ( error ) {
throw error;
}
console.dir( results );
/* e.g., =>
{
'iterations': 1000000,
'repeats': 3,
'min': [0,135734733], // [seconds,nanoseconds]
'elapsed': 0.135734733, // seconds
'rate': 7367311.062526641, // iterations/second
'times': [ // raw timing results
[0,145641393],
[0,135734733],
[0,140462721]
]
}
*/
}
options
:""
.""
.null
, the number of iterations is determined by trying successive powers of 10
until the total time is at least 0.1
seconds. Default: 1e6
.3
.boolean
indicating whether a snippet is asynchronous. Default: false
.javascript
var setup = 'var randu = require( \'@stdlib/random-base-randu\' );';
setup += 'var pow = require( \'@stdlib/math-base-special-pow\' );';
var code = 'var x = pow( randu(), 3 );';
code += 'if ( x !== x ) {';
code += 'throw new Error( \'Something went wrong.\' );';
code += '}';
var opts = {
'before': setup
};
timeit( code, opts, done );
function done( error, results ) {
if ( error ) {
throw error;
}
console.dir( results );
}
javascript
var setup = 'var randu = require( \'@stdlib/random-base-randu\' );';
setup += 'var hypot = require( \'@stdlib/math-base-special-hypot\' );';
var code = 'var h = hypot( randu()*10, randu()*10 );';
code += 'if ( h < 0 || h > 200 ) {';
code += 'throw new Error( \'Something went wrong.\' );';
code += '}';
var cleanup = 'if ( h !== h ) {';
cleanup += 'throw new Error( \'Something went wrong.\' );';
cleanup += '}';
var opts = {
'before': setup,
'after': cleanup
};
timeit( code, opts, done );
function done( error, results ) {
if ( error ) {
throw error;
}
console.dir( results );
}
asynchronous
option to true
.javascript
var code = 'var x = Math.pow( Math.random(), 3 );';
code += 'if ( x !== x ) {';
code += 'var err = new Error( \'Something went wrong.\' );';
code += 'next( err );';
code += '}';
code += 'process.nextTick( next );';
var opts = {
'iterations': 1e2,
'asynchronous': true
};
timeit( code, opts, done );
function done( error, results ) {
if ( error ) {
throw error;
}
console.dir( results );
}
asynchronous
is true
, the implementation assumes that before
, after
, and code
snippets are all asynchronous. Accordingly, these snippets should invoke a next( [error] )
callback once complete. For example, given the following snippet,javascript
setTimeout( done, 0 );
function done( error ) {
if ( error ) {
return next( error );
}
next();
}
javascript
function wrapped( state, next ) {
setTimeout( done, 0 );
function done( error ) {
if ( error ) {
return next( error );
}
next();
}
}
state
parameter is simply an empty {}
which allows the before
, after
, and code
snippets to share state.javascript
function before( state, next ) {
state.counter = 0;
}
function code( state, next ) {
setTimeout( done, 0 );
function done( error ) {
if ( error ) {
return next( error );
}
state.counter += 1;
next();
}
}
function after( state, next ) {
var err;
if ( state.counter !== state.counter ) {
err = new Error( 'Something went wrong!' );
return next( err );
}
next();
}
asynchronous
is true
, the main code
snippet is actually asynchronous. If a snippet releases the [zalgo][zalgo], an error complaining about exceeding the maximum call stack size is highly likely.javascript
var join = require( 'path' ).join;
var readFileSync = require( '@stdlib/fs-read-file' ).sync;
var timeit = require( '@stdlib/utils-timeit' );
var before = readFileSync( join( __dirname, 'examples', 'before.txt' ), 'utf8' );
var code = readFileSync( join( __dirname, 'examples', 'code.txt' ), 'utf8' );
var opts = {
'iterations': 1e6,
'repeats': 5,
'before': before
};
timeit( code, opts, done );
function done( error, results ) {
if ( error ) {
throw error;
}
console.dir( results );
}
bash
npm install -g @stdlib/utils-timeit-cli
text
Usage: timeit [options] [<code>]
Options:
-h, --help Print this message.
-V, --version Print the package version.
--iterations iter Number of iterations.
--repeats repeats Number of repeats. Default: 3.
--before setup Setup code.
--after cleanup Cleanup code.
--async Time asynchronous code.
--format fmt Output format: pretty, csv, json. Default: pretty.
csv
, the output consists of only raw timing results.--iterations
, the implementation tries successive powers of 10
until the total time is at least 0.1
seconds.bash
$ timeit "$(cat ./examples/code.txt)" --before "$(cat ./examples/before.txt)" --iterations 1000000
iterations: 1000000
repeats: 3
iterations/s: 7261975.851461222
elapsed time: 0.13770357 sec
lower bound: 0.13770357 usec/iteration
bash
$ timeit "$(cat ./examples/code.txt)" --before "$(cat ./examples/before.txt)" --iterations 1000000 --format json
{"iterations":1000000,"repeats":3,"min":[0,132431806],"elapsed":0.132431806,"rate":7551056.1261997735,"times":[[0,142115140],[0,132431806],[0,134808376]]}
bash
$ timeit "$(cat ./examples/code.txt)" --before "$(cat ./examples/before.txt)" --iterations 1000000 --format csv
seconds,nanoseconds
0,139365407
0,138033545
0,135175834
bash
$ cat ./examples/code.txt | timeit --before "$(cat ./examples/before.txt)" --iterations 1000000
iterations: 1000000
repeats: 3
iterations/s: 7433536.674260073
elapsed time: 0.134525468 sec
lower bound: 0.134525468 usec/iteration