0

I've made a program which process a lot of data, and it takes forever at runtime, but looking in Task Manager I found out that the executable only uses a small part of my cpu and my RAM...

How can I tell my IDE to allocate more resources (as much as he can) to my program?

Running it in Release x64 helps but not enough.

#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;
}

this is the cycle which takes up so much time... For some reason even using pointers to library doesn't optimize it

  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/208766/discussion-on-question-by-temp-allocate-more-ram-to-executable). – Samuel Liew Feb 29 '20 at 13:07

1 Answers1

4

RAM doesn't make things go faster. RAM is just there to store data your program uses; if it's not using much then it doesn't need much.

Similarly, in terms of CPU usage, the program will use everything it can (the operating system can change priority, and there are APIs for that, but this is probably not your issue).

If you're seeing it using a fraction of CPU percentage, the chances are you're either waiting on I/O or writing a single threaded application that can only use a single core at any one time. If you've optimised your solution as much as possible on a single thread, then it's worth looking into breaking its work down across multiple threads.

What you need to do is use a tool called a profiler to find out where your code is spending its time and then use that information to optimise it. This will help you with microoptimisations especially, but for larger algorithmic changes (i.e. changing how it works entirely), you'll need to think about things at a higher level of abstraction.

OMGtechy
  • 7,935
  • 8
  • 48
  • 83
  • I already know where it takes so much time and I did as much as I could to optimise it, but there are many cycles which require tons of calculations –  Feb 29 '20 at 11:15
  • Wouldn't using more CPU help making it faster? –  Feb 29 '20 at 11:15
  • I've learnt C++ from a tutorial a few months ago, and it didn't mention what a thread is or how to use them –  Feb 29 '20 at 11:53
  • @Temp Google / Ecosia (yay for trees) is your friend ;) search for a C++ threads tutorial, ideally C++11 onwards if you can – OMGtechy Feb 29 '20 at 11:54