Hi,
I have one main loop and a sub-loop (if statement within the loop) that tracks N = 1e7 objects while saving every individual object's properties in a data file:
for(unsigned int i = 0; i < N; i++)
{
// do stuff
// save data
if(condition)
{
// do stuff
// save data
}
}
I have created one class that assigns unique properties (member variables) to every object.
I want to know, since there are so many objects, what the correct way (or best) is to loop over the objects of the class.
I have done it (with relative success) in three different ways, all three ways give acceptable results and all three ways have the same computational time when I set N = 1e5.
But the first one have the best computational time when setting N = 1e7, and I think the third one gives memory leaks since my computer did freeze when setting N = 1e7 (Although I did add this:
https://docs.microsoft.com/en-us/visualstudio/debugger/finding-memory-leaks-using-the-crt-library?view=vs-2017 in order to find memory leaks and none was detected)
Option 1: Best computational time for N = 1e7 - Declare object outside loop, save data for every object inside loop:
void Function_in_main();
int main()
{
Function_in_main();
}
void Function_in_main()
{
MyClass MyObject; // All member variables initialized to their null state
for(unsigned int i = 0; i < N; i++)
{
MyObject.Public_Member_Function(); // A member variable is calculated
MyObject.Another_Public_Member_Function();
maindatafile >> MyObject.getMemberVariable(); // Save data of objects in main loop
if(condition)
{
MyObject.New_Public_Member_Function();
MyObject.Another_New_Public_Member_Function();
subdatafile >> MyObject.getAnotherMemberVariable(); // Save data of objects in sub-loop
}
}
}
Option 2: Takes very long for N > 1e5 - Declare object inside loop and save data for every object inside loop:
void Function_in_main();
int main()
{
Function_in_main();
}
void Function_in_main()
{
for(unsigned int i = 0; i < N; i++)
{
MyClass MyObject; // All member variables initialized to their null state
MyObject.Public_Member_Function(); // A member variable is calculated
MyObject.Another_Public_Member_Function();
datafile >> MyObject.getMemberVariable();
if(condition)
{
MyObject.New_Public_Member_Function();
MyObject.Another_New_Public_Member_Function();
subdatafile >> MyObject.getAnotherMemberVariable();
}
}
}
Option 3: Also takes very long for N > 1e5, Think there are memory leaks even though none was detected with CTR lib
void Function_in_main(vector<MyClass>&);
void Another_Function_in_main(const vector<MyClass>&);
int main()
{
vector<MyClass> ClassVect
Function_in_main(ClassVect);
Another_Function_in_main(ClassVect);
}
void Function_in_main(vector<MyClass>& InputVect)
{
vector<MyClass> InputVect;
for(unsigned int i = 0; i < N; i++)
{
MyClass MyObject; // All member variables initialized to their null state
MyObject.Public_Member_Function(); // A member variable is calculated
MyObject.Another_Public_Member_Function();
if(condition)
{
MyObject.New_Public_Member_Function();
MyObject.Another_New_Public_Member_Function();
}
InputVect.push_back(MyObject);
}
}
void Another_Function_in_main( const vector<MyClass>& InputVect)
{
for(unsigned int i = 0; i < N; i++)
{
MyClass MyObject; // All member variables initialized to their null state
maindatafile << InputVect[i].getMemberVariable(); // Save Data of objects in main loop
if(condition)
{
subdatafile << InputVect[i].getMemberVariable(); // Save Data of objects in sub loop
}
}
}
Note: I have done all the ifstream and include stuff that C++ requires.
Also, since this is sometimes a factor when I'm trying to do stuff: This is a Monte Carlo Simulation, and I use the mersenne twister 64 bit PRNG in the <random> lib - The random numbers only gets drawn in the class itself (in the member functions (public and private)
The function I use to draw the random numbers look like this (ICpol is the class name):
double ICpol::getRand(const double & minDist, const double & maxDist)
{
static random_device randDev;
static mt19937_64 twister(randDev());
static uniform_real_distribution<double> dist;
dist.param(uniform_real_distribution<double>::param_type(minDist, maxDist));
return dist(twister);
}