色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

PHP 魔法函數(shù)性能分析

瀏覽:94日期:2022-09-11 14:43:33

曾經(jīng)記得鳥哥Laruence提過不建議使用”魔術方法”,自此之后一旦涉及使用魔術方法的地方,我都會下意識的想一下,這樣寫真的好嗎?由于這一到兩年來一直忙于工作和學習新的知識,所以在這道坎兒上一直沒有做深入的探索一直恍恍惚惚過去了,今年是我進行深入學習的一年,因此現(xiàn)在必須對這個問題做個了結了。我們先來看看鳥哥Laruence博客曾經(jīng)提到的:

當我把PPT分享給公司的同事的時候, 會有人質(zhì)疑, 魔術方法都不讓用?

優(yōu)化的建議, 是建議, 是防止大家濫用, 肆無忌憚的用. 如果你能在寫代碼的時候, 能意識到, 什么慢, 什么快, 從而避免一些沒有必要的對魔術方法的調(diào)用, 那就是這個優(yōu)化建議所追求的效果了

疑惑 魔術方法真的性能比較差嗎? PHP7里使用魔術方法的性能還是存在問題嗎? 我們應該如何合理的使用魔術方法? 方案

面對我的疑惑,我的方案是:

統(tǒng)計對比使用魔術方法和不使用魔術方法腳本執(zhí)行的時間差異 PHP5.6.26-1 下連續(xù)執(zhí)行腳本n次 統(tǒng)計執(zhí)行時間的平均值/最小值/最大值 PHP7.0.12-2 下連續(xù)執(zhí)行腳本n次 統(tǒng)計執(zhí)行時間的平均值/最小值/最大值

目前個人能力有限,只能通過這種方式,如果你有更好的方案或者建議可以告訴我,謝謝,haha~

測試 __construct

首先我們先來看看構造函數(shù)__construct的實驗,php腳本如下:

<?php/** * 魔術方法性能探索 * * 構造函數(shù) * * @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];/** * 構造函數(shù)使用類名 */class ClassOne{ public function classOne() {# code... }}/** * 構造函數(shù)使用魔術函數(shù)__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不使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP5.6中連續(xù)調(diào)用腳本10000次sh test 10000 no_magic php5 construct// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__construct_no_magic_php5.log 10000// 結果avg: 34μmmax: 483μmmin: 26μm PHP5.6使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP5.6中連續(xù)調(diào)用腳本10000次sh test 10000 magic php5 construct// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__construct_magic_php5.log 10000// 結果avg: 28μmmax: 896μmmin: 20μm PHP7.0不使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP7.0中連續(xù)調(diào)用腳本10000次sh test 10000 no_magic php construct// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__construct_no_magic_php.log 10000// 結果avg: 19μmmax: 819μmmin: 13μm PHP7.0使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP7.0中連續(xù)調(diào)用腳本10000次sh test 10000 magic php construct// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__construct_magic_php.log 10000// 結果avg: 14μmmax: 157μmmin: 10μm

通過上面的數(shù)據(jù)我們可以看出:

使用__construct作為構造函數(shù)的腳本執(zhí)行的平均時間是要快于使用類名作為構造函數(shù)的, 大概快5到6微秒 ,不論是在php5.6還是php7.0中。

__call

接著,我們來看看__call的實驗,php腳本如下:

<?php/** * 魔術方法性能探索 * * 構造函數(shù) * * @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];/** * 構造函數(shù)使用類名 */class ClassOne{ public function __construct() {# code... } public function test() {# code... }}/** * 構造函數(shù)使用魔術函數(shù)__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不使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP5.6中連續(xù)調(diào)用腳本10000次sh test 10000 no_magic php5 call// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__call_no_magic_php5.log 10000// 結果avg: 27μmmax: 206μmmin: 20μm PHP5.6使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP5.6中連續(xù)調(diào)用腳本10000次sh test 10000 magic php5 call// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__call_magic_php5.log 10000// 結果avg: 29μmmax: 392μmmin: 22μm PHP7.0不使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP7.0中連續(xù)調(diào)用腳本10000次sh test 10000 no_magic php call// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__call_no_magic_php.log 10000// 結果avg: 16μmmax: 256μmmin: 10μm PHP7.0使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP7.0中連續(xù)調(diào)用腳本10000次sh test 10000 magic php call// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__call_magic_php.log 10000// 結果avg: 18μmmax: 2459μmmin: 11μm

通過上面的數(shù)據(jù)我們可以看出:

使用__call的腳本執(zhí)行的平均時間是要慢于不使用, 大概慢2微秒 ,不論是在php5.6還是php7.0中。

__callStatic

接著,我們來看看__callStatic的實驗,php腳本如下:

<?php/** * 魔術方法性能探索 * * 靜態(tài)重載函數(shù) * * @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靜態(tài)方法 */class ClassOne{ public function __construct() {# code... } public static function test() {# code... }}/** * 使用重載實現(xiàn)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不使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP5.6中連續(xù)調(diào)用腳本10000次sh test 10000 no_magic php5 callStatic// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__callStatic_no_magic_php5.log 10000// 結果avg: 25μmmax: 129μmmin: 19μm PHP5.6使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP5.6中連續(xù)調(diào)用腳本10000次sh test 10000 magic php5 callStatic// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__callStatic_magic_php5.log 10000// 結果avg: 28μmmax: 580μmmin: 20μm PHP7.0不使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP7.0中連續(xù)調(diào)用腳本10000次sh test 10000 no_magic php callStatic// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__callStatic_no_magic_php.log 10000// 結果avg: 14μmmax: 130μmmin: 9μm PHP7.0使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP7.0中連續(xù)調(diào)用腳本10000次sh test 10000 magic php callStatic// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__callStatic_magic_php.log 10000// 結果avg: 14μmmax: 159μmmin: 10μm

通過上面的數(shù)據(jù)我們可以看出:

在php5.6中使用__callStatic的腳本執(zhí)行的平均時間是要慢于不使用, 大概慢3微秒 ;在php7.0中使用__callStatic的腳本執(zhí)行的平均時間是要大致等于不使用__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];/** * 實現(xiàn)公共方法設置私有屬性 */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不使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP5.6中連續(xù)調(diào)用腳本10000次sh test 10000 no_magic php5 set// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__set_no_magic_php5.log 10000// 結果avg: 31μmmax: 110μmmin: 24μm PHP5.6使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP5.6中連續(xù)調(diào)用腳本10000次sh test 10000 magic php5 set// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__set_magic_php5.log 10000// 結果avg: 33μmmax: 138μmmin: 25μm PHP7.0不使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP7.0中連續(xù)調(diào)用腳本10000次sh test 10000 no_magic php set// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__set_no_magic_php.log 10000// 結果avg: 15μmmax: 441μmmin: 11μm PHP7.0使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP7.0中連續(xù)調(diào)用腳本10000次sh test 10000 magic php set// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__set_magic_php.log 10000// 結果avg: 17μmmax: 120μmmin: 11μm

通過上面的數(shù)據(jù)我們可以看出:

使用__set的腳本執(zhí)行的平均時間是要慢于不使用, 大概慢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];/** * 實現(xiàn)公共方法獲取私有屬性 */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不使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP5.6中連續(xù)調(diào)用腳本10000次sh test 10000 no_magic php5 get// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__get_no_magic_php5.log 10000// 結果avg: 28μmmax: 590μmmin: 20μm PHP5.6使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP5.6中連續(xù)調(diào)用腳本10000次sh test 10000 magic php5 get// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__get_magic_php5.log 10000// 結果avg: 28μmmax: 211μmmin: 22μm PHP7.0不使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP7.0中連續(xù)調(diào)用腳本10000次sh test 10000 no_magic php get// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__get_no_magic_php.log 10000// 結果avg: 16μmmax: 295μmmin: 10μm PHP7.0使用魔術方法數(shù)據(jù)如下,單位微秒μm

// PHP7.0中連續(xù)調(diào)用腳本10000次sh test 10000 magic php get// 運行數(shù)據(jù)統(tǒng)計腳本sh analysis ./logs/__get_magic_php.log 10000// 結果avg: 19μmmax: 525μmmin: 12μm

通過上面的數(shù)據(jù)我們可以看出:

在php5.6中使用__get的腳本執(zhí)行的平均時間是要大致等于不使用__get的;在php7.0中使用__get的腳本執(zhí)行的平均時間是要慢于不使用, 大概慢3微秒 。

結語

這里主要測試了__construct(), __call(), __callStatic(), __get(), __set()這五個常用的且可有其他實現(xiàn)方式代替的魔法函數(shù)。通過上面的測試再回來解答我的疑惑

魔術方法真的性能比較差嗎?

答:除了使用__construct之外,這里使用其他的魔法方法的時間大致慢10微妙以內(nèi)。

PHP7里使用魔術方法的性能還是存在問題嗎?

答:在PHP7中使用與不使用魔術方法之間的差異和在PHP5.6中近乎一致。

我們應該如何合理的使用魔術方法?

答:通過整個測試我們可以看出使不使用魔法方法這之間的執(zhí)行時間差異大致都是在10微妙以內(nèi)的,所以如果魔法方法可以很好的節(jié)省我們的開發(fā)成本和優(yōu)化我們的代碼結構,我們應該可以考慮犧牲掉這不到10微妙。而__construct是要快的,所以使用__construct應該沒什么異議。

來自:http://tigerb.cn/2017/03/04/php-magic-function/

標簽: PHP
相關文章:
主站蜘蛛池模板: 成人免费在线 | 经典香港一级a毛片免费看 精品400部自拍视频在线播放 | 亚洲精品国产经典一区二区 | 国内交换一区二区三区 | 欧美日韩免费做爰视频 | 久久99国产亚洲高清观看首页 | av狼论坛 | 国产精品分类视频分类一区 | 99国产精品视频免费观看 | 人成午夜| 亚洲国产高清在线 | 欧美高清性色生活片免费观看 | 高清午夜看片a福利在线观看琪琪 | 国产色啪午夜免费视频 | 成人看片黄a在线观看 | 一本久久道 | 九九九免费视频 | 性做久久久久久久免费观看 | 久久久久久久国产精品毛片 | 精品a视频 | 亚洲久草视频 | 亚洲精品天堂自在久久77 | 欧美人与z0z0xxxx| 性刺激免费视频观看在线观看 | 国产91精品久久久久久久 | 91久久精一区二区三区大全 | 日韩美毛片 | 欧美午夜性春猛交 | 国产成人精品aaaa视频一区 | 欧美aaaaaaaa| 日韩欧美在线一区二区三区 | 久久老司机波多野结衣 | 国产精品自在线天天看片 | 在线男人的天堂 | 国产一区二区三区欧美 | 日本特黄特色免费大片 | 午夜免费的国产片在线观看 | 欧美一级在线 | 日韩欧美~中文字幕 | 成人在线观看网址 | 国产精品区牛牛影院 |