PHP 魔法函數性能分析
曾經記得鳥哥Laruence提過不建議使用”魔術方法”,自此之后一旦涉及使用魔術方法的地方,我都會下意識的想一下,這樣寫真的好嗎?由于這一到兩年來一直忙于工作和學習新的知識,所以在這道坎兒上一直沒有做深入的探索一直恍恍惚惚過去了,今年是我進行深入學習的一年,因此現在必須對這個問題做個了結了。我們先來看看鳥哥Laruence博客曾經提到的:
當我把PPT分享給公司的同事的時候, 會有人質疑, 魔術方法都不讓用?
優化的建議, 是建議, 是防止大家濫用, 肆無忌憚的用. 如果你能在寫代碼的時候, 能意識到, 什么慢, 什么快, 從而避免一些沒有必要的對魔術方法的調用, 那就是這個優化建議所追求的效果了
疑惑
- 魔術方法真的性能比較差嗎?
- PHP7里使用魔術方法的性能還是存在問題嗎?
- 我們應該如何合理的使用魔術方法?
方案
面對我的疑惑,我的方案是:
- 統計對比使用魔術方法和不使用魔術方法腳本執行的時間差異
- PHP5.6.26-1 下連續執行腳本n次
- 統計執行時間的平均值/最小值/最大值
- PHP7.0.12-2 下連續執行腳本n次
- 統計執行時間的平均值/最小值/最大值
目前個人能力有限,只能通過這種方式,如果你有更好的方案或者建議可以告訴我,謝謝,haha~
測試
__construct
首先我們先來看看構造函數__construct的實驗,php腳本如下:
<?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使用魔術方法數據如下,單位微秒μm
// PHP5.6中連續調用腳本10000次
sh test 10000 magic php5 construct
// 運行數據統計腳本
sh analysis ./logs/__construct_magic_php5.log 10000
// 結果
avg: 28μm
max: 896μm
min: 20μm
- PHP7.0不使用魔術方法數據如下,單位微秒μm
// PHP7.0中連續調用腳本10000次
sh test 10000 no_magic php construct
// 運行數據統計腳本
sh analysis ./logs/__construct_no_magic_php.log 10000
// 結果
avg: 19μm
max: 819μm
min: 13μm
- PHP7.0使用魔術方法數據如下,單位微秒μm
// PHP7.0中連續調用腳本10000次
sh test 10000 magic php construct
// 運行數據統計腳本
sh analysis ./logs/__construct_magic_php.log 10000
// 結果
avg: 14μm
max: 157μm
min: 10μm
通過上面的數據我們可以看出:
使用__construct作為構造函數的腳本執行的平均時間是要快于使用類名作為構造函數的, 大概快5到6微秒 ,不論是在php5.6還是php7.0中。
__call
接著,我們來看看__call的實驗,php腳本如下:
<?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使用魔術方法數據如下,單位微秒μm
// PHP5.6中連續調用腳本10000次
sh test 10000 magic php5 call
// 運行數據統計腳本
sh analysis ./logs/__call_magic_php5.log 10000
// 結果
avg: 29μm
max: 392μm
min: 22μm
- PHP7.0不使用魔術方法數據如下,單位微秒μm
// PHP7.0中連續調用腳本10000次
sh test 10000 no_magic php call
// 運行數據統計腳本
sh analysis ./logs/__call_no_magic_php.log 10000
// 結果
avg: 16μm
max: 256μm
min: 10μm
- PHP7.0使用魔術方法數據如下,單位微秒μm
// PHP7.0中連續調用腳本10000次
sh test 10000 magic php call
// 運行數據統計腳本
sh analysis ./logs/__call_magic_php.log 10000
// 結果
avg: 18μm
max: 2459μm
min: 11μm
通過上面的數據我們可以看出:
使用__call的腳本執行的平均時間是要慢于不使用, 大概慢2微秒 ,不論是在php5.6還是php7.0中。
__callStatic
接著,我們來看看__callStatic的實驗,php腳本如下:
<?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使用魔術方法數據如下,單位微秒μm
// PHP5.6中連續調用腳本10000次
sh test 10000 magic php5 callStatic
// 運行數據統計腳本
sh analysis ./logs/__callStatic_magic_php5.log 10000
// 結果
avg: 28μm
max: 580μm
min: 20μm
- PHP7.0不使用魔術方法數據如下,單位微秒μm
// PHP7.0中連續調用腳本10000次
sh test 10000 no_magic php callStatic
// 運行數據統計腳本
sh analysis ./logs/__callStatic_no_magic_php.log 10000
// 結果
avg: 14μm
max: 130μm
min: 9μm
- PHP7.0使用魔術方法數據如下,單位微秒μm
// PHP7.0中連續調用腳本10000次
sh test 10000 magic php callStatic
// 運行數據統計腳本
sh analysis ./logs/__callStatic_magic_php.log 10000
// 結果
avg: 14μm
max: 159μm
min: 10μm
通過上面的數據我們可以看出:
在php5.6中使用__callStatic的腳本執行的平均時間是要慢于不使用, 大概慢3微秒 ;在php7.0中使用__callStatic的腳本執行的平均時間是要大致等于不使用__callStatic的;
__set
接著,我們來看看__set的實驗,php腳本如下:
<?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使用魔術方法數據如下,單位微秒μm
// PHP5.6中連續調用腳本10000次
sh test 10000 magic php5 set
// 運行數據統計腳本
sh analysis ./logs/__set_magic_php5.log 10000
// 結果
avg: 33μm
max: 138μm
min: 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μm
max: 441μm
min: 11μm
- PHP7.0使用魔術方法數據如下,單位微秒μm
// PHP7.0中連續調用腳本10000次
sh test 10000 magic php set
// 運行數據統計腳本
sh analysis ./logs/__set_magic_php.log 10000
// 結果
avg: 17μm
max: 120μm
min: 11μm
通過上面的數據我們可以看出:
使用__set的腳本執行的平均時間是要慢于不使用, 大概慢2微秒 ,不論是在php5.6還是php7.0中。
__get
接著,我們來看看__get的實驗,php腳本如下:
<?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使用魔術方法數據如下,單位微秒μm
// PHP5.6中連續調用腳本10000次
sh test 10000 magic php5 get
// 運行數據統計腳本
sh analysis ./logs/__get_magic_php5.log 10000
// 結果
avg: 28μm
max: 211μm
min: 22μm
- PHP7.0不使用魔術方法數據如下,單位微秒μm
// PHP7.0中連續調用腳本10000次
sh test 10000 no_magic php get
// 運行數據統計腳本
sh analysis ./logs/__get_no_magic_php.log 10000
// 結果
avg: 16μm
max: 295μm
min: 10μm
- PHP7.0使用魔術方法數據如下,單位微秒μm
// PHP7.0中連續調用腳本10000次
sh test 10000 magic php get
// 運行數據統計腳本
sh analysis ./logs/__get_magic_php.log 10000
// 結果
avg: 19μm
max: 525μm
min: 12μm
通過上面的數據我們可以看出:
在php5.6中使用__get的腳本執行的平均時間是要大致等于不使用__get的;在php7.0中使用__get的腳本執行的平均時間是要慢于不使用, 大概慢3微秒 。
結語
這里主要測試了__construct(), __call(), __callStatic(), __get(), __set()這五個常用的且可有其他實現方式代替的魔法函數。通過上面的測試再回來解答我的疑惑
- 魔術方法真的性能比較差嗎?
答:除了使用__construct之外,這里使用其他的魔法方法的時間大致慢10微妙以內。
- PHP7里使用魔術方法的性能還是存在問題嗎?
答:在PHP7中使用與不使用魔術方法之間的差異和在PHP5.6中近乎一致。
- 我們應該如何合理的使用魔術方法?
答:通過整個測試我們可以看出使不使用魔法方法這之間的執行時間差異大致都是在10微妙以內的,所以如果魔法方法可以很好的節省我們的開發成本和優化我們的代碼結構,我們應該可以考慮犧牲掉這不到10微妙。而__construct是要快的,所以使用__construct應該沒什么異議。
來自:http://tigerb.cn/2017/03/04/php-magic-function/