设计模式和效率 哪个比较重要? 为什么?
昨天在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%部分进行优化

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

While installing a new version of an application, Windows may display this error message "An error occurred while parsing C:\\Windows\Microsoft.Net\Framework\v2.0.50727\Config\machine.configParser returned error 0xC00CE556". This problem also occurs when your system boots. No matter what situation you encounter this problem, .NETFramework is the real culprit behind the scenes. There are some very simple fixes you can use to stop this error code from appearing again. Fix 1 – Replace corrupted files You can easily replace corrupted ma from the original directory

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

With the continuous development of the PHP language, ThinkPHP, which is widely used in the PHP back-end framework, is also constantly improving. As business scenarios become increasingly complex, the demand for configuration management in ThinkPHP is also increasing. In this context, ThinkPHP provides rich configuration management functions. Today we will introduce how to implement configuration management through ThinkPHPConfig. 1. Introduction to ThinkPHPConfig ThinkPHPConfig is Thin

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? In Vue development, we often use the v-bind instruction to dynamically bind class and style, but sometimes we may encounter some problems, such as being unable to correctly use v-bind to bind class and style. In this article, I will explain the cause of this problem and provide you with a solution. First, let’s understand the v-bind directive. v-bind is used to bind V

1. Introduction to CodeIgniter CodeIgniter is a lightweight and comprehensive PHP development framework designed to provide web developers with fast and powerful tools to build web applications. It is an open source framework that uses the MVC architecture pattern to achieve rapid development and basic functions, while supporting a variety of databases. 2. Introduction to the Config library The Config library is a component in the CodeIgniter framework and is used to configure and manage code. The Config library contains many
