统计cuda函数耗时:win10 + vs2015 + 实现gettimeofday

统计cuda函数耗时:

在利用CPU计时函数对GPU部分进行计时时时,要考虑的一个问题是:核函数的执行是异步执行的,所以必须加上核函数同步函数,才能得到准确的时间。

计时方式,示例代码如下:

double cpuSecond() {

    struct timeval tp;

    gettimeofday(&tp,NULL);

    return ((double)tp.tv_sec + (double)tp.tv_usec*1.e-6);

}

double iStart = cpuSecond();

function(argument list);

cudaDeviceSynchronize();  // 同步函数

double iElaps = cpuSecond() - iStart;

1. gettimeofday

gettimeofday是Linux上的函数,sys/time.h 只是在linux系统上可以调用。

使用方法如下:

#include <sys/time.h>
const char* srs_human_format_time()
{
    struct timeval tv;
    static char buf[23];
 
    memset(buf, 0, sizeof(buf));
 
    // clock time
    if (gettimeofday(&tv, NULL) == -1) {
        return buf;
    }
 
    // to calendar time
    struct tm* tm;
    if ((tm = localtime((const time_t*)&tv.tv_sec)) == NULL) {
        return buf;
    }
 
    snprintf(buf, sizeof(buf),
        "%d-%02d-%02d %02d:%02d:%02d.%03d",
        1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
        tm->tm_hour, tm->tm_min, tm->tm_sec,
        (int)(tv.tv_usec / 1000));
 
    buf[sizeof(buf) - 1] = 0;
 
    return buf;
}

该函数返回如 2018-12-12 17:32:11 的格式化时间字符串。

2. win10 + vs2015 实现获取系统时间:

#include <stdio.h>
#include <WinSock.h>
#define SECS_TO_FT_MULT 10000000  
static LARGE_INTEGER base_time;

typedef struct win_time_val
{
	/** The seconds part of the time. */
	long    sec;

	/** The miliseconds fraction of the time. */
	long    msec;

} win_time_val_t;

typedef struct win_time
{
	/** This represents day of week where value zero means Sunday */
	int wday;

	/** This represents day of month: 1-31 */
	int day;

	/** This represents month, with the value is 0 - 11 (zero is January) */
	int mon;

	/** This represent the actual year (unlike in ANSI libc where
	*  the value must be added by 1900).
	*/
	int year;

	/** This represents the second part, with the value is 0-59 */
	int sec;

	/** This represents the minute part, with the value is: 0-59 */
	int min;

	/** This represents the hour part, with the value is 0-23 */
	int hour;

	/** This represents the milisecond part, with the value is 0-999 */
	int msec;

}win_time_t;

// Find 1st Jan 1970 as a FILETIME   
static void get_base_time(LARGE_INTEGER *base_time)
{
	SYSTEMTIME st;
	FILETIME ft;

	memset(&st, 0, sizeof(st));
	st.wYear = 1970;
	st.wMonth = 1;
	st.wDay = 1;
	SystemTimeToFileTime(&st, &ft);

	base_time->LowPart = ft.dwLowDateTime;
	base_time->HighPart = ft.dwHighDateTime;
	base_time->QuadPart /= SECS_TO_FT_MULT;
}
int win_gettimeofday(win_time_val_t *tv)
{
	SYSTEMTIME st;
	FILETIME ft;
	LARGE_INTEGER li;
	static char get_base_time_flag = 0;

	if (get_base_time_flag == 0)
	{
		get_base_time(&base_time);
	}

	/* Standard Win32 GetLocalTime */
	GetLocalTime(&st);
	SystemTimeToFileTime(&st, &ft);

	li.LowPart = ft.dwLowDateTime;
	li.HighPart = ft.dwHighDateTime;
	li.QuadPart /= SECS_TO_FT_MULT;
	li.QuadPart -= base_time.QuadPart;

	tv->sec = li.LowPart;
	tv->msec = st.wMilliseconds;

	return 0;
}
int win_time(const win_time_val_t *tv, win_time_t *time)
{
	LARGE_INTEGER li;
	FILETIME ft;
	SYSTEMTIME st;

	li.QuadPart = tv->sec;
	li.QuadPart += base_time.QuadPart;
	li.QuadPart *= SECS_TO_FT_MULT;

	ft.dwLowDateTime = li.LowPart;
	ft.dwHighDateTime = li.HighPart;
	FileTimeToSystemTime(&ft, &st);

	time->year = st.wYear;
	time->mon = st.wMonth - 1;
	time->day = st.wDay;
	time->wday = st.wDayOfWeek;

	time->hour = st.wHour;
	time->min = st.wMinute;
	time->sec = st.wSecond;
	time->msec = tv->msec;

	return 0;
}
const char* srs_human_format_time()
{
	static char buf[23];
	memset(buf, 0, sizeof(buf));

	win_time_val_t wintv;
	win_time_t wintime;
	win_gettimeofday(&wintv);
	win_time(&wintv, &wintime);

	_snprintf(buf, sizeof(buf), "%d-%d-%d %d:%d:%d:%d",
		wintime.year, wintime.mon + 1, wintime.day,
		wintime.hour, wintime.min, wintime.sec, wintime.msec);
	buf[sizeof(buf) - 1] = 0;

	return buf;
}

使用方法:

printf("\n%s\n", srs_human_format_time());

实测结果:

2018-12-12 17:43:13:74

参考:https://blog.csdn.net/u012530451/article/details/79366946

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JoannaJuanCV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值