1

I am building an application which does some heavy repetition of functions.

For instance:

$Class = new Class; for($i; $i < 1000; $i++) { $Class->get_something(); } 

I have read so many articles on OOP and why it is slow, but I still don't understand where exactly the "slow" part is. People keep using the world "overhead," is that loading the actual class? Once I require a class, is it now the same speed when I call function or a variable?

14
  • 4
    OOP as such is not "slow". That's rubbish. Programmers create slow programs. Algorithms might be slow. Commented May 2, 2013 at 17:30
  • 2
    What kind of profiling/benchmarking have you done to narrow down the slow part of your application to something like this? Commented May 2, 2013 at 17:31
  • 1
    If you are considered with performance just go native> Commented May 2, 2013 at 17:34
  • 1
    There are contexts where all kinds of indirection (including, but not limited to, method calls) can have a noticeable impact. However, this is not at all relevant, for more reasons that I can enumerate here (two examples: you wouldn't be using PHP, and unless you already optimized the shit out of a million other things you won't even be able to measure any difference). Ignore anyone who just says "OOP is slow" or something along those lines. Sometimes people say very stupid things. Commented May 2, 2013 at 17:36
  • 1
    I think the unanimous answer here is that OOP is not slow. Commented May 2, 2013 at 17:37

4 Answers 4

2

You are touching the very old debate between making a one large query to get your data, or looping over many smaller ones to receive them. And the answer lies in specifics of implementations. In some cases it is faster to call that one function over and over, while other times it will just kill your performance. The "overhead" from just calling a function over and over is pretty minimal, it's the "guts" of it that matter.

Sign up to request clarification or add additional context in comments.

11 Comments

I still don't get it. Can you explain what "overhead" is, that's where my confusion lies. What is "guts"?
en.wikipedia.org/wiki/Overhead_(computing) And guts is simple the implementation of said function, what's inside it.
I've read that Puciek, I'm here because I don't understand it.
Then maybe you should make it clear what you do not understand?
Ask the person who claims the "slow" not an actual OO enthusiast? :) On serious note - it comes from all those little pieces combining, its a half o millisecond there, another half there and we are already millisecond behind.
|
1

One thing that can be said is that you shouldn't use loads of useless getters in PHP, since it is true that it can slow down your code. Why don't you do a benchmark yourself, eg :

<?php class test1{ private $prop = 1; public function get_prop(){ return $this->prop; } } class test2{ public $prop = 1; } $t1 = microtime(true); $obj1 = new test1(); for($i=0;$i<=1000000;$i++){ $a = $obj1->get_prop(); } echo 'test1, access with get_prop : '.(microtime(true) - $t1).'ms<br/>'; $t1 = microtime(true); $obj2 = new test2(); for($i=0;$i<=1000000;$i++){ $a = $obj2->prop; } echo 'test2, direct access to prop : '.(microtime(true) - $t1).'ms'; 

This outputs :

test1, access with get_prop : 1.7691290378571ms

test2, direct access to prop : 0.38315200805664ms

More than 4 times slower!

1 Comment

so we're talking milliseconds here. I will look over my code again.
1

It's the function call overhead, but it's negligible under normal circumstances. This applies for functions too, not just class methods. The overhead difference between class methods and functions is even more negligible.

It makes sense to avoid moving that code within a function and use inline code if you're going to run it thousands of times. Otherwise, you won't notice any performance impact.

Don't do this unless you really need to, because you'll end up with badly organized code

2 Comments

this is what I was looking for thank you. So functions are slow to call if it's 1000's.
Even with thousands it's barely noticeable with the current CPUs. You should run a profiler like XDebug, and analyze the generated cachegrind file. There you'll see what parts of the code slow your app down
0

Other resource related to this topic. Static method seems much slower.

Performance of static methods vs. functions

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.