php中静态方法static和非静态方法效率对比
今天在看这篇文章的时候http://jingyan.baidu.com/article/6b97984d7892d21ca2b0bfc2.html发现第一句是这个
如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍。
实践是检验代码执行效率的唯一标准,于是趁着下班回家有点时间,做了个小测试:
先上测试代码(如果有不对的地方,请大家指正哈):
测试环境:
/test # php -vPHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15) Copyright (c) 1997-2013 The PHP GroupZend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
/test # uname -aDarwin 192.168.0.101 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64
系统配置可以看这篇文章中的:http://blog.csdn.net/lamp_water/article/details/20968155
<?phpclass staticTest { public function test() { $i = 0; $i++; } public static function testStatic() { $i = 0; $i++; } }$start = microtime(true);for($i=0;$i<10000000;$i++) { $test = new staticTest(); $test->test();}echo (microtime(true) - $start) ."\n";$start = microtime(true);for($i=0;$i<br>循环执行一千万次执行结果如下:<p style="text-align:justify"><span style="font-family:Microsoft Yahei,微软雅黑,arial,宋体,sans-serif; font-size:14px; color:#333333"><span style="line-height:28px"></span></span></p><p class="p1"><span class="s1"><strong>/test # </strong></span>php staticTest.php </p><p class="p1">2.2938342094421</p><p class="p1">1.1871800422668<br><strong>第二次执行结果:</strong></p><p class="p1"></p><p class="p1">2.303295135498</p><p class="p1">1.1807670593262</p>执行很多次以后,都在2.3和1.18附近。可见,虽然没有文章中说的效率提升那么恐怖,性能的提升方面确实提升了一倍左右。<p style="text-align:justify"><span style="font-family:Microsoft Yahei,微软雅黑,arial,宋体,sans-serif; font-size:14px; color:#333333"><span style="line-height:28px">因此还是建议:</span></span></p><p style="text-align:justify"><span style="font-family:Microsoft Yahei,微软雅黑,arial,宋体,sans-serif; font-size:14px; color:#333333"><span style="line-height:28px"><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei',微软雅黑,arial,宋体,sans-serif; font-size:16px; line-height:28px; text-align:justify">如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近1倍。</span><br></span></span></p><p style="text-align:justify"><span style="font-family:Microsoft Yahei,微软雅黑,arial,宋体,sans-serif; font-size:14px; color:#333333"><span style="line-height:28px"><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei',微软雅黑,arial,宋体,sans-serif; font-size:16px; line-height:28px; text-align:justify">对于一个正常的php框架,里面的类和方法还是非常多的,如果我们能够做成静态方法,还是使用静态方法比较好,效率比较高。</span></span></span></p><p style="text-align:justify"><span style="font-family:Microsoft Yahei,微软雅黑,arial,宋体,sans-serif; font-size:14px; color:#333333"><span style="line-height:28px"><br></span></span></p><p style="text-align:justify"><span style="font-family:Microsoft Yahei,微软雅黑,arial,宋体,sans-serif; font-size:14px; color:#333333"><span style="line-height:28px"><br></span></span></p><p style="text-align:justify"><span style="font-family:Microsoft Yahei,微软雅黑,arial,宋体,sans-serif; font-size:14px; color:#333333"><span style="line-height:28px"><br></span></span></p><p style="text-align:justify"><span style="font-family:Microsoft Yahei,微软雅黑,arial,宋体,sans-serif; font-size:14px; color:#333333"><span style="line-height:28px"><br></span></span></p><div class="clear"> </div>

LeicareleasedtheLeicaLuxcameraappfortheAppleiPhoneafewdaysago.However,theappwasnotdevelopedbyLeica,butbyFjorden.ThecompanyhasbeenknownprimarilyforitscameragripsfortheiPhoneandwasacquiredbyLeicainDecember2023.Fo

SincethedemiseofLogitech'spopularHarmonyremotecontrols,themarketforhigh-qualityuniversalremotecontrolshasbeenfragmentedatbest.UnfoldedCircleaimstoavoidthefateoftheHarmonyUltimatebyeliminatinganyserverobligationsorsubs

WhiletheLightPhone2from2018wasstillequippedwithaneconomicale-inkdisplay,theLightPhone3usesanOLEDdisplaythatcanonlydisplaygrayscale.Thereasonfortheswitchtothe3.92-inchOLEDpanelwithitsresolutionof1,240x1,080isth

TheJabraEliteserieshascometoanend,withGNCEOPeterKarlstromerannouncingthediscontinuationofthewirelessearbudswithinthelineupinapressrelease.Petersaysthatthechangingmarketdynamicsandhowchallengingithasbecometogetas

Audio-TechnicahasunveiledtheATH-S300BTwirelessheadphoneswithhear-throughnoise-cancellation,multipointpairing,and90hoursofbatterylife.Thenoise-cancellingfeaturehasthreemodes:off,on,andhear-through,whereambientsoundscanbehea

Backin2022,whenPCIe7.0wasstartingtotakeshapeasafuturestandard,PCIe5.0wasjusthittingtheservermarketsandtheprospectofseeingPCIe6.0devicesavailableanytimesoon,letalonePCIe7.0ones,wasquitefar-fetched.CurrentlyPCIe5

JoeGrand-anethicalhackerandYouTuberwhoiswidelyknownforhisprojectsinvolvingcryptowallets,hassuccessfullyrecoveredalife-changingamountofbitcoin,presentlyvaluednorthof$3millionfromasoftwarewalletthathasbeenlockedformo

Realmehasofficiallyconfirmedthelaunchdateofitsupcomingaffordableflagship,theGT6.Accordingtothelatestroundofannouncements,thephonewillmakeitsdebutonJune20,whichisaroundthecorner.Thecompanyhasalsosharedagoodnumber


Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

DVWA
Damn Vulnerable Web App (DVWA) ialah aplikasi web PHP/MySQL yang sangat terdedah. Matlamat utamanya adalah untuk menjadi bantuan bagi profesional keselamatan untuk menguji kemahiran dan alatan mereka dalam persekitaran undang-undang, untuk membantu pembangun web lebih memahami proses mengamankan aplikasi web, dan untuk membantu guru/pelajar mengajar/belajar dalam persekitaran bilik darjah Aplikasi web keselamatan. Matlamat DVWA adalah untuk mempraktikkan beberapa kelemahan web yang paling biasa melalui antara muka yang mudah dan mudah, dengan pelbagai tahap kesukaran. Sila ambil perhatian bahawa perisian ini

PhpStorm versi Mac
Alat pembangunan bersepadu PHP profesional terkini (2018.2.1).

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

MinGW - GNU Minimalis untuk Windows
Projek ini dalam proses untuk dipindahkan ke osdn.net/projects/mingw, anda boleh terus mengikuti kami di sana. MinGW: Port Windows asli bagi GNU Compiler Collection (GCC), perpustakaan import yang boleh diedarkan secara bebas dan fail pengepala untuk membina aplikasi Windows asli termasuk sambungan kepada masa jalan MSVC untuk menyokong fungsi C99. Semua perisian MinGW boleh dijalankan pada platform Windows 64-bit.

ZendStudio 13.5.1 Mac
Persekitaran pembangunan bersepadu PHP yang berkuasa
