I am working on a monte carlo simulation code, with about one cpp-file per class.
While executing a checkOverlap
function is called for each pair of neighbouring particles, thus it is quite important for the execution time.
I noticed that I get a huge difference in execution time when I comment out that function compared to when I immediately return from it. While investigating further I found that
the evaluation speed of a class method depends on the class it is called from.
Currently I have a class layout similar to this pseudo-code
class CollisionLogic{
public:
bool testCall(){
return false;
}
bool checkOverlap(void particle1, void particle2){
//do something...
return value;
}
};
class InformationContainer{
public:
bool testCall(){
return false;
}
CollisionLogic CL;
};
In main code does the following
InformationContainer IC;
checkCollisionOn( particle P )
{
for( 'each neighbouring particle P_n' )
{
if( IC.CL.checkOverlap(P,P_n) )
return true;
/* Marker */
}
}
For testing purposes checkOverlap
returns false
as a first call. Then the evaluation of a frame takes 953ms. If I replace the marker comment by IC.CL.testCall()
the time increases to 1232ms, if replaced by IC.testCall()
it increases even further to 1305ms.
As both functions do exactly the same I assume I can rule out cpu computation time. So my questions are: What causes this to happen?, and what do I need to do to stop it?
Thank you!
Q/A Compiling:
I compile every code file into an object file with the flags '-O3 -std=gnu++11' and link them together afterwards with the same flags.
Ps: I have found several issues explanations on different c++ speed issues. [1,2,3,4] But not one like this.
[1] Speed of C program execution
[2] Why are elementwise additions much faster in separate loops than in a combined loop?
[3] c++ speed comparison iterator vs index
[4] Why are elementwise additions much faster in separate loops than in a combined loop?