文章目录
- 1.代码解析
- 2.作者答疑
1.代码解析
在算力急剧膨胀的时代,测试代码的运行时间,关系到代码的性能,在win32下如何有效的测试运行时间,可参照如下代码:
复制代码
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#include <time.h> #include <iostream> // 定义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 << dur << std::endl; if (dur>=1000) { break; } Sleep(10); } }
希望能够帮到需要时间测试代码的朋友。
2.作者答疑
如有疑问,敬请留言。
最后
以上就是单纯小伙最近收集整理的关于C++-测试代码运行时间-精确到微秒-GetSystemTimeAsFileTime的全部内容,更多相关C++-测试代码运行时间-精确到微秒-GetSystemTimeAsFileTime内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复