Blame view

Debug/src/utils/benchmark.c 5.41 KB
0c8ce2b0   Pedro Roque   missing files
1
2
3
4
/*
 * others.c
 *
 *  Created on: 23/04/2016
4d26a735   Pedro Roque   Increased recogni...
5
 *      Author: pedro
0c8ce2b0   Pedro Roque   missing files
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
 */

#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
 */
4d26a735   Pedro Roque   Increased recogni...
50
51
52
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));
0c8ce2b0   Pedro Roque   missing files
53
54
	cl_ulong milisec;
	if (end_usec > start_usec) {
4d26a735   Pedro Roque   Increased recogni...
55
		milisec = (cl_ulong)((end_usec - start_usec) / 1000);
0c8ce2b0   Pedro Roque   missing files
56
	} else {
4d26a735   Pedro Roque   Increased recogni...
57
		milisec = (cl_ulong)((1000000 - start_usec + end_usec) / 1000);
0c8ce2b0   Pedro Roque   missing files
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
		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
 */
4d26a735   Pedro Roque   Increased recogni...
79
80
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);
0c8ce2b0   Pedro Roque   missing files
81
82
	cl_ulong milisec;
	if (end_usec > start_usec) {
4d26a735   Pedro Roque   Increased recogni...
83
		milisec = (cl_ulong)((end_usec - start_usec) / 1000);
0c8ce2b0   Pedro Roque   missing files
84
	} else {
4d26a735   Pedro Roque   Increased recogni...
85
		milisec = (cl_ulong)((1000000 - start_usec + end_usec) / 1000);
0c8ce2b0   Pedro Roque   missing files
86
87
88
89
90
91
92
93
94
95
96
97
98
99
		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) {
4d26a735   Pedro Roque   Increased recogni...
100
101
	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));
0c8ce2b0   Pedro Roque   missing files
102
103
	cl_ulong milisec;
	if (end_usec > start_usec) {
4d26a735   Pedro Roque   Increased recogni...
104
		milisec = (cl_ulong)((end_usec - start_usec) / 1000);
0c8ce2b0   Pedro Roque   missing files
105
	} else {
4d26a735   Pedro Roque   Increased recogni...
106
		milisec = (cl_ulong)((1000000 - start_usec + end_usec) / 1000);
0c8ce2b0   Pedro Roque   missing files
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
		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
 */
4d26a735   Pedro Roque   Increased recogni...
126
127
128
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));
0c8ce2b0   Pedro Roque   missing files
129
130
	cl_ulong milisec;
	if (curr_usec > init_usec) {
4d26a735   Pedro Roque   Increased recogni...
131
		milisec = (cl_ulong)((curr_usec - init_usec) / 1000);
0c8ce2b0   Pedro Roque   missing files
132
	} else {
4d26a735   Pedro Roque   Increased recogni...
133
		milisec = (cl_ulong)((1000000 - init_usec + curr_usec) / 1000);
0c8ce2b0   Pedro Roque   missing files
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
		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
 */
4d26a735   Pedro Roque   Increased recogni...
153
154
void format_time_s_ms(char* time, __time_t curr_sec, __suseconds_t curr_usec) {
	cl_ulong sec = (cl_ulong)(curr_sec - init_sec);
0c8ce2b0   Pedro Roque   missing files
155
156
	cl_ulong milisec;
	if (curr_usec > init_usec) {
4d26a735   Pedro Roque   Increased recogni...
157
		milisec = (cl_ulong)((curr_usec - init_usec) / 1000);
0c8ce2b0   Pedro Roque   missing files
158
159
160
161
	} else if (curr_usec == init_usec) {
		milisec = 0;
		sec = 0;
	} else {
4d26a735   Pedro Roque   Increased recogni...
162
		milisec = (cl_ulong)((1000000 - init_usec + curr_usec) / 1000);
0c8ce2b0   Pedro Roque   missing files
163
164
165
166
167
168
169
170
171
172
173
		sec--;
	}

	sprintf(time, "%04lu.%03lu", sec, milisec);
}

/*
 * format the time as "%02lu.%03lu" (s.ms)
 * time - string to save the formated time
 * ms - milliseconds
 */
4d26a735   Pedro Roque   Increased recogni...
174
175
void format_ms_s_ms(char* time, cl_ulong ms) {
	cl_ulong init_ms = (cl_ulong)(init_sec * 1000 + init_usec / 1000);
0c8ce2b0   Pedro Roque   missing files
176
177
178
179
180
181
182
183
184
185
186
	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
 */
4d26a735   Pedro Roque   Increased recogni...
187
void format_nanosec_m_s_ms(char* time, cl_ulong nanosec) {
0c8ce2b0   Pedro Roque   missing files
188
189
190
191
192
193
194
195
196
197
198
199
	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
 */
4d26a735   Pedro Roque   Increased recogni...
200
void format_nanosec_s_ms(char* time, cl_ulong nanosec) {
0c8ce2b0   Pedro Roque   missing files
201
202
203
204
205
	cl_ulong sec = nanosec / 1000000000;
	cl_ulong milisec = (nanosec - (sec * 1000000000)) / 1000000;

	sprintf(time, "%04lu.%03lu", sec, milisec);
}