在IF语句C 中测量时间
Measuring time in an if statement c++
好吧...因为两天前,我一直在研究一个项目,该项目记录了您的点击,并一遍又一遍地重复(就像bot一样(,问题在于在点击之间记录时间的时刻,因为在使用if语句上测量" steady_clock :: Now "的时间时,仅在if语句中声明,如果我尝试使其成为全局变量使用零值,编译器会给我带来错误
#include<chrono>
using namespace std::chrono;
auto start = NULL; //this is an error
int main()
{
if (!GetAsyncKeyState(VK_LBUTTON))
{
auto start = steady_clock::now();
}
else if (GetAsyncKeyState(VK_LBUTTON))
{
auto end = steady_clock::now();
std::chrono::duration<double> elapsed = end - start; //here the compiler throws me an error because start is not declared
}
}
如果有人回答我的问题,我真的很感激。
对不起我的英语...
这是decltype
有用的地方:
decltype(steady_clock::now()) start;
// ...
start = steady_clock::now();
是括号内表达式的类型。
首先,您正在尝试两次启动"启动",并且也试图以不正确的方式使用自动。当它看到自动启动= null 时,应该假设编译器应该假设?因此,请查找它的使用方式。
同时,我认为您要寻找的是这样的东西:
#include<chrono>
std::chrono::steady_clock::time_point start; // You can mention the type of start and end explicitly,.
std::chrono::steady_clock::time_point end;
int main()
{
if (!GetAsyncKeyState(VK_LBUTTON))
{
start = std::chrono::steady_clock::now();
}
else // don't think you needed an else-if here.
{
end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed = end - start;
}
}
相关文章:
- 用时钟测量程序的运行时间
- 为什么根据测量位置测量经过的时间时会有很大的不同?
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- 分别测量每个线程上花费的 CPU 时间(C++)
- 测量时间以在 c++ 中生成更好的随机数
- 给定使用 C++ 或 C,我如何测量在 linux 下进行线程切换需要多长时间?可能吗?
- Linux 测量时间问题! std::chrono, QueryPerformanceCounter, clock_ge
- 以相同的结果测量时间
- 在IF语句C 中测量时间
- 当我使用 cin 时,我如何使用 clock() 测量时间
- 在 GPU 上测量时间
- C++精确测量时间,单位为十进制毫秒
- 有没有办法在Windows上测量时间到微秒的精度
- 用C++测量时间
- 测量时间以执行简单的指令
- c++时钟测量时间不正确
- 为Windows c++测量时间,毫秒或微秒
- 获取CPU节拍和测量时间
- C 如何在 <time.h 中测量时间>
- 用c++语言测量时间,精度高