简体   繁体   English

在PHP中使用设计模式有什么性能影响?

[英]What are the performance implications of using design patterns in PHP?

I know that design-patterns are very useful in creating of big projects. 我知道设计模式在创建大项目时非常有用。 Does anyone have experience in both creating project with normal (OO, procedural ) and using design patterns in respect to performance(speed of execution)? 有没有人在使用普通(OO,程序)创建项目和在性能(执行速度)方面使用设计模式方面有经验? I want to create some big project and I am afraid that using design patterns my scripts would run slower. 我想创建一个大项目,我担心使用设计模式我的脚本会运行得更慢。 So What is pefrmance of code with desing paterns compare to normal OO programming? 那么与普通的OO编程相比,设计代码的代码是什么呢?

Greetings 问候

Personally, 亲身,

I'd program it, and then figure out if there are speed problems/bottlenecks. 我编程,然后弄清楚是否有速度问题/瓶颈。

Only worry about performance when it becomes a problem. 当它成为一个问题时,只担心性能。

The objective of design patterns is to solve common problems. 设计模式的目标是解决常见问题。 These problems may include maintainability (which is what it sounds like you're mostly interested in), as well as performance. 这些问题可能包括可维护性(这听起来你最感兴趣), 以及性能。

Personally, I would favour applying architectural patterns first to make the operations of your application easy to understand. 就个人而言,我倾向于首先应用架构模式,以使您的应用程序的操作易于理解。 It should then be easier to refactor the existing code to introduce some performance-related patterns, should the performance of your code warrant it. 如果代码的性能需要,那么重构现有代码以引入一些与性能相关的模式应该更容易。

Rarely code complexity is a factor in total execution speed of a PHP application, usually database interaction and network transfer times are much better candidates for slowness. 很少代码复杂性是PHP应用程序总执行速度的一个因素,通常数据库交互和网络传输时间是缓慢的更好选择。

In any case, develop first and optimize later. 无论如何,先开发并稍后优化。 You might found out that that kind of optimization is not even necessary. 您可能会发现甚至不需要这种优化。

Some of the reasons design patterns are used is to eliminate code duplication and placing the code in the right spot. 使用设计模式的一些原因是消除代码重复并将代码放在正确的位置。 Both of these factors adds up to a fast application. 这两个因素都可以加快应用速度。 Individual techniques such as caching and loading-code-only-when-needed could be easier/faster to apply with design patterns. 个别技术(如缓存和仅在需要时加载代码)可以更容易/更快地应用于设计模式。

That said, maintainability will be much more prominent with some kind of thinking behind the coding applied, which will let you as a programmer focus on the real performance issues. 也就是说,在应用编码背后的某种思维方式下,可维护性将更加突出,这将使您作为程序员专注于真正的性能问题。

An old adage about database design can be applied here: "Design for normalization first, denormalize for performance later". 关于数据库设计的一句古老谚语可以在这里应用:“首先设计规范化,以后再进行规范化”。 In this case, design it right the first time, if you find performance bottlenecks, optimize and break elements of your design on a one off basis then. 在这种情况下,如果您发现性能瓶颈,则首次设计正确,然后在一次性基础上优化和破坏设计元素。 Another issue, get someone using your application before you start optimizing, otherwise, who really cares? 另一个问题是,在开始优化之前让某人使用您的应用程序,否则,谁真正关心?

This depends entirely on what your application is doing and on the details of how you implement the patterns. 这完全取决于您的应用程序正在执行的操作以及您如何实现模式的详细信息。 Design patterns themselves are not really related to performance, but are intended to help you structure your system to aid maintainability. 设计模式本身与性能无关,但旨在帮助您构建系统以帮助维护。

Besides, performance should not be a concern at this stage; 此外,现阶段表现不应成为问题; focus on getting the design pinned down first, and if it turns out to be running too slowly, only then should you worry about optimization. 专注于首先将设计固定下来,如果结果运行得太慢,那么你应该担心优化问题。

Well I don't have experience with programming using design patterns (only MVC). 我没有使用设计模式(只有MVC)编程的经验。 It is useful for maintainability, but amount of classes/code is greater compare to OO programming. 它对于可维护性很有用,但与OO编程相比,类/代码的数量更多。 (that is conclusion from my research ,but not from experience). (这是我的研究得出的结论,但不是来自经验)。 By desing patterns I mean Factory,Decorator, Singleton etc. 通过设计模式,我的意思是工厂,装饰,单身等。

I don't want to be in a situation that my code is well maintained but run slowly. 我不希望处于我的代码维护良好但运行缓慢的情况。 Ex. 防爆。 Many ORM solutions are well maintained but much slower than simple query request coding. 许多ORM解决方案维护得很好,但比简单的查询请求编码慢得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM