benchmark.c 5.41 KB
/*
 * 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);
}