early/ts/early.hrtMeasurement.ts
2022-03-22 00:14:49 +01:00

46 lines
1007 B
TypeScript

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