-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.c
36 lines (28 loc) · 806 Bytes
/
timer.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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include "timer.h"
void stopwatch_start(stopwatch *sw){
if (sw == NULL)
return;
memset(&sw->begin, 0, sizeof(struct timeval));
memset(&sw->end , 0, sizeof(struct timeval));
gettimeofday(&sw->begin, NULL);
}
void stopwatch_stop(stopwatch *sw){
if (sw == NULL)
return;
gettimeofday(&sw->end, NULL);
}
double get_interval_by_sec(stopwatch *sw){
if (sw == NULL)
return 0;
return ((double)(sw->end.tv_sec-sw->begin.tv_sec)+(double)(sw->end.tv_usec-sw->begin.tv_usec)/1000000);
}
int get_interval_by_usec(stopwatch *sw){
if (sw == NULL)
return 0;
return ((sw->end.tv_sec-sw->begin.tv_sec)*1000000+(sw->end.tv_usec-sw->begin.tv_usec));
}