PHP performance analysis magic method example sharing
I once remembered that Brother Laruence mentioned that it was not recommended to use "magic methods". Since then, whenever magic methods are involved, bloggers will subconsciously think about it. This is a good photo. ? Since I have been busy with work and learning new knowledge in the past two years, I have not done any in-depth exploration on this hurdle and it has been in a daze. This year is a year for bloggers to conduct in-depth study, so now I must This issue is settled. Let’s first take a look at what Bird Brother Laruence once mentioned on his blog:
Optimization suggestions are suggestions to prevent people from abusing and using them unscrupulously. If you can realize when writing code, what Slow, what is fast, so as to avoid unnecessary calls to magic methods, that is the effect pursued by this optimization suggestion
Doubts
Is the performance of magic methods really poor?
Is there still a problem with the performance of using magic methods in PHP7?
How should we use magic methods reasonably?
Plan
Faced with my doubts, my plan is:
Statistical comparison of the time difference between script execution using the magic method and not using the magic method
PHP5. 6.26-1 Continuously execute the script n times under
Statistics of the average/minimum/maximum execution time
PHP7.0.12-2 Continuously execute the script n times under PHP7.0.12-2
Statistics of the average/minimum execution time /Maximum value
Currently my personal ability is limited and I can only use this method. If you have a better plan or suggestion, you can tell me, thank you, haha~
test
__construct
First of all, let’s take a look at the experiment of the constructor function __construct. The PHP script is as follows:
<?php/** * 魔术方法性能探索 * * 构造函数 * * @author TIGERB <https://github.com/TIGERB> */require('./function.php');if (!isset($argv[1])) { die('error: variable is_use_magic is empty'); } $is_use_magic = $argv[1];/** * 构造函数使用类名 */class ClassOne { public function classOne() { # code... } }/** * 构造函数使用魔术函数__construct */class ClassTwo { public function __construct() { # code... } } $a = getmicrotime();if ($is_use_magic === 'no_magic') { new ClassOne(); }else { new ClassTwo(); } $b = getmicrotime(); echo ($b-$a) . "\n"; PHP5.6不使用魔术方法数据如下,单位微秒μm// PHP5.6中连续调用脚本10000次sh test 10000 no_magic php5 construct// 运行数据统计脚本sh analysis ./logs/__construct_no_magic_php5.log 10000// 结果avg: 34μm max: 483μm min: 26μm
PHP5.6 uses the magic method data as follows, in microseconds μm
// PHP5.6 Call the script 10,000 times in succession
sh test 10000 magic php5 construct// 运行数据统计脚本sh analysis ./logs/__construct_magic_php5.log 10000// 结果avg: 28μmmax: 896μmmin: 20μm
PHP7.0 does not use the magic method and the data is as follows, the unit is microsecond μm
// PHP7.0中连续调用脚本10000次sh test 10000 no_magic php construct// 运行数据统计脚本sh analysis ./logs/__construct_no_magic_php.log 10000// 结果avg: 19μmmax: 819μmmin: 13μm
PHP7.0 uses the magic method the data is as follows, the unit is microsecond μm
// Continuously call the script 10,000 times in PHP7.0
sh test 10000 magic php construct// 运行数据统计脚本sh analysis ./logs/__construct_magic_php.log 10000// 结果avg: 14μmmax: 157μmmin: 10μm
From the above data we can see:
The average execution time of the script using __construct as the constructor is faster than using Class name as constructor is about 5 to 6 microseconds faster, both in php5.6 and php7.0.
__call
Next, let’s take a look at the __call experiment. The PHP script is as follows:
<?php/** * 魔术方法性能探索 * * 构造函数 * * @author TIGERB <https://github.com/TIGERB> */require('./function.php');if (!isset($argv[1])) { die('error: variable is_use_magic is empty'); }$is_use_magic = $argv[1];/** * 构造函数使用类名 */class ClassOne{ public function __construct() { # code... } public function test() { # code... } }/** * 构造函数使用魔术函数__construct */class ClassTwo{ public function __construct() { # code... } public function __call($method, $argus) { # code... } }$a = getmicrotime();if ($is_use_magic === 'no_magic') { $instance = new ClassOne(); $instance->test(); }else { $instance = new ClassTwo(); $instance->test(); }$b = getmicrotime();echo ($b-$a) . "\n"; PHP5.6不使用魔术方法数据如下,单位微秒μm// PHP5.6中连续调用脚本10000次sh test 10000 no_magic php5 call// 运行数据统计脚本sh analysis ./logs/__call_no_magic_php5.log 10000// 结果avg: 27μm max: 206μm min: 20μm
PHP5.6 uses the magic method and the data is as follows, in microseconds μm
// PHP5.6中连续调用脚本10000次sh test 10000 magic php5 call// 运行数据统计脚本sh analysis ./logs/__call_magic_php5.log 10000// 结果avg: 29μmmax: 392μmmin: 22μm
PHP7.0 does not use the magic method. The data is as follows, in microseconds μm
// PHP7.0中连续调用脚本10000次sh test 10000 no_magic php call// 运行数据统计脚本sh analysis ./logs/__call_no_magic_php.log 10000// 结果avg: 16μmmax: 256μmmin: 10μm
PHP7.0 uses the magic method. The data is as follows, in microseconds μm
// PHP7.0中连续调用脚本10000次sh test 10000 magic php call// 运行数据统计脚本sh analysis ./logs/__call_magic_php.log 10000// 结果avg: 18μmmax: 2459μmmin: 11μm
Through the above data We can see that:
The average execution time of a script using __call is slower than that without using it, about 2 microseconds slower, whether in php5.6 or php7.0.
__callStatic
Next, let’s take a look at the __callStatic experiment. The PHP script is as follows:
<?php/** * 魔术方法性能探索 * * 静态重载函数 * * @author TIGERB <https://github.com/TIGERB> */require('./function.php');if (!isset($argv[1])) { die('error: variable is_use_magic is empty'); }$is_use_magic = $argv[1];/** * 存在test静态方法 */class ClassOne{ public function __construct() { # code... } public static function test() { # code... } }/** * 使用重载实现test */class ClassTwo{ public function __construct() { # code... } public static function __callStatic($method, $argus) { # code... } }$a = getmicrotime();if ($is_use_magic === 'no_magic') { ClassOne::test(); }else { ClassTwo::test(); }$b = getmicrotime();echo ($b-$a) . "\n"; PHP5.6不使用魔术方法数据如下,单位微秒μm// PHP5.6中连续调用脚本10000次sh test 10000 no_magic php5 callStatic// 运行数据统计脚本sh analysis ./logs/__callStatic_no_magic_php5.log 10000// 结果avg: 25μm max: 129μm min: 19μm
PHP5.6 uses the magic method data as follows, in microseconds μm
// PHP5.6中连续调用脚本10000次sh test 10000 magic php5 callStatic// 运行数据统计脚本sh analysis ./logs/__callStatic_magic_php5.log 10000// 结果avg: 28μmmax: 580μmmin: 20μm
PHP7.0 does not use the magic method. The data is as follows, in microseconds μm
// PHP7.0中连续调用脚本10000次sh test 10000 no_magic php callStatic// 运行数据统计脚本sh analysis ./logs/__callStatic_no_magic_php.log 10000// 结果avg: 14μmmax: 130μmmin: 9μm
PHP7.0 uses the magic method. The data is as follows, in microseconds μm
// PHP7.0中连续调用脚本10000次sh test 10000 magic php callStatic// 运行数据统计脚本sh analysis ./logs/__callStatic_magic_php.log 10000// 结果avg: 14μmmax: 159μmmin: 10μm
Through the above data We can see that:
The average execution time of scripts using __callStatic in php5.6 is slower than that without using it, about 3 microseconds; in php7.0, the execution time of scripts using __callStatic is slower The average time should be roughly equal to not using __callStatic;
__set
Next, let’s take a look at the __set experiment. The php script is as follows:
<?php /** * 魔术方法性能探索 * * 设置私有属性__set * * @author TIGERB <https://github.com/TIGERB> */require('./function.php');if (!isset($argv[1])) { die('error: variable is_use_magic is empty'); } $is_use_magic = $argv[1];/** * 实现公共方法设置私有属性 */class ClassOne { /** * 私有属性 * * @var string */ private $someVariable = 'private'; public function __construct() { # code... } public function setSomeVariable($value = '') { $this->someVariable = $value; } }/** * 使用_set设置私有属性 */class ClassTwo { /** * 私有属性 * * @var string */ private $someVariable = 'private'; public function __construct() { # code... } public function __set($name = '', $value = '') { $this->$name = $value; } } $a = getmicrotime();if ($is_use_magic === 'no_magic') { $instance = new ClassOne(); $instance->setSomeVariable('public'); }else { $instance = new ClassTwo(); $instance->someVariable = 'public'; } $b = getmicrotime(); echo ($b-$a) . "\n"; PHP5.6不使用魔术方法数据如下,单位微秒μm// PHP5.6中连续调用脚本10000次sh test 10000 no_magic php5 set// 运行数据统计脚本sh analysis ./logs/__set_no_magic_php5.log 10000// 结果avg: 31μm max: 110μm min: 24μm
PHP5.6 The data using the magic method is as follows, the unit is microsecond μm
// PHP5.6中连续调用脚本10000次 sh test 10000 magic php5 set// 运行数据统计脚本sh analysis ./logs/__set_magic_php5.log 10000// 结果avg: 33μmmax: 138μmmin: 25μm PHP7.0不使用魔术方法数据如下,单位微秒μm// PHP7.0中连续调用脚本10000次sh test 10000 no_magic php set// 运行数据统计脚本sh analysis ./logs/__set_no_magic_php.log 10000// 结果avg: 15μmmax: 441μmmin: 11μm PHP7.0使用魔术方法数据如下,单位微秒μm// PHP7.0中连续调用脚本10000次sh test 10000 magic php set// 运行数据统计脚本sh analysis ./logs/__set_magic_php.log 10000// 结果avg: 17μmmax: 120μmmin: 11μm
We can see from the above data:
The average execution time of the script using __set is slower than not using it, probably slower. 2 microseconds, whether in php5.6 or php7.0.
__get
Next, let’s take a look at the __get experiment. The PHP script is as follows:
<?php/** * 魔术方法性能探索 * * 读取私有属性__get * * @author TIGERB <https://github.com/TIGERB> */require('./function.php');if (!isset($argv[1])) { die('error: variable is_use_magic is empty'); } $is_use_magic = $argv[1];/** * 实现公共方法获取私有属性 */class ClassOne { /** * 私有属性 * * @var string */ private $someVariable = 'private'; public function __construct() { # code... } public function getSomeVariable() { return $this->someVariable; } }/** * 使用_get获取私有属性 */class ClassTwo { /** * 私有属性 * * @var string */ private $someVariable = 'private'; public function __construct() { # code... } public function __get($name = '') { return $this->$name; } } $a = getmicrotime();if ($is_use_magic === 'no_magic') { $instance = new ClassOne(); $instance->getSomeVariable(); }else { $instance = new ClassTwo(); $instance->someVariable; } $b = getmicrotime(); echo ($b-$a) . "\n"; PHP5.6不使用魔术方法数据如下,单位微秒μm// PHP5.6中连续调用脚本10000次sh test 10000 no_magic php5 get// 运行数据统计脚本sh analysis ./logs/__get_no_magic_php5.log 10000// 结果avg: 28μm max: 590μm min: 20μm
PHP5.6 uses the magic method to data as follows, in microseconds μm
// PHP5.6中连续调用脚本10000次 sh test 10000 magic php5 get// 运行数据统计脚本sh analysis ./logs/__get_magic_php5.log 10000// 结果avg: 28μmmax: 211μmmin: 22μm
PHP7.0 does not use the magic method. The data is as follows, in microseconds μm
// PHP7.0中连续调用脚本10000次 sh test 10000 no_magic php get// 运行数据统计脚本sh analysis ./logs/__get_no_magic_php.log 10000// 结果avg: 16μmmax: 295μmmin: 10μm
PHP7.0 uses the magic method. The data is as follows, in microseconds μm
// PHP7.0中连续调用脚本10000次 sh test 10000 magic php get// 运行数据统计脚本sh analysis ./logs/__get_magic_php.log 10000// 结果avg: 19μmmax: 525μmmin: 12μm
Through the above data We can see that:
The average execution time of a script using __get in php5.6 is roughly equal to that of a script that does not use __get; the average execution time of a script using __get in php7.0 is Slower than not used, about 3 microseconds slower.
Conclusion
Here we mainly tested __construct(), __call(), __callStatic(), __get(), __set() which are commonly used and can be replaced by other implementation methods. Magic function. After passing the above test, I will come back to answer my doubts
Is the performance of the magic method really poor?
Answer: In addition to using __construct, the time of using other magic methods here is roughly within 10 microseconds.
Is there still a problem with the performance of magic methods in PHP7?
Answer: The difference between using and not using magic methods in PHP7 is almost the same as in PHP5.6.
How should we use magic methods reasonably?
Answer: Through the entire test, we can see that the difference in execution time between not using the magic method is roughly within 10 microseconds, so if the magic method can save our development costs and To optimize our code structure, we should consider sacrificing less than 10 microseconds. __construct is meant to be fast, so there should be no objection to using __construct.
I once remembered that Brother Laruence mentioned that it was not recommended to use "magic methods." Since then, whenever magic methods are involved, bloggers will subconsciously think about it, is this a good photo? Since I have been busy with work and learning new knowledge in the past two years, I have not done any in-depth exploration on this hurdle and it has been in a daze. This year is a year for bloggers to conduct in-depth study, so now I must This issue is settled. Let’s first take a look at what Bird Brother Laruence once mentioned on his blog:
Optimization suggestions are suggestions to prevent people from abusing and using them unscrupulously. If you can realize when writing code, what What is slow, what is fast, so as to avoid unnecessary calls to magic methods, that is the effect pursued by this optimization suggestion
The above is the detailed content of PHP performance analysis magic method example sharing. For more information, please follow other related articles on the PHP Chinese website!

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.
