will changing all code to object oriented make mem

2019-02-11 07:15发布

There is a project written in PHP that is just simply all procedural ... step by step calling DB functions, processing, and printing out the output. And then it is changed to totally object oriented -- there is a singleton for the App object, and various functions are invoked through the App object.

Someone claims that the memory usage or footprint on the server will be less. Would that be the case? I thought procedural often just uses the bare minimal, while Object oriented programming with various design patterns usually instantiates more things than needed, or simply have objects around while procedural usually just have the bare minimal.

So will changing all code to object oriented actually make the memory usage on the server smaller?

标签: php oop
7条回答
你好瞎i
2楼-- · 2019-02-11 07:42

The answer is both yes and no.

By switching to a more object oriented architecture you could reduce the memory footprint, but if and only if the code is more efficient.

Straight PHP without any function calls or object calls is rather quick. As soon as you start making function calls or object calls things start to go much slower.

From A Python Vs Ruby Vs. Python Benchmark an OO strategy for doing an incremental loop took 3.7079 seconds, function oriented took 4.3501 seconds, and using neither took 0.6164 seconds. Doing a simple "Hello World" app with OO vs. Procedural Vs. Neither yeilded a 4.1248 seconds vs. 3.7656 vs 0.9309 seconds.

So depending on what your code is doing and how it's doing it, objects could be both faster or slower, more memory intensive or less memory intensive, or neither.

查看更多
登录 后发表回答