php 性能优化之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>Copy after login
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>Copy after login
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 可以带给您好像好用的功能)

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

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.
