Archived

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

Get function speed issues...

This topic is 5131 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
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
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
quote:
Original post by Drythe
what is ''inlined''?

An inline function just means that the actual function code is "copy and pasted" every time the function is called, instead of having a function call set up.

It executes faster, but takes up more space in the cache.

Also, no one else has pointed this out, but your GetNumParts() function actually is inlined. By including the function body inside the class (not just the declaration) you are instructing the compiler to make it inline. Actually, it''s more of a suggestion than an instruction, as the compiler is free to ignore it, but generally it will be inlined in release builds.

Share this post


Link to post
Share on other sites
If you don''t do anything in both the get and set function then just make that variable public.

--------
"Hey, what about those baggy pants you kids are wearin'' these days? Aren''t they hip and cool and poppin'' fresh?"-Peter Griffin
"Everytime I see an M followed by a dollar sign I just say "oh god" and go to the next post."-Neurokaotix
the D programming language

Share this post


Link to post
Share on other sites
Your professor is wrong, over use of get and set is as much a violation of data encapsualtion as public members. Classes should encapsulate both data and function, if this advice is followed you will find there is little need for multiple get and set methods.

Wizza Wuzza?

Share this post


Link to post
Share on other sites
quote:
Original post by brassfish89
If you don''t do anything in both the get and set function then just make that variable public.


What if he needs to make the get and set function do something later on?

Or what if he chooses to store the parts differently (perhaps to optimize the program) and has to do some complicated math to find the number of parts? It''s not likely but it could happen.

Also, using get and set functions help avoid errors like this:

CProp prop;

if (prop.GetNumParts() = 25)
OmfgError();

Share this post


Link to post
Share on other sites