Jump to content
  • Advertisement
Sign in to follow this  
DarkZoulz

Speed diffrence?

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, Is there any diffrence in speed in following example:

class B
{
    public:
        void DoIt() { // Do something. }
}

class A
{
    public:
        A() { myB = new B; }
        B* GetB()
        {
            return myB;
        }

    public:
        B *myB;
}

void main()
{
   A myA;
   myA.GetB()->DoIt();
   myA.myB->DoIt();
}


Which is faster? Granting direct access to a class member or creating a Get function? Is there any diffrence? Is it worth creating Get funtions to keep class members protected?

Share this post


Link to post
Share on other sites
Advertisement
For starters, why does it matter? How many billion times will you be calling it?

But to answer the question, the way you posted it, I think it would be identical. If, however, you seperated the implementation of the get function from the declaration, ie. put it in a source file, it would be slower. When you dump the function body in the class like you have, it is automatically inlined. So the code in the functions is sort of copied to where you use it if you will. If you seperate it out, you have to request for it to be inlined with the inline keyword.

Share this post


Link to post
Share on other sites
They should behave (preformance wise) identically. Declaring GetB as it is - within the class body - makes it implicitly inline (as in the keyword's properties applied to the function), which although quite possibly ignored by your compiler (it is a suggestion, not a mandiate), will be what happens anyways (the compiler can inline what it wants for the most part) - except maybe in Debug mode (in which case the function may truely be seperate so debugging information may be inserted - but compilers can do some inlining even then, AFAIK.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!