you are using an O(n*n*n) algorithm to populate the a array ... a loop in main, a loop in recurse, and the recursion itself which is also a loop of sorts.
Perhaps there is a way to do this with less work, somehow?
However, the trouble is your print statement. Printing millions of lines takes forever on a console program. Try it... comment out your call to print in the function, and run it... I did this and it took less than 2 seconds for v=14.
and finally, try it with the print statement in the code, and redirect the output to a file (in your console, start the program normally and add this: >filename to the end of the command). This still takes quite some time, but its reasonable for the gigabytes of output you asked it to generate. I have a decent laptop, and its taken about 10 min redirected to a file. File was 1.1GB in size.
This is a lesson you never forget lol, first month out of school on the job I had an engineer ask me to speed up his program, which had like 500 lines of print statements on what loop it was in and what it was doing. I took all that crap out and it ran instantly (it was taking 2 days per with it in). Naturally, he put the prints back in, sigh.
it is notably faster (well, a few%... 15, 20% faster roughly, I didnt precise time it) if you just keep it dumb -- set a static constant variable to a.size the first time in print. It may be just as good to make your iterator static (?).
1 2 3 4 5 6 7 8 9
|
void print(std::vector<int>& a) {
static int i;
static int j = a.size();
for(i = 0; i < j; i++)
cout<< a[i] << " ";
//ostream_iterator<int> i(cout, " ");
//copy(a.begin(), a.end(), i);
cout << "\n";
}
|