time-to-botec

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

sync.js (2205B)


      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 // MAIN //
     22 
     23 /**
     24 * Generates a source code body for synchronous execution.
     25 *
     26 * ## Notes
     27 *
     28 * -   Example output:
     29 *
     30 *     ```javascript
     31 *     "use strict";
     32 *
     33 *     var ctx = this;
     34 *     var t1, d1, i1;
     35 *
     36 *     // {{before}}
     37 *
     38 *     t1 = ctx.tic();
     39 *     for ( i1 = 0; i1 < 1e6; i1++ ) {
     40 *         // {{code}}
     41 *     }
     42 *     d1 = ctx.toc( t1 );
     43 *
     44 *     // {{after}}
     45 *
     46 *     ctx.done( null, d1 );
     47 *
     48 *     return 1;
     49 *     ```
     50 *
     51 *
     52 * @private
     53 * @param {number} id - id
     54 * @param {string} code - code to time
     55 * @param {Options} opts - function options
     56 * @param {string} opts.before - setup code
     57 * @param {string} opts.after - cleanup code
     58 * @param {PositiveInteger} opts.iterations - number of iterations
     59 * @returns {string} source code body
     60 */
     61 function generate( id, code, opts ) {
     62 	var src;
     63 	var ctx;
     64 	var t;
     65 	var d;
     66 	var i;
     67 
     68 	src = '"use strict";';
     69 
     70 	// Declare variables:
     71 	ctx = '__ctx$'+id+'__';
     72 	i = '__i$'+id+'__';
     73 	t = '__t$'+id+'__';
     74 	d = '__d$'+id+'__';
     75 
     76 	src += 'var '+ctx+' = this;';
     77 	src += 'var '+t+','+d+','+i+';';
     78 
     79 	// Insert the setup code:
     80 	src += opts.before+';';
     81 
     82 	// Start the timer:
     83 	src += t+' = '+ctx+'.tic();';
     84 
     85 	// Create the loop:
     86 	src += 'for ( '+i+'= 0; '+i+' < '+opts.iterations+'; '+i+'++ ) {';
     87 
     88 	// Insert the loop body:
     89 	src += code+';';
     90 
     91 	// Close the loop:
     92 	src += '}';
     93 
     94 	// Stop the timer:
     95 	src += d+' = '+ctx+'.toc( '+t+' );';
     96 
     97 	// Insert the cleanup code:
     98 	src += opts.after+';';
     99 
    100 	// Return results:
    101 	src += ctx+'.done( null, '+d+' );';
    102 
    103 	// Return a value:
    104 	src += 'return '+id+';';
    105 
    106 	return src;
    107 }
    108 
    109 
    110 // EXPORTS //
    111 
    112 module.exports = generate;