search
HomeBackend DevelopmentPHP Tutorial设计模式和效率 哪个比较重要? 为什么?

昨天在GitHub给某开源PHP项目Pull了request
我把common库里的一个常用核心函数改为 先判断config里是否enabled
再决定load class 而不是load后 再在method内判断N次config是否enabled
我想这在效率上还是有很大区别的

提交后 Owner回复说 他明白我的出发点是效率 可是这个判断应该留给class去处理
好吧 我一想 对 可能这样"看着干净些" 但就因为这原因 就可以舍弃效率了?
那个函数单次请求至少会被调用15次 对我来说真不合理
我也没打算去争执 毕竟是别人的项目 但就是蛮纠结的

我对我自己的的代码质量感觉很良好 开始两次的PR都被owner merged了
上一次被close的原因和上述一样 请勿担心 谢谢。

回复内容:

在性能优化里有一句话,就是在性能没有明显表现出问题之前,不要进行优化,因为对性能的优化往往会影响代码的解耦和可重用性

至于题主的具体例子,就不作评论了,毕竟没看到具体的代码 你这是代码写少了。简洁设计的低耦合可扩展性远比一点点效率更重要。自己觉得什么不对就去优化,知道“Premature optimization is the root of all evil”这句话么?

顺便吐个槽:什么时候开始PHP还在乎这点执行效率了? 设计模式难道不是为了提高效率?(设计模式是长期程序工作者总结的一套提高开发效率与程序执行效率的编程方法,在这种情况下,应该根据项目需求与项目的具体实现进行更改!) 效率不是瓶颈的情况下,设计模式重要。 很明显的效率问题肯定是要提前就处理掉的,但是对于你说的例子这个config问题,我不赞同你的说法。
我说不上来具体原因,但是,在method内部调用config来做判断是对的。效率上有很大影响是不可能的。 你可能很少做性能测试。
这样的性能优化,在性能测试的报告里完全不可见,你这个优化相当于一个人为了减肥,而把鼻毛给剪了,不能说你没效果,但是确实没有意义,不值得为此付出的代价 设计模式更重要
设计模式是经验的总结
很多时候必然会走向设计模式
如果它不好只能说明你没理解用错了地方

而性能问题很多时候是想象的产物
脱离了实际场景出来的性能是无意义的

而且楼主的问题看来就是想当然的提前优化

按设计模式搭建出来的系统
大家都有能力去看懂去优化性能
按提前优化局部性能搭建出来的系统
连自己到后来都不想碰 code效率优先,运行效率在99%的环境下不是问题。可读性,可维护性一般优先度在运行效率之前。真有比较严重问题才去针对特定的1%到10%部分进行优化
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools