Home Backend Development PHP Tutorial 如若提升PHP程序的性能

如若提升PHP程序的性能

Jun 13, 2016 pm 12:02 PM
get msg nbsp php smarty

如果提升PHP程序的性能

    一代大神Hoare曾经说过:”过早的优化是一切不幸的根源。“在我是个小菜鸟的时候,根本不知道这句话是否正确,但是当自己成为一个老鸟之后,就发现这句话是相当的犀利,说的恰到好处,也不愧它能够流传这么广泛。

     要想优化PHP的性能,第一点要注意的就是缓存,有人说过如果PHP使用了缓存,它的性能会提高五百倍,可惜Zend公司在致力于卖自己的产品,因此一个公司把一门语言给坑了,先不说这个公司的问题,就缓存的重要性而言,是绝不容忽视的。我们可以用缓存模块(比如Memcache)或者缓存模板(Smarty)等来进行一系列的缓存处理,当然我前面介绍的两个页面静态化和文件方式缓存数据库信息的方式也在这一类里面。

     第二点就是内存的消耗问题,这点说起来还是比较大的,说几个细节把,就是有些人喜欢把变量赋值一份,从而增大内存开销,比如我们从GET接收过来的参数,直接用就可以了,非要再赋值给另外一个变量,导致在该变量的内存消耗翻倍,比如如下代码:

<?php //只是为了美观的新变量$msg = strip_tags($_GET[&#39;msg&#39;]);echo $msg;
Copy after login
其实直接用如下代码就可以了:

echo strip_tags($_GET['msg']);
Copy after login
      第三点就是其他语言带过来的弊病,比如Java程序员可能很喜欢写get函数和set函数,但是作为PHP程序员完全没必要这么做,直接操作属性即可,因为使用这些属性的话,除了加大开销之外,没有做任何有意义的工作。

     第四点就是在数据库的操作上,有些新手喜欢用到一次数据就查一次,其实我们每次连接数据库再断开连接的开销还是蛮大的,我们应该把要做的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这种缓存,其实我第一点说的就是缓存。。。


Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles