惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

博客园 - 吾非无心

MSB4019 找不到导入的项目“C:\Users\...\AppData\Local\QtMsBuild\vs-debugtools\qt_import.props” - 吾非无心 - 博客园 类/结构最后一个成员为类(string)时可能会出现“堆损坏”(HEAP CORRUPTION DETECTED)错误 GCC在C语言中内嵌汇编 asm __volatile__ QT删除python中的单行注释 double转int QJson 存储(u)longlong有问题 QT QPixmap QImage内存泄漏 QMetaObject::connectSlotsByName: No matching signal for QT数据库连接管理类 QT多重继承带来的问题及解决办法,记录备查 QT6 源码编译Win32 x86 stack smashing detected 莫名其妙的错误 strdump的问题 再加一个realloc的问题 linux下简单封装读写锁 codeblocks下libacl配置 centos8 安装、配置redis6 pyftpdlib中文乱码的解决之道 CentOS8让uwsgi开机自动启动django(无需登录,无需手动) libevent学习一
vc++高精度计时sleep
吾非无心 · 2021-09-06 · via 博客园 - 吾非无心
LARGE_INTEGER high_precision_sleep(LARGE_INTEGER preTime, unsigned long long   sleepTime) {
    LARGE_INTEGER t;
    QueryPerformanceCounter(&t);
    
    while(t.QuadPart-preTime.QuadPart<sleepTime)QueryPerformanceCounter(&t);
    //printf("sleepTime=%d\n", t.QuadPart-preTime.QuadPart);//调试时用来观察每次暂停时间。一般应略有不同才正确
    /*//这种方式与直接返回t精度差不多,但多了一个运算,划不来
preTime.QuadPart += sleepTime; return preTime;*/ return t; }

  使用方法:

LARGE_INTEGER freq,t0,t1,t3;

QueryPerformanceFrequency(&freq);//频率   计数/秒

//当前频率 TIMES_PER_SECOND 下间隔的计数
auto SLEEP_COUNT = freq.QuadPart / TIMES_PER_SECOND;

QueryPerformanceCounter(&t0);
QueryPerformanceCounter(&t3);
for (int i = 0; i < number; i++) {

   //do something

   srand(t3.LowPart);//srand()函数产生一个以当前时间开始的随机种子
  Sleep(rand() %20 );//TIMES_PER_SECOND=30,理论上每次Sleep(33.33ms),但不能Sleep(rand()%30),因为Sleep精度问题,可能实际上超过

  //BitBlt(dcMem, 0, 0, 1920, 1080, dcScreen, 0, 0, SRCCOPY);

  t3=high_precision_sleep(t3, SLEEP_COUNT);

}

QueryPerformanceCounter(&t1);

cout << "执行 "<< number << " 次, 耗时 " << (((t1.QuadPart - t0.QuadPart) * 1000000) / freq.QuadPart)<< " 微秒" << std::endl;

当TIMES_PER_SECOND=30,number=500时,理论耗时:

实际耗时:耗时 16668737 微秒、耗时 16668737 微秒、耗时 16667480 微秒...每次相差不大,在1ms左右