大家须知简单的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性能优化注意点,希望本文分享能够帮助到大家。

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

Performance optimization techniques for using PHP to develop and implement Baidu Wenxin Yiyan API interface. With the popularity of the Internet, more and more developers use third-party API interfaces to obtain data to enrich their application content. Baidu Wenxin Yiyan API interface is a popular data interface. It can return a random inspirational, philosophical or warm sentence, which can be used to beautify the program interface, increase user experience, etc. However, when using the Baidu Wenxinyiyan API interface, we also face some performance considerations. API call speed

How to use PHP to optimize website performance and loading speed With the rapid development of the Internet, website performance and loading speed have attracted more and more attention. As a widely used server-side scripting language, PHP plays an important role in optimizing website performance and loading speed. This article will introduce some tips and methods for using PHP to improve the performance and loading speed of your website. Using a caching mechanism Caching is an effective way to improve website performance. PHP provides a variety of caching mechanisms, such as file caching, memory caching and data caching.

How to standardize performance optimization through PHP code specifications Introduction: With the rapid development of the Internet, more and more websites and applications are developed based on the PHP language. In the PHP development process, performance optimization is a crucial aspect. A high-performance PHP code can significantly improve the website's response speed and user experience. This article will explore how to standardize performance optimization through PHP code specifications and provide some practical code examples for reference. 1. Reduce database queries. Frequent database queries are a common feature during the development process.

How to use PHP for performance optimization and tuning In the process of developing web applications, performance optimization and tuning are important tasks that cannot be ignored. As a popular server-side scripting language, PHP also has some techniques and tools that can improve performance. This article will introduce some common PHP performance optimization and tuning methods, and provide sample code to help readers better understand. Using cache caching is one of the important means to improve the performance of web applications. You can reduce access to the database and reduce IO operations to improve performance by using cache. make

PHP7 performance optimization tips: How to use the isset function to determine whether a variable has been declared Introduction: In PHP development, we often need to determine whether a variable has been declared. This is particularly important in situations such as when using an undeclared variable that produces an error. In PHP7, for performance optimization reasons, we should try to use the isset function to determine whether a variable has been declared, instead of directly using functions such as empty and is_null. Why use isset: In PHP

In actual development, in order to achieve better performance and higher scalability of a website or application, the optimization of PHP code is a very important step. Here are some PHP performance tips to help your code run faster. 1. Minimize function calls and variables 1.1 Function calls Function calls have a great impact on the performance of PHP code, because each function needs to allocate space in memory. When writing PHP code, you should try to avoid too many function calls and use inline functions or custom functions instead. 1.2 Variables

How to improve website performance and responsiveness with PHP? With the development of the Internet, website performance and response speed are increasingly important for user experience and search engine optimization. As a commonly used server-side scripting language, PHP also plays a key role in improving website performance and response speed. This article will introduce some methods to improve website performance and response speed through PHP. 1. Optimize the code and use the appropriate PHP version: choose the latest stable version and choose the appropriate running method (CGI/F

PHP is a widely used server-side programming language that plays an important role in web development. When we develop APIs, performance optimization is very important. The best performance optimization methods can help us create efficient and fast APIs, improve the response speed of the website, and provide a better user experience. This article will explore the best performance optimization methods in PHPAPI development. 1. Set up PHP cache PHP cache is a technology for caching PHP code, which can help us improve execution speed. PHP cache will
