Correct use of class objects (for loops)

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);
}
Last edited on
Option 3 will of course use a lot more memory than the other two because it stores all N objects in memory at the same time.

Option 1 is potentially faster than option 2 because it avoids having to call the constructor each time but the effect of this depends on what the constructor do and how well the compiler is able to optimize it. You did turn on optimizations before measuring, didn't you?
Topic archived. No new replies allowed.