[英]C++ Hot loop makes timing and function accurate but takes 20% CPU
Hey guys so this is my first question of Stack Overflow so if I've done something wrong my bad.嘿伙计们,这是我关于 Stack Overflow 的第一个问题,所以如果我做错了什么,那是我的坏事。
I have my program which is designed to make precise mouse movements at specific times, and it calculates the timing using a few hard coded variables and a timing function, which is running in microseconds for accuracy.我的程序旨在在特定时间进行精确的鼠标移动,它使用一些硬编码变量和计时函数来计算计时,计时函数以微秒为单位运行以确保准确性。 The program works perfectly as intended, and makes the correct movements at the correct timing etc.该程序按预期完美运行,并在正确的时间进行正确的动作等。
Only problem is, that the sleeping function I am using is a hot loop (as in, its a while loop without a sleep), so when the program is executing the movements, it can take up to 20% CPU usage.唯一的问题是,我使用的睡眠函数是一个热循环(例如,它是一个没有睡眠的 while 循环),所以当程序执行运动时,它可能会占用 20% 的 CPU 使用率。 The context of this is in a game, and can drop FPS in game from 60 down to 30 with lots of stuttering, making the game unplayable.这种情况发生在游戏中,并且可以将游戏中的 FPS 从 60 降低到 30,并且会出现很多卡顿,从而使游戏无法玩。 I am still learning c++, so any help is greatly appreciated.我仍在学习 C++,因此非常感谢任何帮助。 Below is some snippets of my code to show what I am trying to explain.下面是我的一些代码片段,以显示我要解释的内容。
this is where the sleep is called for some context这是在某些上下文中调用 sleep 的地方
void foo(paramenters and stuff not rly important)
{
Code is doing a bunch of movement stuff here not important blah blah
//after doing its first movement of many, it calls this sleep function from if statement (Time::Sleep) so it knows how long to sleep before executing the next movement.
if (repeat_delay - animation > 0) Time::Sleep(repeat_delay - animation, excess);
}
Now here is the actual sleeping function, which, after using the visual studio performance debugger I can see is using all my resources.现在这里是实际的睡眠功能,在使用 Visual Studio 性能调试器后,我可以看到它正在使用我的所有资源。 All of the parameters in this function are accounted for already, like I said before, the code works perfectly, apart from performance.这个函数中的所有参数都已经考虑过了,就像我之前说的,除了性能之外,代码运行得很好。
#include "Time.hpp"
#include <windows.h>
namespace Time
{
void Sleep(int64_t sleep_ms, std::chrono::time_point<std::chrono::steady_clock> start)
{
sleep_ms *= 1000;
auto truncated = (sleep_ms - std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start).count()) / 1000;
while (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start).count() < sleep_ms)
{
if (truncated)
{
std::this_thread::sleep_for(std::chrono::milliseconds(truncated));
truncated = 0;
}
/*
I have attempted putting even a 1 microsecond sleep in here, which brings CPU usage down to
0.5%
which is great, but my movements slowed right down, and even after attempting to speed up
the movements manually by altering a the movement functions mouse speed variable, it just
makes the movements inaccurate. How can I improve performance here without sacrificing
accuracy
*/
}
}
}
Why did you write a sleep function?你为什么要写一个睡眠函数? Just use std::this_thread::sleep_for
as it doesn't use any resources and is reasonably accurate.只需使用std::this_thread::sleep_for
因为它不使用任何资源并且相当准确。
Its' accuracy might depend on platform.它的准确性可能取决于平台。 On my Windows 10 PC it is accurate within 1 millisecond which should be suitable for durations over 10ms (= 100fps).在我的 Windows 10 PC 上,它的准确度在 1 毫秒内,这应该适用于超过 10 毫秒(= 100 fps)的持续时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.