[英]Reducing the CPU usage of a thread or process
There a bunch of other questions like this, but the only substantial answer I've seen is the one where you use SetPriorityClass
to give priority to other processes. 还有很多像这样的问题,但我看到的唯一实质性答案就是你使用SetPriorityClass
优先考虑其他进程。 This is not what I want. 这不是我想要的。 I want to explicitly limit the CPU usage of my thread/process. 我想明确限制我的线程/进程的CPU使用率。
How can I do this? 我怎样才能做到这一点?
Edit: I can't improve the efficiency of the process itself, because I'm not controlling it. 编辑:我无法提高过程本身的效率,因为我没有控制它。 I'm injecting my code into a game which I'd like to 'automate' in the background. 我正在将我的代码注入到游戏中,我希望在后台“自动化”。
The best solution to limiting the cpu usage for a process or thread is to make sure that the thread or process uses less cpu. 限制进程或线程的cpu使用的最佳解决方案是确保线程或进程使用较少的cpu。
That can best be done by improving the efficiency of the code, or by calling it less often. 最好的办法是提高代码的效率,或者不经常调用它。 The aim is to make sure that the process doesn't continually consume all of its available time slice. 目的是确保该过程不会持续消耗其所有可用时间片。
Things to try: 要尝试的事情:
如何定期发出睡眠命令?
Your question is broad -- I don't know what it's doing. 你的问题很广泛 - 我不知道它在做什么。 You can certainly track the thread's I/O and force it to give up the cpu after a certain threshold is passed. 您当然可以跟踪线程的I / O并强制它在传递某个阈值后放弃cpu。
I ended up enumerating a list of threads, then having a 100ms timer that suspended the list of threads two out of every five iterations (which in theory reduces CPU usage by 40%). 我最终枚举了一个线程列表,然后有一个100ms的计时器,它在每五次迭代中有两次暂停线程列表(理论上这会减少40%的CPU使用率)。
Thanks for all the answers. 感谢所有的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.