无法让我的计时数一次以上
Can't get my chrono to count more often than once a ms
我试图让我的代码测量时间持续时间短于1ms,但只是不能。
我已经搜索了,但还没有设法理解如何做到这一点。我添加了一些我认为相关的代码。
template <class Clock>
void
display_precision()
{
typedef std::chrono::duration<double, std::nano> NS;
NS ns = typename Clock::duration(1);
std::cout << ns.count() << " nsn";
}
int main()
{
display_precision<std::chrono::high_resolution_clock>();
display_precision<std::chrono::system_clock>();
display_precision<std::chrono::steady_clock>();
std::cout << std::chrono::high_resolution_clock::period::num
<< "/" << std::chrono::high_resolution_clock::period::den;
std::chrono::high_resolution_clock::time_point nowTime;
std::chrono::high_resolution_clock::time_point startTime;
startTime = std::chrono::high_resolution_clock::now();
int count = 0;
do
{
nowTime = std::chrono::high_resolution_clock::now();
std::chrono::high_resolution_clock::duration diff = nowTime - startTime;
__int64 difference = std::chrono::duration_cast<std::chrono::microseconds>(diff).count();
printf("n%i", difference);
count++;
if (std::chrono::duration_cast<std::chrono::seconds>(diff).count() > 2) { break; }
} while (1);
}
我得到的输出是这样的:
100 ns
100 ns
100 ns
1 / 10000000
:
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
3000300
正如你所看到的,difference
每毫秒改变一次,并在其间保持不变,尽管它在此期间循环了15次左右(尽管我据说有100ns的精度)。
此外,这似乎不是一个printf瓶颈,因为我试着用数据加载一个向量,我得到了相同的结果。
[Windows 7 Professional, VS Community 2013]
这是VS2013的问题,在VS2015中修复。来自:Visual Studio 2015 RTM details
"
相关文章:
- 为什么我的信号处理程序只执行一次?
- 我的字符串数组一次打印出前两个字符串
- 为什么完整的字符串没有一次打印,但是当我尝试逐个字符打印时
- 我需要线程函数在不停止实际程序的情况下,每2秒打印一次随机数
- 如何防止我的循环额外运行一次?
- 为什么我的 cout 一次执行,尽管 cin 应该在两者之间读取?
- 计算一次占用的最大房间数
- 使用互斥锁将一次运行的线程数限制为 2
- GCC 5.4.0的一次昂贵的跳跃
- 如何只保存一次相同的面
- 使用 std::unique 和 vector.erase 删除除最后一次出现的重复元素之外的所有元素
- C++执行一次序列的最佳方法
- 只实例化派生类一次错误的 OOP
- 如何创建一个只能添加一次自身的类?
- 对于原始数组和std::vector,明显没有对下标进行过一次结束的说明.这个问题已经得到了决定性的解决吗?
- 带ACK的Vim:搜索所有不以一次结尾的#pragma
- 如何删除一次制作的所有纹理
- 只使用一次输入的单词
- Opencv未能捕获上一次之后的早期点击
- 如何使C或c++程序每X秒执行一次特定的任务,但其间仍执行其他任务