“性能影响”当使用 20K 行单类时

发布于 2024-10-09 05:34:34 字数 217 浏览 0 评论 0原文

这个问题之前曾在此处提出过,但没有一个答案真正尝试过回答实际提出的问题,所以我以不同的方式提出。加载包含 20,000 行和 100 个函数的单个类是否比将代码分解为每个函数较少的较小类并根据需要加载这些较小的类更加消耗资源?

This question was asked here before, but none of the answers really tried to answer the actual question asked, so I'm asking it in a different way. Is loading a single class of 20,000 lines with 100s of functions more resource intensive in any way than breaking up the code to smaller classes with fewer functions each and loading these smaller classes as needed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-10-16 05:34:34

脚本或类越大,每个实例使用的内存就越多。 PHP 没有现成的方法来共享库和类的内存空间,因此为网站创建大量脚本并不是一个好主意。

典型的方法应该是将类分解为块,这样您只需在每个脚本中包含运行该脚本实际需要的内容。

此外,除非您有大量流量,否则它不太可能导致性能问题 - 然后您可能比重构类更容易解决问题。

加载脚本时,需要固定数量的内存来解析它。它越大,需要的内存就越多。接下来,执行脚本本身,运行任何顶级代码(不在
类或全局函数)。如果其中包含任何 require/include 语句,则会加载这些脚本(如有必要)。如果它创建对象,则会占用更多内存。

但是,该类的每个实例的大小仅受其存储的数据的影响。抛开这个修正不谈,这里的建议是正确的:根据职责划分你的班级。其原因还与开发的便利性而非性能有关。假设您有一个充满静态方法的怪物类。如果您的应用程序对每个请求使用大部分这些方法,则拆分它不会有任何性能优势,因为无论如何两个脚本最终都会被加载。但是,如果您可以将这些方法分组为逻辑子系统,它们将更易于理解和使用。

The larger a script or class is, the more memory this uses per instance. Out of the box, PHP doesn't have a way to share the memory space of libraries and classes, so creating massive scripts for a website is not a great idea.

The typical approach should be to break classes down into blocks such that you need only include per script what you actually need to run that script.

Also, it's unlikely to cause you performance problems unless you've got a huge amount of traffic - and then you could probably fix your problems easier than refactoring classes.

When a script is loaded, it requires a fixed amount of memory to parse it. The larger it is, the more memory it requires. Next, the script itself is executed, running any top-level code (not in a
class or global function). If that includes any require/include statements, those scripts are loaded (if necessary). If it creates objects, that takes more memory.

However, the size of each instance of the class is affected only by the data it stores. This correction aside, the advice here is spot on: split your classes based on responsibilities. The reason for this has also to do with ease of development than performance. Say you have one monster class filled with static methods. If your application uses most of those methods for each request, splitting it will have no performance benefit because both scripts will end up being loaded anyway. But if you can group the methods into logical subsystems, they will be easier to understand and work with.

甜扑 2024-10-16 05:34:34

一大类需要一个周期才能编译成二进制代码(操作码)。

许多较小的类使用较少的内存但需要更多的编译,并且编译所使用的内存将会累积。

实际上取决于运行时中包含了多少个类/文件。

因此,解决方案是分成多个类并使用 APC< /a> 或同等内容。

PS:大文件的内存消耗要小得多,因为 PHP 不需要将源代码重新编译为操作码(如果您不愿意将大类分解为更小的类)

One big class require single cycle to compile into binary code (op-code).

Many smaller classes using lesser memory but require more compilation, and the memory use for compilation will be accumulated.

Is really depend how many classes/files has been included in run-time.

So, solution for this, break into multiple classes and use APC or equivalent.

PS: the memory consumption for the big file is much smaller, because PHP do not need to re-compile the source into op-code (if you reluctant to break the big class into smaller)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文