C++ 计时 micro-/nanoseconds 不工作
C++ chrono micro-/nanoseconds not working
Edit: Chuck Walbourn's answer solved my problem. Working code is added at the bottom!
我正在尝试使用 chrono 库计算时差(小于毫秒),但我得到的结果非常 strange/inaccurate。此外,如果我尝试使用小于 1 毫秒(1000 us / 1000000 ns)的值 "sleep_for",程序会恰好休眠 1 毫秒。
我的代码:
#include <iostream>
#include <chrono>
int main() {
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 100; i++)
{
auto finish = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count() << std::endl;
}
}
我的输出:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1000000
1000000
1000000
1000000
1000000
2000100
2000100
2000100
3000100
3000100
4000200
4000200
5000200
5000200
6000300
6000300
7000400
7000400
8000400
8000400
9000500
9000500
10000500
10000500
11000600
11000600
12000600
12000600
12000600
13000700
13000700
14000800
14000800
15000800
15000800
15000800
16000900
16000900
17000900
17000900
18001000
18001000
19001000
19001000
19001000
20001100
20001100
21001200
21001200
22001200
22001200
23001300
23001300
23001300
24001300
24001300
25001400
25001400
26001400
26001400
27001500
27001500
28001600
28001600
29001600
29001600
29001600
30001700
30001700
31001700
31001700
32001800
32001800
32001800
33001800
33001800
34001900
34001900
35002000
35002000
36002000
36002000
37002100
Druk op een toets om door te gaan. . .
它显然只增加了 1 毫秒,但这仍然不能解释有时添加的随机 hunderd。有人可以帮我吗:(
我 运行 Visual Studio 2013 和 Windows 7
添加的工作代码:
#include <iostream>
#include <chrono>
#include <Windows.h>
LARGE_INTEGER freq;
LARGE_INTEGER t1, t2;
long elapsedTime;
int main() {
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&t1);
for (int i = 0; i < 100; i++)
{
QueryPerformanceCounter(&t2);
elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000000000 / freq.QuadPart;
std::cout << elapsedTime << std::endl;
}
}
在 VS 2012 和 2013 中,high_resolution_clock
基于系统时间,分辨率为 1 毫秒。在 VS 2015 中,high_resolution_clock
已正确更新为使用 QueryPerformanceCounter
,因此它具有预期的高分辨率。
见C++14 STL Features, Fixes, And Breaking Changes In Visual Studio 14 CTP1
sleep_until
中还有一些其他错误已在 VS 2015 Update 2 中修复。
Edit: Chuck Walbourn's answer solved my problem. Working code is added at the bottom!
我正在尝试使用 chrono 库计算时差(小于毫秒),但我得到的结果非常 strange/inaccurate。此外,如果我尝试使用小于 1 毫秒(1000 us / 1000000 ns)的值 "sleep_for",程序会恰好休眠 1 毫秒。
我的代码:
#include <iostream>
#include <chrono>
int main() {
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 100; i++)
{
auto finish = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count() << std::endl;
}
}
我的输出:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1000000
1000000
1000000
1000000
1000000
2000100
2000100
2000100
3000100
3000100
4000200
4000200
5000200
5000200
6000300
6000300
7000400
7000400
8000400
8000400
9000500
9000500
10000500
10000500
11000600
11000600
12000600
12000600
12000600
13000700
13000700
14000800
14000800
15000800
15000800
15000800
16000900
16000900
17000900
17000900
18001000
18001000
19001000
19001000
19001000
20001100
20001100
21001200
21001200
22001200
22001200
23001300
23001300
23001300
24001300
24001300
25001400
25001400
26001400
26001400
27001500
27001500
28001600
28001600
29001600
29001600
29001600
30001700
30001700
31001700
31001700
32001800
32001800
32001800
33001800
33001800
34001900
34001900
35002000
35002000
36002000
36002000
37002100
Druk op een toets om door te gaan. . .
它显然只增加了 1 毫秒,但这仍然不能解释有时添加的随机 hunderd。有人可以帮我吗:(
我 运行 Visual Studio 2013 和 Windows 7
添加的工作代码:
#include <iostream>
#include <chrono>
#include <Windows.h>
LARGE_INTEGER freq;
LARGE_INTEGER t1, t2;
long elapsedTime;
int main() {
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&t1);
for (int i = 0; i < 100; i++)
{
QueryPerformanceCounter(&t2);
elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000000000 / freq.QuadPart;
std::cout << elapsedTime << std::endl;
}
}
在 VS 2012 和 2013 中,high_resolution_clock
基于系统时间,分辨率为 1 毫秒。在 VS 2015 中,high_resolution_clock
已正确更新为使用 QueryPerformanceCounter
,因此它具有预期的高分辨率。
见C++14 STL Features, Fixes, And Breaking Changes In Visual Studio 14 CTP1
sleep_until
中还有一些其他错误已在 VS 2015 Update 2 中修复。