-
Notifications
You must be signed in to change notification settings - Fork 7
/
stimer.c
65 lines (57 loc) · 1.87 KB
/
stimer.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "stimer.h"
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <math.h>
#define TIME_MAX 100
#define TIME_WRAP 60;
#include <semaphore.h>
#include <pthread.h>
pthread_mutex_t lock_time_mtx = PTHREAD_MUTEX_INITIALIZER;
char time_diff[TIME_MAX];
void s_start(stimer_t *s_clock)
{
// pthread_mutex_lock(&lock_time_mtx);
clock_gettime(CLOCK_MONOTONIC, &(s_clock->tstart));
// pthread_mutex_unlock(&lock_time_mtx);
}
void s_stop(stimer_t *s_clock)
{
// pthread_mutex_lock(&lock_time_mtx);
clock_gettime(CLOCK_MONOTONIC, &(s_clock->tend));
// pthread_mutex_unlock(&lock_time_mtx);
}
void get_time_difference(stimer_t *s_clock)
{
// pthread_mutex_lock(&lock_time_mtx);
s_clock->difference.tv_sec = abs(s_clock->tend.tv_sec - s_clock->tstart.tv_sec);
s_clock->difference.tv_nsec = abs(s_clock->tend.tv_nsec - s_clock->tstart.tv_nsec);
s_clock->total_difference.tv_sec += s_clock->difference.tv_sec;
s_clock->total_difference.tv_nsec += s_clock->difference.tv_nsec;
// pthread_mutex_unlock(&lock_time_mtx);
}
char* show_time_difference(stimer_t *s_clock)
{
long int minutes=0,hours=0;
minutes = s_clock->difference.tv_sec / 60;
hours = minutes / 60;
sprintf(time_diff,"%ld hr,%ld min, %ld.%ld seconds",hours,minutes,s_clock->difference.tv_sec%60,s_clock->difference.tv_nsec);
return time_diff;
}
char* show_total_time_difference(stimer_t * s_clock)
{
long int minutes=0,hours=0;
minutes = s_clock->total_difference.tv_sec / 60;
hours = minutes/60;
sprintf(time_diff,"%ld hr,%ld min, %ld.%ld seconds",hours,minutes,s_clock->total_difference.tv_sec%60,s_clock->total_difference.tv_nsec);
return time_diff;
}
char* show_average_time(stimer_t * s_clock,int quantity)
{
if(quantity == 0)
{
return "0.0 second";
}
sprintf(time_diff,"%ld.%08ld seconds",(s_clock->total_difference.tv_sec / quantity),(s_clock->total_difference.tv_nsec / quantity));
return time_diff;
}