smarttime/ts/smarttime.classes.hrtmeasurement.ts

46 lines
971 B
TypeScript
Raw Permalink Normal View History

2018-03-11 15:44:32 +00:00
import * as process from 'process';
2017-08-16 12:29:12 +00:00
/**
* easy high resolution time measurement
*/
export class HrtMeasurement {
2018-03-11 15:44:32 +00:00
nanoSeconds: number = null;
milliSeconds: number = null;
private _hrTimeStart = null;
private _hrTimeStopDiff = null;
private _started: boolean = false;
2017-08-16 12:29:12 +00:00
/**
* start the measurement
*/
2018-03-11 15:44:32 +00:00
start() {
this._started = true;
this._hrTimeStart = process.hrtime();
2017-08-16 12:29:12 +00:00
}
/**
* stop the measurement
*/
2018-03-11 15:44:32 +00:00
stop() {
2017-08-16 12:29:12 +00:00
if (this._started === false) {
2018-03-11 15:44:32 +00:00
console.log("Hasn't started yet");
return;
2017-08-16 12:29:12 +00:00
}
2018-03-11 15:44:32 +00:00
this._hrTimeStopDiff = process.hrtime(this._hrTimeStart);
this.nanoSeconds = this._hrTimeStopDiff[0] * 1e9 + this._hrTimeStopDiff[1];
this.milliSeconds = this.nanoSeconds / 1000000;
return this;
2017-08-16 12:29:12 +00:00
}
/**
* reset the measurement
*/
2018-03-11 15:44:32 +00:00
reset() {
this.nanoSeconds = null;
this.milliSeconds = null;
this._hrTimeStart = null;
this._hrTimeStopDiff = null;
this._started = false;
2017-08-16 12:29:12 +00:00
}
}