-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiming.c
38 lines (33 loc) · 872 Bytes
/
timing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* timing.c
* ndt: n-dimensional tracer
*
* Copyright (c) 2019 Bryan Franklin. All rights reserved.
*/
#include <sys/time.h>
#include <stdlib.h>
int timer_start(struct timeval *tv)
{
gettimeofday(tv,NULL);
return 0;
}
int timer_elapsed(struct timeval *tv, double *elapsed)
{
struct timeval end;
gettimeofday(&end,NULL);
*elapsed = (end.tv_sec-tv->tv_sec)+((end.tv_usec-tv->tv_usec)/1000000.0);
return 0;
}
double timer_remaining(struct timeval *tv, double curr, double total) {
if( total <= 0 )
return -1.0; /* invalid total */
if( curr <= 0 || curr > total ) {
return -2.0; /* curr outside valid range */
}
double progress = curr / total;
double elapsed;
timer_elapsed(tv,&elapsed);
double est_total = (elapsed / progress);
double ret = (1-progress) * est_total;
return ret;
}