Speed ββin my project from pointing to int
I am working on a project in which a large number of objects interact with each other. And I have a performance issue with my code. Let's say it has the structure described below.
There is a class (let's call it "A") as follows:
class A
{
/*Some methods and variables*/
public:
const int* getPointerToTime() { return &mTime; }
private:
std::vector<B> mBList;
int mTime;
}
Then there is a class "B" like this
class B
{
/*Some methods and variables*/
public:
B(A* myA) { mTime = myA->getPointerToTime(); }
int getTime() { return *mTime; }
private:
std::vector<C> mCList;
const int* mTime;
}
Then there is another class "C" that has a method ("update") that will be called in every program cycle. eg:
class C
{
/*Some methods and variables*/
public:
C(B* myB) { mMyB = myB; }
void update()
{
int t = mMyB->getTime();
/*some things to do here with "t"*/
}
private:
B* mMyB;
}
So basically there are a large number of "B" objects in the "A" class, and there are a large number of "C" objects in the "B" class, and in each cycle, each of these objects must be updated. Everything works fine, but I tested its performance using visual studio performance analysis and I wonder (or maybe not ...) the line
int t = B->getTime();
became the hottest line with 38% of the total samples! And I use this method in other parts of the code. There is actually something else in the code that I can think of that calls this method a lot more than it does here, but the poll percentage is not even close to that used in class "C"! I suspected that the variable in class "A" is not cached in the CPU when the program reaches the update method "C", but a friend suggested that this is not the case, since the speed of computation from L3 and RAM can It does not matter much in speed. So what do you think?
source to share
What I think as far as I know Visual Studio
function B-> getTime (); called more times than any other function.
Since this function is called by C objects that are members of class B.
Given the code, there is a good chance this function is called more times than any other function.
For example, 10 Bs have 100 Cs each. Then B-> getTime () is called 1000 times in one loop.
source to share