Skip to content

Instantly share code, notes, and snippets.

@greenlikeorange
Created January 16, 2015 07:44
Show Gist options
  • Save greenlikeorange/4af01dfc77d7472efae4 to your computer and use it in GitHub Desktop.
Save greenlikeorange/4af01dfc77d7472efae4 to your computer and use it in GitHub Desktop.
Process Time (nodejs)
function constr(){
return new ProcessTime();
}
function ProcessTime(){
this._start_time = new Date();
}
ProcessTime.prototype.done = function(format, nonPrint){
var differ = ((new Date()) - this._start_time);
var mms = differ%1000 !== 0 ? ( (differ/1000) +"").split(".")[1] : "0000";
var floor = Math.floor(differ/1000);
var h, m, s;
var res;
switch(format){
case "s":
res = floor + "." + mms;
break;
default:
h = Math.floor(floor/3600);
h = h < 10 ? "0"+h : h;
m = Math.floor(floor%3600/60);
m = m < 10 ? "0"+m : m;
s = floor%60;
s = s < 10 ? "0"+s : s;
res = h+"."+m+"."+s+"."+mms;
break;
}
if(!nonPrint)
console.log("Process Escape Time: " + res);
return res;
}
module.exports = constr;
@greenlikeorange
Copy link
Author

Usage

var count = 2;
var last_2 = [1,2];
var number = 0;
var sum = 2;
var escape = require('../processTime')();

while( (number = last_2[0] + last_2[1]) < 4000000){
  if(number%2 === 0)
    sum += number;
  last_2 = [last_2[1], number];
}


escape.done();  // "Process Escape Time: 00.00.00.001"
console.log("Sum is: " + sum);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment