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!
[solved] C++ direct access a class mber variable vs getValue
[solved] C++ direct access a class mber variable vs getValue
Last edited by whisp on Sat Feb 05, 2011 7:15 pm, edited 1 time in total.
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- Posts: 1186
- Joined: Fri Dec 29, 2006 12:04 am
Make it inline and const-correct: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.
Code: Select all
inline int getValue() const { return mValue; }
Code: Select all
inline const Object& getObject() const { return mObject; }
"Whoops..."