首页 > 解决方案 > 为可执行文件分配更多 RAM

问题描述

我制作了一个处理大量数据的程序,它在运行时需要很长时间,但是在任务管理器中我发现可执行文件只使用了我的 cpu 和 RAM 的一小部分......

如何告诉我的 IDE 为我的程序分配更多资源(尽可能多)?

在版本 x64 中运行它有帮助,但还不够。

#include <cstddef>
#include <iostream>
#include <utility>
#include <vector>

int main() {
    using namespace std;

    struct library {
        int num = 0;

        unsigned int total = 0;

        int booksnum = 0;
        int signup = 0;
        int ship = 0;

        vector<int> scores;
    };

    unsigned int libraries = 30000; // in the program this number is read a file
    unsigned int books = 20000;     // in the program this number is read a file
    unsigned int days = 40000;      // in the program this number is read a file

    vector<int> scores(books, 0);
    vector<library*> all(libraries);

    for(auto& it : all) {
        it = new library;
        it->booksnum = 15000; // in the program this number is read a file
        it->signup = 50000;   // in the program this number is read a file
        it->ship = 99999;     // in the program this number is read a file
        it->scores.resize(it->booksnum, 0);
    }

    unsigned int past = 0;

    for(size_t done = 0; done < all.size(); done++) {
        if(!(done % 1000)) cout << done << '-' << all.size() << endl;
        for(size_t m = done; m < all.size() - 1; m++) {
            all[m]->total = 0;
            {
                double run = past + all[m]->signup;
                for(auto at : all[m]->scores) {
                    if(days - run > 0) {
                        all[m]->total += scores[at];
                        run += 1. / all[m]->ship;
                    } else
                        break;
                }
            }
        }
        for(size_t n = done; n < all.size(); n++)
            for(size_t m = 0; m < all.size() - 1; m++) {
                if(all[m]->total < all[m + 1]->total) swap(all[m], all[m + 1]);
            }
        past += all[done]->signup;
        if (past > days) break;
    }

    return 0;
}

这是一个占用这么多时间的循环......出于某种原因,即使使用指针library也不能优化它

标签: c++processexeram

解决方案


RAM 不会让事情变得更快。RAM 只是用来存储程序使用的数据;如果它不使用太多,那么它不需要太多。

同样,在 CPU 使用率方面,程序将使用它可以使用的所有内容(操作系统可以更改优先级,并且有用于此的 API,但这可能不是您的问题)。

如果您看到它使用 CPU 百分比的一小部分,那么您可能正在等待 I/O 或编写一个单线程应用程序,该应用程序在任何时候只能使用一个内核。如果您已在单个线程上尽可能地优化了您的解决方案,那么值得考虑将其工作分解为多个线程。

您需要做的是使用一个称为分析器的工具来找出您的代码在哪里花费时间,然后使用该信息来优化它。这将特别帮助您进行微优化,但是对于较大的算法更改(即完全改变它的工作方式),您需要在更高的抽象级别上考虑问题。


推荐阅读