PHP__autoload()方法真的影响性能吗?
介绍
对于php性能问题,议论最多的就是__autoload()方法,很多人提到这个方法非常影响性能。还有人说opcode也能影响到__autoload()方法,所以针对这两点我做了个测试。
最终发现__autoload方法在性能方面的影响不是很大的。有的网友说不用__autoload(),带来的维护成本比性能成本高无数倍(比较赞同)。
环境
PHP:5.3.9 ——以fastcgi模式启动
Nginx:1.1.12
eaccelerator:0.9.6.1
截图:
启动界面:
eAccelerator(在php.ini文件中)配置情况:
文件结构:(每一个Test文件都是6000多行代码的文件)
测试:带着eAccelerator缓存进行测试
测试代码:经过__autoload加载
图1
我在浏览器里通过刷新得到的结果分别如下:
Total Time:0.10401391983032
Total Time:0.10252094268799
Total Time:0.095267057418823
Total Time:0.10013008117676
Total Time:0.096842050552368
Total Time:0.097998142242432
Total Time:0.10348510742188
Total Time:0.096648931503296
测试:不经过__autoload()
图2
测试结果——通过刷浏览器
Total Time:0.10309100151062
Total Time:0.10285210609436
Total Time:0.10154414176941
Total Time:0.097845792770386
Total Time:0.099545001983643
Total Time:0.10166597366333
最终看到的结果:autoload方法没有出现明显的性能下降
测试:取消eAccelerator缓存进行测试
图1的测试结果
Total Time:0.24992394447327
Total Time:0.25681900978088
Total Time:0.25327301025391
Total Time:0.22580695152283
Total Time:0.22656512260437
Total Time:0.22530484199524
Total Time:0.23080611228943
图2的测试结果
Total Time:0.23054909706116
Total Time:0.22633790969849
Total Time:0.23442888259888
Total Time:0.2350070476532
Total Time:0.22897601127625
Total Time:0.23207712173462
__autoload()方法在性能上还是没有明显的影响。
总结
通过上面的两组实验得出:
1>__autoload方法没有明显的性能损失。所以大家不要介于使用__autoload方法。
2>opcode对代码的影响是比较大的,明显使用opcode的时候,php代码执行速度提高了将近2倍多。
摘自 川山甲

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

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
