cari
Rumahpembangunan bahagian belakangtutorial phpphp中静态方法static和非静态方法效率对照

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>
Kenyataan
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Apple iPhone 16 Pro as Leica smartphone: Leica subsidiary confirms camera grip with official Leica appApple iPhone 16 Pro as Leica smartphone: Leica subsidiary confirms camera grip with official Leica appJun 13, 2024 pm 08:52 PM

LeicareleasedtheLeicaLuxcameraappfortheAppleiPhoneafewdaysago.However,theappwasnotdevelopedbyLeica,butbyFjorden.ThecompanyhasbeenknownprimarilyforitscameragripsfortheiPhoneandwasacquiredbyLeicainDecember2023.Fo

Remote 3 universal remote control comes with touchscreen, but without subscription or server obligationRemote 3 universal remote control comes with touchscreen, but without subscription or server obligationJun 14, 2024 am 09:13 AM

SincethedemiseofLogitech'spopularHarmonyremotecontrols,themarketforhigh-qualityuniversalremotecontrolshasbeenfragmentedatbest.UnfoldedCircleaimstoavoidthefateoftheHarmonyUltimatebyeliminatinganyserverobligationsorsubs

Light Phone 3 launches with 50% discount, monochrome OLED and minimalist designLight Phone 3 launches with 50% discount, monochrome OLED and minimalist designJun 13, 2024 pm 10:18 PM

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

Jabra discontinues Elite wireless earbuds lineupJabra discontinues Elite wireless earbuds lineupJun 14, 2024 am 09:39 AM

TheJabraEliteserieshascometoanend,withGNCEOPeterKarlstromerannouncingthediscontinuationofthewirelessearbudswithinthelineupinapressrelease.Petersaysthatthechangingmarketdynamicsandhowchallengingithasbecometogetas

Audio-Technica unveils ATH-S300BT wireless headphones with hear-through noise-cancellation, 90 hours battery life, and multipoint pairingAudio-Technica unveils ATH-S300BT wireless headphones with hear-through noise-cancellation, 90 hours battery life, and multipoint pairingJun 14, 2024 am 09:46 AM

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

First complete PCIe 7.0 IP solution presented by Synopsys coming to market in 2025 for HPCs and AI supercomputersFirst complete PCIe 7.0 IP solution presented by Synopsys coming to market in 2025 for HPCs and AI supercomputersJun 14, 2024 am 09:19 AM

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

Hackers recover $3 million in bitcoin from software wallet locked for over a decadeHackers recover $3 million in bitcoin from software wallet locked for over a decadeJun 14, 2024 am 09:57 AM

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

Realme GT 6 launches next week with newly launched affordable flagship SoCRealme GT 6 launches next week with newly launched affordable flagship SoCJun 13, 2024 pm 10:13 PM

Realmehasofficiallyconfirmedthelaunchdateofitsupcomingaffordableflagship,theGT6.Accordingtothelatestroundofannouncements,thephonewillmakeitsdebutonJune20,whichisaroundthecorner.Thecompanyhasalsosharedagoodnumber

See all articles

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Alat panas

DVWA

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

PhpStorm versi Mac

Alat pembangunan bersepadu PHP profesional terkini (2018.2.1).

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

MinGW - GNU Minimalis untuk Windows

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

ZendStudio 13.5.1 Mac

Persekitaran pembangunan bersepadu PHP yang berkuasa