[英]Why can't I get a loop to spin for less than a millisecond in C++ on Windows with Chrono?
EDIT: I am using VS2013 and on Windows 7. 编辑:我正在使用VS2013和Windows 7上。
With the below code I'd expect to be able to have a time difference of at least one microsecond, however, when executed it builds it up to at least 1000 microseconds (one millisecond). 使用以下代码,我希望时差至少为1微秒,但是,在执行时,它的时差至少为1000微秒(一毫秒)。 What is the reasoning I'm not able to get a time lower then one millisecond? 我无法将时间缩短到不到一毫秒的原因是什么? Is there any way around this? 有没有办法解决?
// SleepTesting.cpp : Defines the entry point for the console application.
//
#include <chrono>
#include "windows.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
FILETIME startFileTime, endFileTime;
uint64_t ullStartTime, ullEndTime;
bool sleep = true;
auto start = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
auto elapsedTime = std::chrono::duration_cast<std::chrono::microseconds>(now - start);
GetSystemTimeAsFileTime(&startFileTime);
ullStartTime = static_cast<uint64_t>(startFileTime.dwHighDateTime) << 32 | startFileTime.dwLowDateTime;
while (sleep)
{
now = std::chrono::system_clock::now();
elapsedTime = std::chrono::duration_cast < std::chrono::microseconds > (now - start);
if (elapsedTime.count() > 0)
{
sleep = false;
}
}
GetSystemTimeAsFileTime(&endFileTime);
ullEndTime = static_cast<uint64_t>(endFileTime.dwHighDateTime) << 32 | endFileTime.dwLowDateTime;
uint64_t timeDifferenceHundredsOfNano = ullEndTime - ullStartTime;
std::cout << "Elapsed time with Chrono library: " << elapsedTime.count() << " micro-seconds" << std::endl;
std::cout << "Elapsed time with Windows.h FILETIME: " << timeDifferenceHundredsOfNano << " hundreds of nanoseconds" << std::endl;
return 0;
}
since you're using system_clock, I think that you can't get micro-second resolution on windows 7 (at least from what I've seen). 由于您使用的是system_clock,因此我认为您无法在Windows 7上获得微秒级的分辨率(至少从我所见)。 try high-resolution clock, but even that won't always work, since windows doesn't even guarantee that time elapsed between two consecutive operations is less then one millisecond, even without sleeping 尝试使用高分辨率时钟,但即使这样也不能总是起作用,因为Windows甚至不能保证两次连续操作之间经过的时间少于一毫秒,即使不睡觉也是如此
IIRC in VS2013 the system_clock (and highres_clock) are in clock ticks ie ms. VS2013中的IIRC,system_clock(和highres_clock)以时钟滴答表示,即ms。 If you need higher resolution you can go all Windows and take a look at QueryPerformanceCounter . 如果需要更高的分辨率,则可以使用所有Windows并查看QueryPerformanceCounter 。
LARGE_INTEGER startCount;
LARGE_INTEGER endCount;
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&startCount);
{...}
QueryPerformanceCounter(&endCount);
double startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
double endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
// endTimeInMicroSec - startTimeInMicroSec
disclaimer: ocular compilation 免责声明:眼动编译
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.