您当前的位置: 首页 >  c++

插件开发

暂无认证

  • 1浏览

    0关注

    492博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C++-测试代码运行时间-精确到微秒-GetSystemTimeAsFileTime

插件开发 发布时间:2022-06-09 08:56:06 ,浏览量:1

文章目录
    • 1.代码解析
    • 2.作者答疑

1.代码解析

  在算力急剧膨胀的时代,测试代码的运行时间,关系到代码的性能,在win32下如何有效的测试运行时间,可参照如下代码:

#include 
#include 

// 定义64位整形
#if defined(_WIN32) && !defined(CYGWIN)
typedef __int64 int64_t;
#else
typedef long long int64t;
#endif  // _WIN32

// 获取系统的当前时间,单位微秒(us)
static int64_t GetSysTimeMicros()
{
#ifdef _WIN32
	//从1601年1月1日0:0:0:000到1970年1月1日0:0:0:000的时间(单位100ns)
#define EPOCHFILETIME   (116444736000000000UL)
	FILETIME ft;
	LARGE_INTEGER li;
	int64_t tt = 0;
	GetSystemTimeAsFileTime(&ft);
	li.LowPart = ft.dwLowDateTime;
	li.HighPart = ft.dwHighDateTime;
	// 从1970年1月1日0:0:0:000到现在的微秒数(UTC时间)
	tt = (li.QuadPart - EPOCHFILETIME) / 10;
	return tt;
#else
	timeval tv;
	gettimeofday(&tv, 0);
	return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
#endif // _WIN32
	return 0;
}

void Test()
{
	bool logoExist=false;
	int64_t start_time=GetSysTimeMicros();//监控指定时间
	while (!logoExist)
	{
		logoExist = FileAndFolderOperate::FileExist(remove_logo_filename);
		int64_t cur = GetSysTimeMicros();
		int64_t dur = cur - start_time;
		dur = dur / 1000; //毫秒
		std::cout             
关注
打赏
1665481431
查看更多评论
0.0393s