Getter Setter & Performance [new]

Jul 4, 2014 at 4:37pm
I've created already one topic (2months ago~)
You'll find my problem description in this link:
http://www.cplusplus.com/forum/general/131696/


I've tried now the following code:

void MyClass::setObject(int &i)
{
this->i = i;
}

using setter with reference has no real impact on performance, when using simple types/parameter (in this case int)

but when I use a vector with 10++ items, as parameter using reference for setter is twice as fast as without reference/pointer (tested it)

Using getter with reference:

int& MyClass::getObject()
{
return i;
}

or better:

const int& MyClass::getObject()
{
return i;
}

also has no impact using simple types,
much better performance when i use it for vectors or other "big things".


is even a better and faster way?
let me know your thoughts about this.


~slei


Jul 4, 2014 at 4:46pm
In general, getters and setters are bad anyway... so this is kind of a moot point. If you are writing a bunch of getters/setters for your classes, then you are probably not designing your classes properly.
Jul 4, 2014 at 10:42pm
i tried the same test in java and somehow the java version is faster than my c++ version ._.
Jul 4, 2014 at 11:00pm
Try turning compiler optimizations on ;) (Build in Release mode)
Last edited on Jul 4, 2014 at 11:00pm
Jul 6, 2014 at 7:53am
i built it in release mode
Topic archived. No new replies allowed.