大家须知简单的php性能优化注意点,须知php性能优化
大家须知简单的php性能优化注意点,须知php性能优化
什么情况,可能遇到性能问题:
1.php语法使用的不恰当
2.使用php语言做了它不擅长做的事
3.用php语言连接的服务不给力
4.php自身的短板
5.我也不知道的问题
一般情况:php性能问题不超过二分之一(一般30%~40%)
php性能问题解决方向:
php语言级的性能优化—>php周边问题的性能优化(连接的服务,网络环境)—>php语言自身分析和优化
(php语言级)
优化点:少写代码,多用php自身能力
问题:自写代码冗余较多,可读性不佳,导致性能低
为什么低:php代码需要编译解析为底层语言,这一过程每次请求都会处理一遍,开销大
方法:多使用php内置变量、常量、函数
优化点:php内置函数的性能优劣
情况描述:php内置函数,之间依然存在快慢差异
建议:多去了解php内置函数的时间复杂度
优化点:尽可能少用魔法函数
情况描述:php提供的魔法函数,性能不佳
为什么性能低:为了为php程序员省事,php语言为你做了很多
好的方法:尽可能规避使用php魔法函数
优化点:产生额外开销的错误抑制符@
实际逻辑:在代码开始前,设置报错级别最高,结束后,再回复设置报错级别。增加Opcode,忽略报错
优化点:合理使用内存
情况描述:php有内存回收机制保底,但也请小心使用内存
建议:利用unset()及时释放不适用的内存(注:unset()出现注销不掉的情况)
优化点:尽量少的使用正则表达式
情况描述:正在表达式的回溯开销较大,“没有金刚钻别揽陶瓷活”
建议:利用字符串处理函数,实现相同逻辑
优化点:避免在循环内做运算
情况描述:循环内的计算式将会被重复计算
<?php $str="hello world"; 阿 //strlen($str)放在外面 for($i=0;$i<strlen($str);$i++){ //do something } ?>
优化点:减少计算密集型业务
情况描述:php不适合密集型运算的场景
为什么?php语言特性决定了php不适合做大数据量运算
php适合场景:适合衔接Webserver与后端服务、UI呈现
优化点:务必使用带引号字符串做键值
情况描述:php会将没有引号的键值当作常量,产生查找常量的开销
建议:严格使用引号作为键值
--------------------------------------------
(php周边问题的性能优化)-
运行环境,文件储存,数据库,缓存,网络
减少文件类操作
常见php场景的开销次序:
读写磁盘、读写数据库、读写内存、读写网络数据
读写内存
优化网络请求
网络请求的坑:
1.对方接口的不确定因素
2.网络稳定性
如何优化网络请求?
1、设置超时时间
a)连接超时 200ms
b)读超时 800ms
c)写超时 500ms
2、将串行请求并行化
a)使用curl_multi_*()
b)使用swoole扩展
压缩php接口输出
缓存重复计算内容
什么情况下做输出内容的缓存?
多次请求,内容不变情况
重叠时间窗口思想
旁路方案
分析php自身分析和优化:
用工具测试
PHP性能瓶颈解决方法:
Opcode cache(代码编译的最后一个环节进行缓存)PHP扩展APC做Opcode缓存用的
补充压力测试软件使用说明:
Ab -h
apache Benchmark(ab)由Apache提供的压力测试软件,安装apache服务器时会自带该压测软件
使用:./ab -n1000 -c100 http://www.baidu.com/
-n请求数 -c并发数 url目标压测地址
以上内容给大家介绍了简单的php性能优化注意点,希望本文分享能够帮助到大家。
您可能感兴趣的文章:
- 优化php效率,提高php性能的一些方法
- php性能优化分析工具XDebug 大型网站调试工具
- PHP性能优化 产生高度优化代码
- PHP性能优化工具篇Benchmark类调试执行时间
- LAMP服务器性能优化技巧之加速PHP
- 50个PHP程序性能优化的方法
- php导入大量数据到mysql性能优化技巧

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

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

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

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

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

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.
