search
HomeBackend DevelopmentPHP Tutorialphp 性能优化之php 语言级的性能优化1

php 性能优化之php 语言级的性能优化一


   对于这个问题首先我们要知道影响php的性能的原因是什么?也就是

    1 什么情况下会出现php性能问题?

1php语法使用不当(包括某些业务可以使用php 本身自带的函数来处理)

2使用php语言做了它不擅长的事 

3用php语言链接的服务器不给力(当然如果是localhost也就是你本地配置比较差哈,建议换本吧,哈哈)

4php自身的短板 (PHP 自身就做不了)

5我们也不知道的问题 (囧)

 

  2 php 性能问题简介之php的性能问题的解决方向

从困难度由浅到深分别为:

1 Php 语言级的性能优化 

2 Php 周边问题的性能优化 (比如说mysql nginx|apache

3 Php 语言资深分析,优化(ps 主要指的是底层的c代码)


下面针对php语言级的性能优化做实例讨论,对标题里面说的内容进行一个测试,接下来要书写两个文件 bad.php, goods.php

我们要测试的是合并两个数组的操作(测试工具 apache ab test)

bad.php

思路:

先将数组1逐个加入到目标数组中;

之后,遍历数组2,对比数组2的元素是否在数组1中出现,如果没有则插入到目标数组,否则忽略掉

<span style="font-size:14px;"><?php //准备两个内容随机的数组$arr1 = $arr2 = $arr_merged = array();//接下来随机给两个数组赋值for ( $i=0; $i<rand(1000,2000); $i++) {	$arr1[] = rand();}for ( $i=0; $i<rand(1000,2000); $i++) {	$arr2[] = rand();}//开始循环比较foreach ( $arr1 as $v ) {	$arr_merged[]  = $v;}foreach ( $arr2 as $v ) {	if(!in_array($v, $arr_merged)){		$arr_merged[]  = $v;	}}var_dump($arr_merged);</span></span>


goods.php

思路:

随机生成两个数组,在打乱顺序

之后,使用array_merge 合并

<span style="font-size:14px;"><?php //准备两个内容随机的数组$arr1 = $arr2 = range(1000, 2000);$arr_merged = array();//接下来随机给两个数组赋值shuffle($arr1);shuffle($arr2);$arr_merged = array_merge($arr1, $arr2);var_dump($arr_merged);</span></span>


3 使用 ab 测试两个php脚本的速度:(ps:惊人的时刻开始了)

bad.php


goods.php



对于这两个测试,我们明显的看出这的差的不是一点半点啊,在执行bad.php 的时候我明显的感觉电脑的风扇翁的一下,你懂得!

bad.php 每秒响应174个请求, 每个请求处理572ms

goods.php 每秒响应4050个请求, 每个请求处理24ms

ps: 如果还要小伙伴在使用bad的写法,赶快改掉它,小心不要你的领导知道哦!



4 究其原因

对于这个结果我也很吃惊,但是事实就在眼前,没办法啊,那么到底为什么会有这样的效果呢,咱们继续一探究竟:

*.php(php代码) -----Scanner(zend引擎逐行扫描变为zend可以识别的语法)----> exprs  

-----parser(解析为opcode)-----> opcodes ------exec(执行最后输出)-------> output


可以知道 一个php文件执行zend需要先扫描,然后解析为opcode 在执行输出,问题就出在这里,当我们使用php自身的函数的时候其实他就属于zend 引擎,所以zend 解析起来就比较顺手,速度那叫个快(goods.php), 但是当执行bad.php 的时候就会比较慢,因为读别人写的代码肯定没有读自己写的代码快大家说对吧,所以还是使用原创,不要自己造轮子比较好啊!


多说一点,这也就是为什么现在好些Php扩展(比如 apc)都来缓存opcode 因为这样就少了扫描与解析的环节,那肯定是快了!


5 总结

优化点:少写代码,多用php自身提供的能力

性能问题:

自己写的代码冗余较多,可读性不好,并且性能低

为什么性能低?

php代码需要编译解析为底层语言, 这一过程每次请求都会 处理一遍,开销大。

好方法:

多使用php内置变量,常量, 函数(spl 可以带给您好像好用的功能)


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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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.

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

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