smarttime/ts/smarttime.classes.hrtmeasurement.ts

44 lines
913 B
TypeScript
Raw Permalink Normal View History

2017-08-16 12:29:12 +00:00
/**
* easy high resolution time measurement
*/
export class HrtMeasurement {
2019-06-17 14:54:39 +00:00
public nanoSeconds: number = null;
public milliSeconds: number = null;
2020-07-07 22:37:41 +00:00
private _milliStart: number = null;
private _milliDiff: number = null;
2018-03-11 15:44:32 +00:00
private _started: boolean = false;
2017-08-16 12:29:12 +00:00
/**
* start the measurement
*/
2019-06-17 14:54:39 +00:00
public start() {
2018-03-11 15:44:32 +00:00
this._started = true;
2020-07-07 22:37:41 +00:00
this._milliStart = Date.now();
2017-08-16 12:29:12 +00:00
}
/**
* stop the measurement
*/
2019-06-17 14:54:39 +00:00
public 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
}
2020-07-07 22:37:41 +00:00
this._milliDiff = Date.now() - this._milliStart;
this.nanoSeconds = this._milliDiff * 1000;
this.milliSeconds = this._milliDiff;
2018-03-11 15:44:32 +00:00
return this;
2017-08-16 12:29:12 +00:00
}
/**
* reset the measurement
*/
2019-06-17 14:54:39 +00:00
public reset() {
2018-03-11 15:44:32 +00:00
this.nanoSeconds = null;
this.milliSeconds = null;
2020-07-07 22:37:41 +00:00
this._milliStart = null;
this._milliDiff = null;
2018-03-11 15:44:32 +00:00
this._started = false;
2017-08-16 12:29:12 +00:00
}
}