early/ts/early.hrtMeasurement.ts

46 lines
1007 B
TypeScript
Raw Normal View History

import * as process from 'process';
2017-04-23 18:57:01 +00:00
/**
* easy high resolution time measurement
*/
export class HrtMeasurement {
nanoSeconds: number = null;
milliSeconds: number = null;
2022-03-21 23:14:49 +00:00
private _hrTimeStart: [number, number] = null;
private _hrTimeStopDiff: [number, number] = null;
private _started: boolean = false;
2017-04-23 18:57:01 +00:00
/**
* start the measurement
*/
start() {
this._started = true;
this._hrTimeStart = process.hrtime();
2017-04-23 18:57:01 +00:00
}
/**
* stop the measurement
*/
stop() {
2017-04-23 18:57:01 +00:00
if (this._started === false) {
console.log("Hasn't started yet");
return;
2017-04-23 18:57:01 +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-04-23 18:57:01 +00:00
}
/**
* reset the measurement
*/
reset() {
this.nanoSeconds = null;
this.milliSeconds = null;
this._hrTimeStart = null;
this._hrTimeStopDiff = null;
this._started = false;
2017-04-23 18:57:01 +00:00
}
}