So i have to generate 3kk diffrent 6-digit codes (i know its a lot) i wrote this program and it got quite a few flaws first and main problem is that everytime i start it, i got an error with "bad allocation size" but it works anywys so i tried to ignore it but the biggest number it ever generated was 30k so its only 1/100 of what i need. In bigger cases it just doesnt start i belive he lack memory so firstly is there any way i can make it work? On the other hand i tried to make it save to the file only uniqe codes but in 30k case it saved all of them, so is there a mistake in code or i just got lucky?
string tab[X];
int z=0;
while (z <=X)
{
tab[z]; // this will be an error on the last iteration, because z==X
}
Furthermore, lines 18.25 have a loop, where w increases. Should it start the loop from 0 every time?
Your 'tab' is a local variable and therefore it is in stack memory. That is relatively limited. You must use dynamic allocation; the heap memory has much more space. Consider std::vector<string> tab(X); instead of array.
Consider std::set<string> too. It might be more efficient. You don't actually need the whole array; you print every value, and write only the unique values, so a set of uniques should be enough.
I've tried this out using the existing code with the various bugs ironed out (I think!). It does work, but as the size is increased, the program spends more and more time searching the table to see whether the value is already there.
Well its ridiculously slow got 100k i 30min and itll prolog get slower :/ anyways I need it in the file so I think I just need to wait or mb urs is somehow faster?
If I'm honest, I don't know how sets work in terms of checking for currently existing values. Someone else here might be able to chime in on that. Given that, in terms of generation, it could be faster. In terms of outputting to the file, it'll be the same speed.
Took roughly between 90 seconds to two minutes to generate 3 million on my work laptop here, which is reasonable (but not stupendous) spec.
1. I made a wrong conclusion based on the original code: that you store only the unique among 3E+6 generated keys. As you apparently need 3E+6 unique codes, you really need space for that much. The latter versions do so.
2. @iHutch105: std::set is ordered. The order that you write keys out is not the order they were generated. This may or may not be significant. Therefore,
I don't know whether this is better or worse than the other version posted - but it is definitely faster than my first attempt. Took just somewhere between 45 seconds to 1 minute to run and used about 185MB of RAM..
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <cstdlib>
usingnamespace std;
int main()
{
constint size = 3000000;
set<string> tab;
constint len = 6;
string kod(len,'-'); // create string of 6 characters
int z=0;
ofstream File("C:\\file.txt");
while (z <size)
{
for (int j=0; j<len; j++)
{
kod[j]= ( rand() % ( 'Z' - 'A' + 1 ) ) + 'A';
}
if (tab.find(kod) == tab.end())
{
tab.insert(kod);
File << kod << endl;
z++;
}
}
File.close();
cout << "Done" << endl;
return 0;
}
EDIT: this is the shortest, and about the fastest version I came up with. One significant change is the use of newline '\n' rather than std::endl when writing to the file. That gave a big time saving on my machine.
Ok sorry for double post but i need conclusion First i want to thank everyone here and second dont ever use visual studio :D but now to be serious i used same code on the dev cpp and it acually finished in like 1 min or smth when MSV took 1,5h for ~200k dunno if i messed up with smth or whatever reason but ms used like 200mb of ram and 15% of CPU when dev took all my 4gb and whole cpu mb i need to work with the configuration or whatever. Anyways im so happy its over :)