Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Drythe

Get function speed issues...

This topic is 5341 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

I have a class called CProp and have wired it with ''Get'' functions for all of the data members (all of the data members are protected). I don''t see a whole lot of advantage to doing this... but my programming teachers always said to do this. anyway... I have a ''for'' loop in my program that is looping through many times and I noticed that the ''get'' functions seem to be killing the frame rate. To test it out, I put the variables I was using from the class into public and accessed them directly with no ''get'' function. The frame rate improved like 90%. I really don''t understand this... do simple variable return functions take 10x the operations or something? I hope I''m missing something here

Share this post


Link to post
Share on other sites
Advertisement
here''s what the class def looks like:

class CProp
{
public:
CProp();
virtual ~CProp();

int GetNumParts() { return numParts; }

protected:

int numParts;

};


is there a lot of overhead with a class function compared to just accessing a public variable? I''m getting a huge speed decrease when calling GetNumParts() many times in a ''for'' loop rather that numParts (if I put it in public).

what is ''inlined''?

Share this post


Link to post
Share on other sites
OrangyTang: Interesting... I tried doing a release build and now I''m getting the same frame rate with both (no decrease in speed). Why do the 2 ways differ so much in debug?

Share this post


Link to post
Share on other sites
in debug mode the compiler puts in loads of extra checks and calls the function as a function which takes alot longer.

With release build a compiler will try to inline functions which are small enuff/defined in the header/have the inline keyword, which instead of call it as a function just runs the code directly, so assuming we have an array of CProp classes this :

for (int i = 0;i<MAX_PROPS;++i)
somevar +=propsarray->GetNumParts()

can become this after its compiled:

for (int i = 0;i<MAX_PROPS;++i)
somevar +=propsarray[i]->numParts;



[edited by - _the_phantom_ on November 30, 2003 10:19:00 AM]

Share this post


Link to post
Share on other sites
Never, ever, ever try and profile something (especially something this minor) in a debug build, you won''t get the results you expect.

Share this post


Link to post
Share on other sites
quote:
Original post by Drythe
here''s what the class def looks like:

class CProp
{
public:
CProp();
virtual ~CProp();

int GetNumParts() { return numParts; }

protected:

int numParts;

};


is there a lot of overhead with a class function compared to just accessing a public variable? I''m getting a huge speed decrease when calling GetNumParts() many times in a ''for'' loop rather that numParts (if I put it in public).

what is ''inlined''?


You should also make the int GetNumParts() function const, since it doesn''t modify any of the variables. This helps the compiler realise that it could inline the function. Not such a big problem here but with more complex functions it can be.

James

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
marking a function const doesn''t affect the result in any way.

Its usefulness is in catching YOUR errors, and doesn''t assist in any kind of optimization.

Share this post


Link to post
Share on other sites

  • 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!