What is the overhead of using object orientation?

Asked

Viewed 838 times

41

In the company where I work today we are strongly encouraged to avoid object-oriented programming for both old and new projects.

For old projects I agree that it is a bad practice to start inserting object orientation in a totally structural project, but for new projects I believe it is more productive to use OOP.

Questioning the situation I received as a response from the company: object orientation is heavy and slow, so we recommend not to use.

1 - What is the overhead to use object orientation vs procedural programming?

2 - Considering that good programming practices are followed, it is more productive to use object orientation or procedural programming?

  • orientação a objetos é pesada e lenta, portanto recomendamos não utilizar. This looks like the mantra of Rasmus. How you talk depends on the project.

  • I understand that using or not depends on design, but excluding guidance in 100% of projects seems a little radical. There must be some reason for this kind of attitude.

  • "PHP is heavy and slow, so we recommend not to use it" - Myself. I have no data but I doubt that using objects is much worse than not using in PHP. It is not a criticism of PHP but to have the advantages it has it has to structure the memory in a way that makes everything slower. If I can find a consistent way to respond, I’ll post something.

  • @Kaminary, there’s a phrase/benchmark from Rasmus that he swears that php procedural is faster than OO, so some people think... hum Rasmus is the creator of the language knows what he’s talking about so it’s an 'authority' or is <irony>we should believe it blindly! </irony>

  • 4

    And I agree that procedural is faster anyway, there is some reason to be, there is reason even in static languages. But the difference will be very small in a language that does not feature to be fast (which is not, in itself, a defect). But that the.

  • @bigown I think the original source is here: https://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html . Ironically, there is a php error on the xD page

  • I’ve lost something or can’t access any relevant information on it?

  • @gmsantos, would that be the link from the famous benchmark http://talks.php.net/show/froscon08/24 ? yes it is old. The following slides are the comparisons with the other frameworks.

  • @rray as soon as you find the quote create an answer based on it.

  • 7

    If the question is the "raw" performance of the language, then why not use C or Assembly instead of PHP? If the question is the response time, then just invest in a good architecture in the application, hardware and database, and use mechanisms cache wherever possible. I believe that all major PHP frameworks are object-oriented, so the statement is simply not pragmatically justified.

  • 1

    As a friend of mine would say: Object orientation can "spend" more memory than procedural guidance, but you will agree that in the long run, according to what you develop with procedural, you will end up consuming more resources, because of repetitions and etc?

  • If object orientation is that heavy, they should end ZEND! Perhaps to think that object-oriented programming is too heavy would be to think too ridiculously about microtimization. It’s like that windows user defragmenting your computer every 2 hours :)

  • 2

    From what I noticed in PHP classes can even save memory. Of course I do not know the context your friend made such a statement but in this context here she is wrong. Even in C++ you only spend more memory if you use polymorphism, and even then it’s minimal is just the space of a pointer. Who knows how to program well procedurally reuses code equal or almost equal to OOP. It may be more complicated to organize, but it is possible. I do not know a case of well done OOP that consumes less resources than procedural well done. In poorly done code may be but there the comparison is unfair.

  • 3

    As I have also experienced this on my own, please allow me a moment of vent. In good, this type of dogmatism (XML is very "heavy", OOP has a lot of "overhead", etc.) is usually used by those who know nothing of the technology in question and are afraid/ashamed to admit it. Even worse, you’re too lazy to learn. Okay, it’s over.

  • The company is wrong, the problem now is influencing them to change their mind.

  • 1

    There are many good reasons not to use OOP for nothing in PHP, but not exactly what the company claims. One thing is a fact: OOP does not add anything in PHP, but if it is something with enough reuse (a library, for example) there is no reason not to use. If you are going to make the application router, for example, the procedural is much shorter and readable. The language allows the two paradigms, how about using what is good in each one and at the right time? Purists are terrified of these "mixes", but if you(s) are(don’t) using your brains to program, you have no problem using the tools.

Show 11 more comments

2 answers

29


I haven’t programmed in PHP for a long time and even at the time I never went into it. I got into it Internals of language because I like languages. Still I can’t remember all the details of the implementation and at the time had no object orientation in the language, or at least it was incipient.

Overhead of PHP

I know all those languages said script or at least dynamic in their essence have one thing in common: they spin on top of a vitual machine.

PHP is basically a interpreted language, at least today. Even though she has a way of optimizing it, the source code is eventually transformed into a bytecode to improve the performance of the virtual machine, it will still be slow, not only because the compilation process has a cost and that must be paid every time the application runs from scratch (and this occurs much in most cases of use of PHP), but also the virtual machine is software (in general it is a loop with a huge switch to select which micro-instruction should be executed at that time) which generates a beautiful of a overhead. A pre-compilation and a Jitter would do a little better work (although for a script not so much).

Also dynamic typing languages need to do checks and possibly conversions every time you access a value. Although you can do some optimizations there is a overhead clear in each access to memory.

I don’t remember the case of PHP but almost all of these languages store the variables in a hash (I can’t imagine how PHP can be very different, other similar languages can even, but PHP would not have without imposing some limitations that today it does not have). Accessing a structure that needs to do a calculation is obviously slower than accessing a direct memory address as is the case with other languages that have "real variables".

There are several other "advantages" of the dynamism of PHP that imposes more overhead.

To assemble the classes there is very likely more overhead. But it’s just one more within a context full of overheads. Maybe it’s not such a big evil compared to everything that happens in the execution.

You know how these classes are usually assembled into languages?

Well, in static languages it is possible to mount objects like structures simple data, almost like a array simple (note that the array PHP is different, it is a complex and slow structure depending on the flexibility it offers). There is a ovehead if use polymorphism, in this case there will be a pointer indirect to indicate what is the real type of a value. This is fast but is still a overhead.

This cost exists in all values in dynamic languages. Any given in dynamic language is polymorphic regardless of whether it is a class or not. And it’s so flexible that it’s not enough to have an indirect pointer, it’s necessary to have a slightly more complex structure and there’s no guarantee of data integrity for every situation requiring verification on every access.

In dynamic languages classes are often a very similar form to a array associative, in general implemented as hashs. In the background you have elements that store data and some of this data are references to functions. Methods are nothing more than "garnished" functions with a surname and an additional parameter to access the this.

Classes are in hashes, common variables as well. So unless you have something I don’t know, it’s not that different to access a normal variable or a class instance variable. Same functions, in the background are also in tables hash. Even functions written in C need to figure out their address by these tables before they are called.

OOP has overhead?

So if someone doesn’t show me that there is a huge difference, I say there is little. But there is.

Of course it’s an extra layer. And it imposes some overhead. OOP usually organize and abstract more code but imposes extra internal code that produces overhead. The simple fact that you have to upload multiple files is already one overhead important.

In many cases the methods only exist to call another procedural function. There are cases that methods need to be "interpreted" even if they are not actually used.

And remember that many procedural functions are written in C and the PHP code does not need to do anything. When you add a layer, its code needs to be interpreted. Even worse if you make all the logic in the method and not just delegate to another function that alone would be a much more costly indirect than an indirect pointer.

In theory accessing members in a class should not be much different from accessing members in a array associative. But the research I’m conducting to properly answer this question shows that it may not be quite so. That is why it is useless to theorize, it is necessary to measure to have reliable information. And it is necessary to know how to measure correctly.

I am unable to make my measurements now and will have to rely on some existing on the internet. It doesn’t mean that they’re all right, because they’re conflicting, I couldn’t make an assessment if they’re right.

Note that there are differences depending on the PHP version. This makes sense, so you can’t tell if OOP in PHP imposes a overhead and does impose on the version you are using.

We also see that overhead memory and processing can give different, obvious results.

Recalling that direct competition from Hack is making PHP chase the damage.

  • Question in the OS with tests that show it is almost equal or even classes can be faster.

    It makes sense for classes alone, without looking at their actual application of all cases, to be faster because they can use an implementation of hash optimized for the need for a structure that is known to be used only to store class structure.

  • In-depth testing.

    In fact all these tests are not accurate they compare not only the overhead processing using OOP, there are other processing taking place that makes the difference seem smaller than it really is if we consider only the extra expense to access the data and methods in a class.

  • slides comparing implementations of frameworks (provided by rray in comment)

    We see there are several other factors that interfere with performance. No news. Most comparisons measure implementations, not the raw resource you want to measure for truth. Without measuring the specific case it is complicated to say something. And staying to measure everything doesn’t make sense. Only worth measuring if you realize there are problems.

  • Explanation of minor memory usage.

    In PHP it seems that classes actually use less memory than arrays if this is the swap option. But I don’t know if this is true if the comparison is not with arrays.

  • Another comparison showing which classes can be more efficient.

    Note that all comparisons are contextual. No proof anything in all contexts, even considering that they have no flaws.

  • Comparison with results different from the others.

    Within the criteria used (not shown) objects occupied more memory and much longer time to load the data.

