Change all code to object oriented memory usage more or less?

There is a project written in PHP that is just just procedural ... calls the DB functions, process and print the output step by step. And then it changes to be fully object oriented - there is a singleton for the App object, and various functions are called through the App object.

Someone argues that the memory usage or footprint on the server will be less. Will it be so? I thought that procedural often just uses a minimum minimum level, while object-oriented programming with different design patterns usually creates more things than it needs or just has objects, while procedural ones usually have a minimum minimum size.

So, will change all the code to object-oriented, actually reduce the memory usage on the server?

+1


a source to share


7 replies


It will probably be more, but there is nothing to say. If your code is really improving by doing it in an OOP way, then it might be less. There is no direct correlation between used memory and having object oriented. It may be that OO memory takes up more memory overall, but only if both sets of code are written equally well, and this is almost never the case.



Is there any reason this app is being updated for objective orientation? You know it's not one or the other, you can mix and match items ... OOP is not a silver bullet.

+8


a source


Unfortunately I did my tests too. I tested the speed and it's about the same, but when testing the memory usage getting memory_get_usage () in PHP, I saw a bigger number on the OOP side.

116,576 bytes for OOP to 18,856 bytes for procedural. I know the hardware is cheap, but come on! Increased usage by 1000%? Sorry, this is not optimal. And with so many users hitting your site all at once, I'm pretty sure your RAM will simply burn out or run out. I'm wrong?

Basically, what I hear from all my OOP fans is that ... you will use more resources, it will be as fast as written procedural function calls, but it will be better for large projects and multiuser applications, developer environment. A balance must be found.

Other developers (sloppy devs) and a larger site

Con: more RAM used for your application.

Pro: Easier to maintain code among many in the whole application.

One developer with a simple website



Con: If your site grows or starts to include many developers, development might be a little slower if your procedural code sucks.

Pro: Low RAM, slightly faster speed. If your code is written correctly (and only good developers can do that - haha), your code will be just as easy to maintain.

In RAM wars, procedure wins. Good code wins in maintainability wars.;)

OOP fans say OOP is cleaner. I saw some really messy OOP code and then I saw some REALLY CLEAN procedural code written by developers who could write great code in any language or style. Code that was a pleasure to work with. The bottom line is that if you have sloppy developers, no matter what style you use, you will end up with sloppy code.

Due to my own personal tests, I am ditching OOP with memory, mainly because I do write purely procedural and I am generally the only developer on any of my projects.

Hooray!

+5


a source


Your question sounds like this to me: we have an irreplaceable codebase, would it be faster to rewrite it in Java?

First, you need to identify the problem: is your code unreadable and / or difficult to maintain, or is your runtime too large? These are two problems completely .

If you have a codebase that is difficult to maintain (it sounds like you), you won't solve it by rewriting your code with a different paradigm. Why isn't the code readable? Whether developers develop unreadable code and then rewrite it in Java or C #, they just give you object oriented unreadable code. In this case, you need to improve the skills of your developers (hire the best, re-educate existing ones, etc.).

If you have a memory issue, you should approach it like any other performance issue. Measure first , then do some optimization (rewrite some of the code to make it more memory efficient) and then measure again . Measuring before and after is important to make sure you don't make things worse. And trust me, even very good coders make this mistake.

+3


a source


The answer is yes and no.

By moving to a more object-oriented architecture, you can reduce the amount of memory, but only if and only if the code is more efficient.

Straightforward PHP without any function or object calls is pretty fast. Once you start making function calls or object calls, things start to go a lot slower.

From Python Vs Ruby Vs. Python Benchmark 's OO strategy to create an incremental loop took 3.7079 seconds, the function was targeting 4.3501 seconds, and usage did not take 0.6164 seconds. Running a Simple "Hello World" Application with OO and Procedural Vs. Neither did you have 4.1248 seconds versus 3.7656 versus 0.9309 seconds.

So, depending on what your code is doing and how it does it, objects can be either faster or slower, more memory intensive or less memory intensive, or with none of these.

+1


a source


And then it changes completely object oriented - there is a singleton for the App object and various functions are called through the application object.

It really looks like the opposite of "fully object oriented". This indicates a level of understanding of what "object-oriented" means, which most likely will not result in a memory footprint.

Overall, design will have a much larger impact on memory footprint than the programming paradigm, although it is probably true that, on average, OO applications have a larger footprint.

+1


a source


Memory usage is not an advantage of OO. The advantage of OO is that the code will become more maintainable. If you don't have performance issues, you don't need to look for ways to be more efficient.

+1


a source


In addition to other comments, I would like to say that OO helps reduce redundancy ... which can make your code more efficient and reduce memory footprint. Of course, OO has more overhead, but it should work more efficiently on large projects.

+1


a source







All Articles