time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

toc.js (1868B)


      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 isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives;
     24 var tic = require( './../../tic' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Returns a high-resolution time difference.
     31 *
     32 * ## Notes
     33 *
     34 * -   Output format: `[seconds, nanoseconds]`.
     35 *
     36 *
     37 * @param {NonNegativeIntegerArray} time - high-resolution time
     38 * @throws {TypeError} must provide a nonnegative integer array
     39 * @throws {RangeError} input array must have length `2`
     40 * @returns {NumberArray} high resolution time difference
     41 *
     42 * @example
     43 * var tic = require( '@stdlib/time/tic' );
     44 *
     45 * var start = tic();
     46 * var delta = toc( start );
     47 * // returns [<number>,<number>]
     48 */
     49 function toc( time ) {
     50 	var now = tic();
     51 	var sec;
     52 	var ns;
     53 	if ( !isNonNegativeIntegerArray( time ) ) {
     54 		throw new TypeError( 'invalid argument. Must provide an array of nonnegative integers. Value: `' + time + '`.' );
     55 	}
     56 	if ( time.length !== 2 ) {
     57 		throw new RangeError( 'invalid argument. Input array must have length `2`.' );
     58 	}
     59 	sec = now[ 0 ] - time[ 0 ];
     60 	ns = now[ 1 ] - time[ 1 ];
     61 	if ( sec > 0 && ns < 0 ) {
     62 		sec -= 1;
     63 		ns += 1e9;
     64 	}
     65 	else if ( sec < 0 && ns > 0 ) {
     66 		sec += 1;
     67 		ns -= 1e9;
     68 	}
     69 	return [ sec, ns ];
     70 }
     71 
     72 
     73 // EXPORTS //
     74 
     75 module.exports = toc;