简体   繁体   English

为什么在带有Chrono的Windows上的C ++中,为什么我的循环不能旋转不到一毫秒?

[英]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.

相关问题 为什么我不能使用 chrono 库? - Why can't I use chrono library? 自Windows中的时代以来的C ++ boost :: chrono vs std:chrono时间(获取当前系统时间) - C++ boost::chrono vs std:chrono time since epoch (get current system time) in windows 为什么我不能在 FOR LOOP、C++ 中使用 i/10? - Why I can't use i/10 in FOR LOOP, C++? 如何在c ++中随机但非常少量地旋转for循环? - How can I spin in a for loop for a random but very small amount of time in c++? C ++ - 我们如何在linux中获得毫秒时间戳? - C++ - How can we get a millisecond timestamp in linux? 如何使 Windows 上的线程睡眠时间少于一毫秒 - How to make thread sleep less than a millisecond on Windows 使用C ++ Chrono处理更新循环? - Handling an update loop using C++ Chrono? 在C ++中为什么我不能像这样编写for()循环:for(int i = 1,double i2 = 0; - In C++ why can't I write a for() loop like this: for( int i = 1, double i2 = 0; 当我插入地图c ++时,为什么不调用小于运算符? - Why isnt the less than operator being called when I insert into a map c++? 使用Windows和C ++ chrono为每个人安排一个事件1ms - 我要求的太多了吗? - Scheduling an event for everyone 1ms using Windows and C++ chrono - am I asking for too much?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM