🔒 Locked
Get function speed issues...
Advertisement
Original Post
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
First I''d check that you''re profiling a release build and not a debug build...
They are inlined right?
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''?
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''?
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 :
can become this after its compiled:
[edited by - _the_phantom_ on November 30, 2003 10:19:00 AM]
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]
Never, ever, ever try and profile something (especially something this minor) in a debug build, you won''t get the results you expect.
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
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.
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
--------
"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
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?
Wizza Wuzza?
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();
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement