[solved] C++ direct access a class mber variable vs getValue

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
whisp
Posts: 17
Joined: Sat Jan 09, 2010 1:58 pm

[solved] C++ direct access a class mber variable vs getValue

Post by whisp »

Is there a difference in speed when accessing a classes member variable directly, compared to getting the value of the variable via a getValue method (that does nothing else than return value)?

If it would be an interpreted language i would say yes, due to the additional lines that had to be processed from the function call. But i have no clue how the compiler compiles such a method. So, again, is there a speed difference between accessing the value directly compared to a getValue function call?

Thanks for any answers!
Last edited by whisp on Sat Feb 05, 2011 7:15 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

When you compile with optimization the function-call will usually be removed, so there is no difference then. Without optimization the function-call itself will slow this down.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
whisp
Posts: 17
Joined: Sat Jan 09, 2010 1:58 pm

Post by whisp »

Thanks CuteAlien. That's good to know, even though the "usually" makes me a bit wary. Depending on the compiler? Or are there some special situations where no compiler would translate that method to direct access? I guess i have to read a bit about compilers some time.
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Optimization at this level is senseless. Modern compilers optimize better than most of us could do in years.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

I said usually because I also don't know if there are situations where it might keep the function. Well, it would do so for virtual functions, but those are special anyway. But aside from that I guess this is rather one of the most typcial cases and will likely be optimized away by every modern compiler.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
whisp
Posts: 17
Joined: Sat Jan 09, 2010 1:58 pm

Post by whisp »

Thanks. This makes the decission to implement a public getValue() method instead a public value variable for own classes an easier one, also for values that are needed very often.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

whisp wrote:Thanks. This makes the decission to implement a public getValue() method instead a public value variable for own classes an easier one, also for values that are needed very often.
Make it inline and const-correct:

Code: Select all

inline int getValue() const { return mValue; }
If you return other types than POD, you should return a constant reference.

Code: Select all

inline const Object& getObject() const { return mObject; }
"Whoops..."
Post Reply