2

My Question is about performance vs design. I have readed much about Getter and Setter in PHP. And the idea behind, is very good and usefull (Debugging, Sanitize).

So I started to do a Benchmark:

class Foo {
    public $bar;

    public function __construct($bar) {
        $this->bar = $bar;
    }

    public function getBar() {
        return $this->bar;
    }

    public function setBar($bar) {
        $this->bar = $bar;
    }
}

$foo = new Foo(42);
Debug::ProcessingTimeSinceLastCall();

//Without Setter and Getter
for ($i = 0; $i < 1000000; $i++) {
    if ($foo->bar === 42) {
        $foo->bar = 43;
    } else {
        $foo->bar = 42;
    }
}
Debug::ProcessingTimeSinceLastCall('No Setter and Getter');

//With Getter and Setter
for ($i = 0; $i < 1000000; $i++) {
    if ($foo->getBar() === 42) {
        $foo->setBar(43);
    } else {
        $foo->setBar(42);
    }
}
Debug::ProcessingTimeSinceLastCall('With Setter and Getter');

Result:

0.0000 Seconds (First call)
0.1428 Seconds (No Setter and Getter)
0.4366 Seconds (With Setter and Getter)

Setter and Getter takes 3 times more time. Is my Benchmark wrong?

I am in development of an big application, with thousands of those calls. Is it still a good practise to have getter and setter, if performance matters?

Neo
  • 6,753
  • 1
  • 19
  • 25
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • 3
    Every method call is a time overhead, so using getters and setters will always be slower than direct access to a public property.... but if performance is *that* critical then PHP isn't the language you should be using.... getters and setters may be an overhead, but they also have a purpose, such as ensuring that only valid datatypes or datavalues can be set in properties – Mark Baker May 20 '14 at 18:05
  • 2
    I would suggest reading: http://stackoverflow.com/questions/1568091/why-use-getters-and-setters – Neo May 20 '14 at 18:43

1 Answers1

7

Thousands of calls may not be relevant in the face of fetching data from a database and sending the response back out over the wire.

You talk about "thousands of calls". Your code can make one million calls to the setters/getters and only be slowed down 0.29 seconds. Do you have code that makes millions of calls, and that has a runtime such that you can notice a difference of 0.29 seconds?

Before you go optimizing, find out where the slow spots in your code are using a code profiler like XDebug: http://xdebug.org

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • 2
    +1 for profiling / tracing. Until you actually see the data about where your code is spending the most time you can't actually know where your slow areas or, or where you'll get the most benefit from changes. – STW May 20 '14 at 19:01
  • 2
    Thanks, you opened my eyes. – Christian Gollhardt May 20 '14 at 19:08
  • late but we can profile when our first codes are ready. At pre-design stage ,when we are going to make decisions to use `getter/setter` or not, there is no code even to profile. What could we do in these stages? – Seyfi Jul 06 '16 at 11:55
  • 1
    @Seyfi proper encapsulation should be used (including getter/setter methods). The data implementation should only be accessible by public methods. It can always be optimized later if necessary, but by my experience this is rarely required (exceptions can be things like game engines). Sorry for being late as well ;) – Katai Feb 20 '20 at 13:36