benchmark.c
5.43 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* others.c
*
* Created on: 23/04/2016
* Author: Pedro
*/
#include "benchmark.h"
#include <stdio.h>
#include "../config.h"
// for elapsed time calculation under windows
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <time.h>
#include <Winsock2.h>
#include <stdint.h>
int gettimeofday(struct timeval* tp, struct timezone * tzp)
{
static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
SYSTEMTIME system_time;
FILETIME file_time;
uint64_t time;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
time = ((uint64_t)file_time.dwLowDateTime);
time += ((uint64_t)file_time.dwHighDateTime) << 32;
tp->tv_sec = (long)((time - EPOCH) / 10000000L);
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
return 0;
}
#else
#include <sys/time.h>
#endif
/*
* format the elapsed time as "%02lu:%02lu.%03lu" (m:s.ms)
* elapsed_time - string to save the elapsed time
* start_sec - start seconds
* start_usec - start microseconds
* end_sec - end seconds
* end_usec - end microseconds
*/
void format_elapsed_time_m_s_ms(char *elapsed_time, __time_t start_sec, __suseconds_t start_usec, __time_t end_sec, __suseconds_t end_usec) {
cl_ulong min = (cl_ulong) (end_sec - start_sec) / 60;
cl_ulong sec = (cl_ulong) (end_sec - start_sec - (((end_sec - start_sec) / 60) * 60));
cl_ulong milisec;
if (end_usec > start_usec) {
milisec = (cl_ulong) ((end_usec - start_usec) / 1000);
} else {
milisec = (cl_ulong) ((1000000 - start_usec + end_usec) / 1000);
if (sec > 0) {
sec--;
} else {
if (min > 0) {
min--;
sec = 59;
}
}
}
sprintf(elapsed_time, "%02lu:%02lu.%03lu", min, sec, milisec);
}
/*
* format the elapsed time as "%02lu.%03lu" (s.ms)
* elapsed_time - string to save the elapsed time
* start_sec - start seconds
* start_usec - start microseconds
* end_sec - end seconds
* end_usec - end microseconds
*/
void format_elapsed_time_s_ms(char *elapsed_time, __time_t start_sec, __suseconds_t start_usec, __time_t end_sec, __suseconds_t end_usec) {
cl_ulong sec = (cl_ulong) (end_sec - start_sec);
cl_ulong milisec;
if (end_usec > start_usec) {
milisec = (cl_ulong) ((end_usec - start_usec) / 1000);
} else {
milisec = (cl_ulong) ((1000000 - start_usec + end_usec) / 1000);
sec--;
}
sprintf(elapsed_time, "%lu.%03lu", sec, milisec);
}
/*
* return the elapsed time in milliseconds
* start_sec - start seconds
* start_usec - start microseconds
* end_sec - end seconds
* end_usec - end microseconds
*/
cl_ulong get_elapsed_ms(__time_t start_sec, __suseconds_t start_usec, __time_t end_sec, __suseconds_t end_usec) {
cl_ulong min = (cl_ulong) ((end_sec - start_sec) / 60);
cl_ulong sec = (cl_ulong) (end_sec - start_sec - (((end_sec - start_sec) / 60) * 60));
cl_ulong milisec;
if (end_usec > start_usec) {
milisec = (cl_ulong) ((end_usec - start_usec) / 1000);
} else {
milisec = (cl_ulong) ((1000000 - start_usec + end_usec) / 1000);
if (sec > 0) {
sec--;
} else {
if (min > 0) {
min--;
sec = 59;
}
}
}
return min * 60 * 1000 + sec * 1000 + milisec;
}
/*
* format the time as "%02lu:%02lu.%03lu" (m:s.ms)
* time - string to save the formated time
* curr_sec - seconds
* curr_usec - microseconds
*/
void format_time_m_s_ms(char *time, __time_t curr_sec, __suseconds_t curr_usec) {
cl_ulong min = (cl_ulong) ((curr_sec - init_sec) / 60);
cl_ulong sec = (cl_ulong) (curr_sec - init_sec - (((curr_sec - init_sec) / 60) * 60));
cl_ulong milisec;
if (curr_usec > init_usec) {
milisec = (cl_ulong) ((curr_usec - init_usec) / 1000);
} else {
milisec = (cl_ulong) ((1000000 - init_usec + curr_usec) / 1000);
if (sec > 0) {
sec--;
} else {
if (min > 0) {
min--;
sec = 59;
}
}
}
sprintf(time, "%02lu:%02lu.%03lu", min, sec, milisec);
}
/*
* format the time as "%02lu.%03lu" (s.ms)
* time - string to save the formated time
* curr_sec - seconds
* curr_usec - microseconds
*/
void format_time_s_ms(char *time, __time_t curr_sec, __suseconds_t curr_usec) {
cl_ulong sec = (cl_ulong) (curr_sec - init_sec);
cl_ulong milisec;
if (curr_usec > init_usec) {
milisec = (cl_ulong) ((curr_usec - init_usec) / 1000);
} else if (curr_usec == init_usec) {
milisec = 0;
sec = 0;
} else {
milisec = (cl_ulong) ((1000000 - init_usec + curr_usec) / 1000);
sec--;
}
sprintf(time, "%04lu.%03lu", sec, milisec);
}
/*
* format the time as "%02lu.%03lu" (s.ms)
* time - string to save the formated time
* ms - milliseconds
*/
void format_ms_s_ms(char *time, cl_ulong ms) {
cl_ulong init_ms = (cl_ulong) (init_sec * 1000 + init_usec / 1000);
cl_ulong sec = (ms - init_ms) / 1000;
cl_ulong milisec = ms - init_ms - sec * 1000;
sprintf(time, "%04lu.%03lu", sec, milisec);
}
/*
* format nanoseconds as "%02lu:%02lu.%03lu" (m:s.ms)
* time - string to save the formated time
* nanosec - nanoseconds
*/
void format_nanosec_m_s_ms(char *time, cl_ulong nanosec) {
cl_ulong min = nanosec / 1000000000 / 60;
cl_ulong sec = nanosec / 1000000000 - (60 * min);
cl_ulong milisec = (nanosec - (min * 60 * 1000000000 + sec * 1000000000)) / 1000000;
sprintf(time, "%02lu:%02lu.%03lu", min, sec, milisec);
}
/*
* format nanoseconds as "%02lu.%03lu" (s.ms)
* time - string to save the formated time
* nanosec - nanoseconds
*/
void format_nanosec_s_ms(char *time, cl_ulong nanosec) {
cl_ulong sec = nanosec / 1000000000;
cl_ulong milisec = (nanosec - (sec * 1000000000)) / 1000000;
sprintf(time, "%04lu.%03lu", sec, milisec);
}