All this information has been placed to show that nothing definitive can be stated.

Which to use?

Use the one that organizes your code best.

Now some may be thinking that OOP is the way. And it’s not, not necessarily. I see many OOP codes that are much worse than procedural codes, because programming OOP is not that simple. It provides abstractions, but most people have problems with abstractions (so they shouldn’t even try to program that it’s all something based on abstractions, but this is another problem).

People always choose one or the other to solve all the problems because they usually have only one hammer and see all problems as nails.

inserir a descrição da imagem aqui

In PHP I tried to always do procedural until OOP is the best solution independent of having overhead or not (unless the overhad kill the advantage, which is rare). In fact almost everyone does much more procedural. Many even understand the procedural dividing line of OOP. Just because you’ve created a class doesn’t mean you’re actually doing OOP. And almost every language they say is 100% OOP is lying.

Want performance? Use static, compiled, low-level languages. C, C++, Delphi, C# especially Native or Assembly, for example. Worrying about performance using PHP doesn’t make sense.

Most optimizations will come by decisions about algorithms and architecture.

When using true good practice, we use what is best for the case. It is good practice not to blindly follow good practice. And this is mostly bad practice practiced by programmers. Information is silver, understanding is gold.

Final note

Learn to do it in various ways and avoid the golden hammer.

If you want reliable information, learn how to get it yourself. If you don’t know how to do it correctly, you won’t get anything reliable. Mostly it won’t get from random people on the internet, like me.

And the creator of language is a random person on the Internet. Especially in this case where the original creator created a whole crooked language, and himself You admit you didn’t know what you were doing. Worse than other people took on the development of language and worse mistakes continued to be made. It seems that PHP cannot attract professionals who really know what they are doing.

  • 3

    "I see many OOP code much worse than procedural codes" is a very correct statement, especially if you take a look at the source code of Codeigniter :(

  • 3

    "It seems that PHP cannot attract professionals who really know what they are doing." Do you mean language development or developers who use the language? I think it can fit the first case more in the tone of irony, because the language has several deficiencies, but well or badly it is a "success", already if it is the second case, in my opinion it would be a statement too widespread and superficial, you can use PHP on a specific occasion very aware of what you are doing.

  • 2

    This something more like joke, but I would say that both :D Of course there are exceptions, of course there are good people developing the language (not equal to the best "computer linguists") and developing c/the language. But exceptions only justify the rule, they do not :P But every day I become more convinced that this is more or less widespread in almost all languages. Some the ratio changes. I’ve seen bad programmer in Assembly but it’s rare. The same goes for p/ C/C++, after the guy leaves college if he ñ gets good, he stops c/ the language, PHP, JS, Java, C#, etc. q ñ knows keeps programming.

  • 3

    I program in php and what I can say about the language is that it really has a lot of overhead on top of each other, but object orientation doesn’t have a big differential so far in the matter of overhead(it’s like waiting 1 second longer when you were already waiting 1 hour). However, with today’s machines, php has become a very good language for professionals in the field (except for large projects) you don’t feel any difference between it and other languages in small and medium-sized projects. So in these cases it is preferable to use a language that is easy to learn and fast to develop, i.e., PHP

0

In my view, saying not to use OOP because "it consumes more memory and is slower", is the same as:

Do not use bootstrap.css because it uses more memory and is slower;

Do not use jquery.js because it uses more memory and is slower;

Do not use angular.js because it uses more memory and is slower;

Don’t use an IDE because it uses more memory and is slower, and encode everything in the notepad.

Everything has a price to pay, then it is necessary to consider where the balance leans most, whether to the pros or whether to the cons side

  • This does not answer the question. When you have reputation enough, you’ll be able to leave comments on any post but until then, write only answer that no depend on more information of who asked. - Of Revision

  • But there’s a context there, what I said is that OOP can be considered an extra layer. And that yes, it can even consume a little more memory and be a little slower, has its price but also has its benefit, also the technologies I mentioned. He asked two questions, does not technically answer one, but I still consider that answers two, with an example of argument to use.

  • There are two problems in my view in his answer, first: The question had already been answered, digging questions to give an answer is not wrong, but his answer added nothing to what the bigown had already answered above :) I believe that in this case it would fit more as a comment, an addendum. Understand that I am not criticizing, just advising, I hope you do not feel angry.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.