c++ - 简单程序的高CPU使用率

标签 c++ cpu-usage sfml

下面的代码是针对一个空窗口的,但在我的 Intel 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 Docs :

If a limit is set, the window will use a small delay after each call to display() to ensure that the current frame lasted long enough to match the framerate limit.

关于c++ - 简单程序的高CPU使用率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34314892/

相关文章:

c++ - 我的 glOrtho 有什么问题?负零而不是零

c++ - 使用 CUDA 进行循环卷积

mysql - mysql查询的高cpu使用率

iOS CPU 使用率飙升

c++ - C++ SFML Gamedev书-ResourceHolder类中的未解析外部符号

c++ - sf::RenderWIndows的 vector

c++ - 如何向 "interrupt"调用一个函数?

c++ - 尽管没有错误,openCL内核仍返回垃圾值

运行tomcat时Java限制CPU?

C++ 发布程序无法在 visual studio 2015 中加载图像和字体