首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cpu负载过高。C++/sfml

cpu负载过高。C++/sfml
EN

Stack Overflow用户
提问于 2019-05-03 14:41:55
回答 2查看 176关注 0票数 0

我的程序以80%的速度加载处理器。前段时间我用gpu遇到了同样的问题,我用定时器解决了它。Cpu负载大约是50-60 %,现在是- 80 %。我做错了什么?我不能解决它的问题。

代码语言:javascript
复制
#include <fstream>
#include <SFML/Graphics.hpp>
#include <ctime>
using namespace std;
char * readFile(char * filePath, unsigned int &lengthBuffer, fstream &f) {
    f.open(filePath, ios::in | ios::app | ios::binary);
    f.seekg (0, f.end);
    lengthBuffer = f.tellg();
    f.seekg (0, f.beg);
    char * fileBuffer = new char[lengthBuffer];
    f.read(fileBuffer, lengthBuffer);
    f.close();
    return fileBuffer;
}
char * writeFile(char * fileBuffer, char * filePath, unsigned int &lengthBuffer, fstream &f, bool &fileCreated){
    filePath[23] += 1;
    f.open(filePath, ios::out | ios::app | ios::binary);
    f.write(fileBuffer, lengthBuffer);
    filePath[23] -= 1;
    fileCreated = 1;
    f.close();
    return fileBuffer;
}

void removeFile(char * filePath, bool &fileCreated) {
    filePath[23] += 1;
    remove(filePath);
    filePath[23] -= 1;
    fileCreated = 0;
}
unsigned int mouse(unsigned int &funcSelector, bool &mouseLeft, sf::RenderWindow &window) {
    mouseLeft = sf::Mouse::isButtonPressed(sf::Mouse::Left);
    sf::Vector2i mouse = sf::Mouse::getPosition(window);
    if (mouseLeft & mouse.y < 100) {
        funcSelector = 1 + mouse.x/100;
    }
    return funcSelector;
}
int main(){
    sf::RenderWindow window(sf::VideoMode(500, 400), "COT++", sf::Style::Titlebar);
    sf::VertexArray points(sf::Points, 3000);
    sf::Event event;
    fstream f;
    bool mouseLeft, fileCreated = 0;
    unsigned int n = 0, funcSelector = 0, lengthBuffer = 0;
    float start = 0, now = 0, x = 0.f, y = 1.f, pos = 0.f;
    char * fileBuffer, filePath[30] = "c:/users/79994/desktop/a.exe";
    while (x < 500.f){
        points[n].position = sf::Vector2f(x + pos, y + pos);
        points[n].color = sf::Color::Green;
        x += 1.f;
        n += 1;
        if (x == 500.f & y < 100.f){
            x = 0.f;
            y += 100.f;
        }
    }
    x = 100.f, y = 1.f;
    while (x < 600.f){
        points[n].position = sf::Vector2f(x + pos, y + pos);
        points[n].color = sf::Color::Green;
        y += 1.f;
        n += 1;
        if (y > 101.f){
            x += 100.f;
            y = 1.f;
        }
    }
    while (window.isOpen())
    {
        while (window.pollEvent(event)) {
            if((clock()-start) > 50){
            start = clock();
            switch(funcSelector){
                case 5: window.close();
                break;
                case 1: if (lengthBuffer == 0){
                    fileBuffer = readFile(filePath, lengthBuffer, f);
                }    
                break;
                case 2: if (lengthBuffer > 0 & fileCreated == 0) {
                    writeFile(fileBuffer, filePath, lengthBuffer, f, fileCreated);
                }
                break;
                case 3: removeFile(filePath, fileCreated);  
                break;
            }
            mouse(funcSelector, mouseLeft, window);
            window.clear();
            window.draw(points);
            window.display();
        }
    }
    }
    return 0;
}

附注:“看起来你的帖子主要是代码;请添加更多细节”-我想我已经描述了足够多的细节。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-03 15:04:42

这取决于你想做什么。对于您给出的简单示例,您可以使用waitEvent而不是pollEvent。我建议检查waitEvent是否适合您的需求,但它看起来会的。

但是,如果您坚持使用pollEvent,请继续阅读!

您的while(window.pollEvent(event))循环在100%的时间内全速运行,不间断地轮询,即使您的目标似乎是在每个50时钟上“工作”。(时钟与实际测量单位不是一致的,除非通过CLOCKS_PER_SECwhich is implementation defined...而且std::clock可能与挂壁时间无关。CLOCKS_PER_SEC might be hardcoded to 1 million when that isn't a meaningful value..。但我离题了。无论您如何跟踪代码编写的时间,您的问题都将存在,尽管您可能需要一些其他的计时机制。)

这里有一个可能的解决方案,只需对代码进行最小程度的更改。替换:

代码语言:javascript
复制
    while (window.pollEvent(event)) {
        if((clock()-start) > 50){

具有以下功能:

代码语言:javascript
复制
    while (window.pollEvent(event)) {
        const auto delta = clock() - start;
        if (delta < 50) {
          std::this_thread::sleep_for(std::chrono::microseconds(
            static_cast<int>(1E6 * (50 - delta) / CLOCKS_PER_SEC)));
        } else {

它所做的,不是不断地调用window.pollEvent(event),它会调用它,如果需要的话,休眠一段时间,然后做一些工作。

这种方法也有一些缺点,但它应该让你开始思考这个问题。

您还需要使用#include <chrono> (或者,如果您不使用C++11或更高版本,您可以找到其他方式在几乎正确的持续时间内睡眠)。

票数 2
EN

Stack Overflow用户

发布于 2019-05-03 15:07:05

仅仅通过查看代码来尝试和猜测或多或少是困难和不精确的。您应该尝试使用分析器来分析代码。

PS:你在"readFile“和"writeFile”中似乎也有一个内存泄漏,你分配了一个缓冲区,但再也不会释放它了。

代码语言:javascript
复制
char * readFile(char * filePath, unsigned int &lengthBuffer, fstream &f) {
    // [...]
    char * fileBuffer = new char[lengthBuffer];
    // here you allocated memory on the heap, but you'll never free it.
    // [...]
    return fileBuffer;
}

我还认为根本就不要使用那些动态字符数组和C文件。只需使用std::string和std::ostream即可。ostream使用更多的STL和字符串,您不必关心它的内存使用情况和潜在的内存泄漏。它会在自身之后进行清理,而不是char-array。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55964515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档