简单程序的高CPU使用率
High CPU usage of simple program
下面的代码是针对一个空窗口的,但在我的英特尔i3上显示了25%的相对较高的CPU使用率。我也尝试了setFramerateLimit
,没有任何变化。有没有办法减少CPU的使用?
#include<SFML/Window.hpp>
void processEvents(sf::Window& window);
int main()
{
sf::Window window(sf::VideoMode(800, 600), "My Window", sf::Style::Close);
window.setVerticalSyncEnabled(true);
while (window.isOpen())
{
processEvents(window);
}
return 0;
}
void processEvents(sf::Window& window)
{
sf::Event event;
window.pollEvent(event);
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
由于您没有在循环中调用window.display()
,因此需要注意将线程暂停适当的时间,设置为sf::RenderWindow::setVerticalSyncEnabled
或sf::RenderWindow::setMaxFramerateLimit
。
试试这个:
while (window.isOpen())
{
processEvents(window);
// this makes the thread sleep
// (for ~16.7ms minus the time already spent since
// the previous window.display() if synced with 60FPS)
window.display();
}
来自SFML文档:
如果设置了限制,则窗口将在每次调用
display()
后使用一个小延迟,以确保当前帧持续足够长的时间以匹配帧速率限制。
问题是
while (window.isOpen())
{
processEvents(window);
}
是一个没有停顿的循环。由于像这样的循环通常会消耗100%的CPU,我不得不猜测你有一个4核CPU,所以它消耗了一个完整的核,相当于CPU容量的25%。
您可以在循环中添加一个暂停,使其不是100%运行,也可以一起更改事件处理。
相关文章:
- 编写一个函数以使用 n 百分比的 CPU 使用率
- 对于等待以 std::future wait() 返回的函数的 CPU 使用率或检查标志在循环中休眠一段时间哪个更好?
- 如何降低应用程序的 CPU 使用率?
- 如何以编程方式获取任务管理器进程CPU使用率(不是PerfMon API)
- 一段时间后 CPU 使用率高
- 高CPU使用率,在API桌面复制中获取帧之间具有不同的超时间隔
- GetDC ReleaseDC 在特定上下文中的高 CPU 使用率
- 从 C++11 代码中获取系统内存和 CPU 使用率
- 在 Visual Studio 中记录 CPU 使用率
- C++如何允许等待而不会过多的 CPU 使用率
- Qtcreator 冻结,CPU 使用率为 100%
- 使用 fgets() c++ 读取大型 txt 文件时的 CPU 使用率为 99%
- OpenCL 的 CPU 使用率意外
- Loop Wregex始终返回false和高CPU使用率约50%
- 简单的C++SFML程序高CPU使用率
- 使用 PID 获取内存和 CPU 使用率
- 如何在TCP端口上实时传输数据期间减少CPU使用率
- WinAPI:在WM_PAINT省略 BeginPaint 和 EndPaint 会导致 100% 的 CPU 使用率
- 当CPU使用率高时,OpenCV会堆积内存吗
- 如何在运行SDL程序时降低CPU使用率