如若提升PHP程序的性能
如果提升PHP程序的性能
一代大神Hoare曾经说过:”过早的优化是一切不幸的根源。“在我是个小菜鸟的时候,根本不知道这句话是否正确,但是当自己成为一个老鸟之后,就发现这句话是相当的犀利,说的恰到好处,也不愧它能够流传这么广泛。
要想优化PHP的性能,第一点要注意的就是缓存,有人说过如果PHP使用了缓存,它的性能会提高五百倍,可惜Zend公司在致力于卖自己的产品,因此一个公司把一门语言给坑了,先不说这个公司的问题,就缓存的重要性而言,是绝不容忽视的。我们可以用缓存模块(比如Memcache)或者缓存模板(Smarty)等来进行一系列的缓存处理,当然我前面介绍的两个页面静态化和文件方式缓存数据库信息的方式也在这一类里面。
第二点就是内存的消耗问题,这点说起来还是比较大的,说几个细节把,就是有些人喜欢把变量赋值一份,从而增大内存开销,比如我们从GET接收过来的参数,直接用就可以了,非要再赋值给另外一个变量,导致在该变量的内存消耗翻倍,比如如下代码:
<?php //只是为了美观的新变量$msg = strip_tags($_GET['msg']);echo $msg;
echo strip_tags($_GET['msg']);
第四点就是在数据库的操作上,有些新手喜欢用到一次数据就查一次,其实我们每次连接数据库再断开连接的开销还是蛮大的,我们应该把要做的sql操作放到一起,统一执行,避免多次的数据库连接和断开。
第五点就是在include或者require文件的时候,使用绝对路径绝对要快很多,另外可以使用自动加载机制,即方便,又保证了效率,关于自动加载,可以看我的一篇博文,今天刚写的。
第六点就是在某些大块头的部分进行调优,很多人喜欢用smarty,如果是大型的网站,还需要考虑到smarty的开销,有人统计过,smarty会占据到百分之十的开销,这点还是非常可观的。
第七点就是使用单引号代替双引号来定义字符串会让速度稍微快一点,因为PHP会在双引号包围的字符串中搜寻变量,但是单引号不会。使用静态方法也提高速度,因为它避免了类的实例化,会节省开销。使用echo输出多个字符信息的时候,使用逗号而不是原点会加快速度,因为它省去了拼接字符串的开销。include文件的时候使用绝对路径也会减小开销,因为它避免了PHP去includ_path里面查找文件的操作,提升性能。
第八点就是对于简单的字符串,不要使用正则表达式,这样会加大开销,使用switch case语句要比多个if。。elseif要优化的多,因为它省去了多重判断的开销。对于变量的操作的时候,递增一个局部变量是最快的,递增一个全局变量要慢大约2倍,递增一个对象的属性大约会慢三倍。
第九点就是我们的静态化的一点原因了,因为Apache解析一个PHP脚本的速度比解析一个静态HTML页面会慢大约2倍到10倍左右,因此,这也是静态化的优点之一。在我们读取文件的时候,能用file_get_contents就使用它,而不是fopen,fread等操作。
最后一点就是foreach的效率比for和while效率高一点,是在想不起来什么其他的了,如果读者能够想到的话,希望补充奥,对了,还有apc这种缓存,其实我第一点说的就是缓存。。。

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
