Skip to content

Commit 1ffbbb4

Browse files
Aidan63Aidan Lee
andauthored
add millisecond resolution timer (#790)
* add millisecond resolution timer * Don't get in a muddle with the variable names from other functions --------- Co-authored-by: Aidan Lee <aidan.lee@evcam.com>
1 parent 533baa5 commit 1ffbbb4

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/std/sys.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ HL_PRIM void hl_sys_vtune_init() {
202202
static double CURT = 0;
203203
#endif
204204

205+
#ifdef HL_WIN
206+
static LARGE_INTEGER qpcFrequency;
207+
#endif
208+
205209
HL_PRIM double hl_sys_time() {
206210
#ifdef HL_DEBUG_REPRO
207211
CURT += 0.001;
@@ -210,13 +214,9 @@ HL_PRIM double hl_sys_time() {
210214
#ifdef HL_WIN
211215
#define EPOCH_DIFF (134774*24*60*60.0)
212216
static double time_diff = 0.;
213-
static double freq = 0.;
214217
LARGE_INTEGER time;
215218

216-
if( freq == 0 ) {
217-
QueryPerformanceFrequency(&time);
218-
freq = (double)time.QuadPart;
219-
}
219+
double freq = (double)qpcFrequency.QuadPart;
220220
QueryPerformanceCounter(&time);
221221
if( time_diff == 0 ) {
222222
FILETIME ft;
@@ -235,6 +235,22 @@ HL_PRIM double hl_sys_time() {
235235
#endif
236236
}
237237

238+
HL_PRIM int64 hl_sys_timestamp_ms() {
239+
#ifdef HL_WIN
240+
LARGE_INTEGER time;
241+
QueryPerformanceCounter(&time);
242+
243+
return time.QuadPart * 1000LL / qpcFrequency.QuadPart;
244+
#else
245+
struct timespec ts;
246+
if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
247+
return 0;
248+
}
249+
250+
return ts.tv_sec * 1000 + (ts.tv_nsec / 1000000);
251+
#endif
252+
}
253+
238254
HL_PRIM vbyte *hl_sys_get_env( vbyte *v ) {
239255
return (vbyte*)getenv((pchar*)v);
240256
}
@@ -671,6 +687,9 @@ HL_PRIM varray *hl_sys_args() {
671687
static void *hl_file = NULL;
672688

673689
HL_PRIM void hl_sys_init(void **args, int nargs, void *hlfile) {
690+
#ifdef HL_WIN
691+
QueryPerformanceFrequency(&qpcFrequency);
692+
#endif
674693
sys_args = (pchar**)args;
675694
sys_nargs = nargs;
676695
hl_file = hlfile;
@@ -718,6 +737,7 @@ DEFINE_PRIM(_BYTES, sys_locale, _NO_ARG);
718737
DEFINE_PRIM(_VOID, sys_print, _BYTES);
719738
DEFINE_PRIM(_VOID, sys_exit, _I32);
720739
DEFINE_PRIM(_F64, sys_time, _NO_ARG);
740+
DEFINE_PRIM(_I64, sys_timestamp_ms, _NO_ARG);
721741
DEFINE_PRIM(_BYTES, sys_get_env, _BYTES);
722742
DEFINE_PRIM(_BOOL, sys_put_env, _BYTES _BYTES);
723743
DEFINE_PRIM(_ARR, sys_env, _NO_ARG);

0 commit comments

Comments
 (0)