PHP是一种执行起来非常迅速的编程语言,但是比起仅仅优化代码来说仍然值得优化PHP本身。
本文我们将根据一些实效阐述为什么优化PHP本身要比优化代码来的更贴切,以及为什么需要理解根据PHP在你的服务器上其他相关子系统的表现找出瓶颈并修复之。与此同时,我们也提到了如何优化您的PHP代码来让他们拥有更快的执行速度。
获得高性能
当我们谈及好的性能,往往不仅仅是指您的PHP代码执行起来有多快。性能是一套在可量化评测和速度之间取出的平衡。仅仅依靠使用更少资源的代码执行起来也许比在高速缓存中之行的代码更慢,并且相同的一组(在高速缓冲中执行的)代码可以在同时并发执行在一台Web服务器上。
在下面的例子中,A.php算作一位尽可能跑得快的赛跑选手,而B.php是一个几乎可以以同一慢速永远跑下去的马拉松选手。轻负荷情况下,A.php可以充分的快,但是当流量增加后,B.php的性能表现将仅仅降低一点点而A.php会垮掉。

让我们来通过一个事实来验证此说法更深远的本质意义。假设我们需要读取一个250K的文件并生成一个关于此文件的HTML概要。我们写了两个脚本来做同样一件事:hare.php将一次性读取整个文件到内存中,然后一步执行到位;而tortoise.php每次只读取文件的一行,并且决不超过内容容量。结果Tortoise.php因为多次读写需要更多的系统回应而慢得多。"
程序每执行一次,hare.php需要0.04秒CPU执行时间和10Mb的内存,而tortoise.php需要0.06秒CPU执行时间和5Mb的内存。服务器共有100Mb实际内存容量并且其CPU有99%是空闲的。我们同时假定执行这样一个简单事件不产生内存碎片。
当有10各程序并发执行时,hare.php将发生内存溢出(10 ×10 = 100)。与此同时,tortoise.php仍将有50Mb空余内存可用!11个程序并发执行将使hare.php彻底“溃败”因为它开始需要使用虚拟内存——执行速度有可能降低到其常规速度的一半以下;而且现在每一个单独程序进程需要0.08秒CPU执行时间。而此期间,tortoise.php仍旧运行在其常规CPU执行时间——0.06秒!
以下表格中,执行得更快的PHP脚本使用粗体区分开来:
------------------------------------------------------------------------------------------
| 连接数 | 每执行1次HTTP请求所需CPU执行时间(秒)| 每执行10次HTTP请求所需CPU执行时间(秒)| 每执行11次HTTP请求所需CPU执行时间(秒)|
------------------------------------------------------------------------------------------
| hare.php | 0.04 | 0.04 | 0.88(内存溢出) |
------------------------------------------------------------------------------------------
| tortoise.php | 0.06 | 0.60 | 0.66 |
------------------------------------------------------------------------------------------
如您在上例中看到的,获得更好的性能不再仅仅是写出执行起来更快的PHP程序。高性能PHP表现需要对底层硬件知识以及操作系统、软件支持如Web服务器、数据库等有一个良好认识和理解。
瓶颈
以上两个例子让我们看到了(性能)下降的瓶颈所在。当拥有无限大容量的内存时,hare.php的确是始终比tortoise.php快。但是,仅仅认为内存是PHP整体性能的瓶颈所在显得过于单纯——实际上远不止这些:
(a) 网络
你的网络有可能是最大的瓶颈所在。如果你有10M的带宽——最多你只能获得1M/秒的传输速度。如果假设每个PHP页为30k,那么每秒仅仅只传输33页就将使你的网络带宽达到饱和。更多导致瓶颈产生的因素包括频繁访问低速DNS,或者网络设备仅能获得十分有限的存储。
(b) CPU
如果你监视一下你的CPU负荷情况,发送一个纯静态HTML页面并不会增加CPU负担——就像我们以上提到的,此时瓶颈在于网络。当然啦,对于由PHP生成的复杂动态页面,你的CPU速度自然将成为限制因素之一。拥有包含多个CPU的服务器或者一个服务器阵列将减轻因CPU带来的影响。

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment