3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $a = <<< EOT Improve the efficiency of php code 2 favorites New one: php programmers easy to commit 10 errors | old one: PHP code optimization The this essay translation finishing brightest from the network documentation (see last reference) in multiple PHP Conference is the Ilia Alshanetsky admiration in speech, mainly all kinds of techniques to improve PHP performance. For the sake of precision, many parts have detailed the efficiency of the data, as well as the corresponding version and so on. Lazy, data is not one given directly to the conclusion, if you need to see the original document, please go to the end "References" section. The orange title for the recommended part. ================================================== ====== Members must be called statically defined as static (PHP5 ONLY) Tips: PHP 5 introduces the concept of static members, the role and function of PHP 4 internal static variables, but the former is as a member of the class to use. Static variables and class variables in Ruby (class variable) almost all instances of a class share the same static variable. Static call non-static member, efficiency 50-60% slower than statically call the static member. Mainly because the former will produce E_STRICT warning, the internal need to do the conversion. Use class constants (PHP5 ONLY) Tips: PHP 5 new features, similar to C + + const. The benefits of using the class constant is: - Resolved at compile time, there is no overhead - The hash table is smaller, faster internal lookup - Class constants exist only in a particular namespace, so the hash name shorter - The code is cleaner and more convenient to make debugging Do not use require / include_once (temporarily) require / include_once each call will open the target file! - If you use absolute path, PHP 5.2/6.0 there is no problem - The new version of the the APC cache system has to solve this problem File I / O => increase in efficiency is reduced If necessary, you can inspect the files yourself is to require / include. Do not call meaningless function Corresponding constant, do not use the function. Although the use of small, but the efficiency about 3500%. The fastest Win32 check - Do not function - Win98/NT/2000/XP/Vista/Longhorn/Shorthorn/Whistler ... General - Has been available A matter of time (PHP> 5.1.0 ONLY) You know how your software now? Simple "time (), time () again, you ask me ...". But I fear, will call the function, slow. Well now, do not call the function, but also save a. Acceleration PCRE - Without saving the results, do not (), all (? :) This PHP do not comply with the contents of the allocated memory, Province. Efficiency about 15%. - Do not have a regular, do not have regular, carefully read the manual "String Functions" section in the analysis. There is no useful function that you missed? For example: The strpbrk () strncasecmp () strpos () / strrpos () / stripos () / strripos () Acceleration strtr If you need to convert all single character string instead of an array do strtr: Efficiency: 10 times. Do not do unnecessary replacement Even if it is not replaced the str_replace also its parameter assignment memory. Very slow! The solution: - Strpos first find (very fast) to see if it needs to be replaced, if necessary, replace Efficiency: - If you need to replace: the efficiency is almost equal to the difference of about 0.1%. - If you do not need to replace: strpos 200% faster. Evil @ operator Do not abuse the @ operator. @ Looks very simple, but in fact there are a lot of background operation. @ @ Than without, the efficiency gap: three times. Especially not in the loop using the @ 5 cycles of testing, even with error_reporting (0) to turn off the error, and then the cycle is complete, open, than fast @. Efficient use of strncmp Need to compare the "before" n characters strncmp / strncasecmp and not substr / pass through strtolower more not PCRE, more 10 million indescribably ereg. strncmp / the strncasecmp efficient (although not much). EOT; $start_time = array_sum(explode(" ", microtime())); for ($i = 0; $i < 10000; $i++) { if (strPos($a, 'strncmp')) { str_replace("strncmp", "[replaced]", $a); } } echo $Runtime = "runtime:" . ((array_sum(explode(" ", microtime())) - $start_time) * 1000) . "Ms";
Output for 7.1.0
runtime:55.577993392944Ms
Output for 7.0.14
runtime:85.348844528198Ms
Output for 7.0.6
runtime:66.995143890381Ms
Output for 7.0.5
runtime:71.197032928467Ms
Output for 7.0.4
runtime:82.274198532104Ms
Output for 7.0.3
runtime:80.568790435791Ms
Output for 7.0.2
runtime:78.650951385498Ms
Output for 7.0.1
runtime:78.3531665802Ms
Output for 7.0.0
runtime:73.514938354492Ms
Output for hhvm-3.12.0
runtime:42.371034622192Ms
Output for hhvm-3.10.0
runtime:26.968002319336Ms
Output for 5.6.28
runtime:60.497045516968Ms
Output for 5.6.21
runtime:57.404041290283Ms
Output for 5.6.20
runtime:50.722122192383Ms
Output for 5.6.19
runtime:38.366079330444Ms
Output for 5.6.18
runtime:39.628028869629Ms
Output for 5.6.17
runtime:44.928789138794Ms
Output for 5.6.16
runtime:60.853958129883Ms
Output for 5.6.15
runtime:50.734996795654Ms
Output for 5.6.14
runtime:63.292026519775Ms
Output for 5.6.13
runtime:57.4951171875Ms
Output for 5.6.12
runtime:43.591976165771Ms
Output for 5.6.11
runtime:53.999185562134Ms
Output for 5.6.10
runtime:87.006092071533Ms
Output for 5.6.9
runtime:50.554037094116Ms
Output for 5.6.8
runtime:79.675912857056Ms
Output for 5.6.7
runtime:45.403003692627Ms
Output for 5.5.35
runtime:54.609060287476Ms
Output for 5.5.34
runtime:70.468187332153Ms
Output for 5.5.33
runtime:50.842046737671Ms
Output for 5.5.32
runtime:64.756155014038Ms
Output for 5.5.31
runtime:68.170070648193Ms
Output for 5.5.30
runtime:62.427997589111Ms
Output for 5.5.29
runtime:55.118083953857Ms
Output for 5.5.28
runtime:45.800924301147Ms
Output for 5.5.27
runtime:79.405069351196Ms
Output for 5.5.26
runtime:53.781986236572Ms
Output for 5.5.25
runtime:55.706977844238Ms
Output for 5.5.24
runtime:41.687965393066Ms
Output for 5.4.45
runtime:39.737224578857Ms
Output for 5.4.44
runtime:44.620990753174Ms
Output for 5.4.43
runtime:39.703845977783Ms
Output for 5.4.42
runtime:41.430950164795Ms
Output for 5.4.41
runtime:48.169136047363Ms
Output for 5.4.40
runtime:39.615869522095Ms
Output for 5.4.39
runtime:39.888143539429Ms
Output for 5.4.38
runtime:48.557043075562Ms
Output for 5.4.37
runtime:40.263891220093Ms
Output for 5.4.36
runtime:44.00897026062Ms
Output for 5.4.35
runtime:44.595003128052Ms
Output for 5.4.34
runtime:44.755220413208Ms
Output for 5.4.32
runtime:40.179014205933Ms
Output for 5.4.31
runtime:44.910907745361Ms
Output for 5.4.30
runtime:39.644002914429Ms
Output for 5.4.29
runtime:50.327062606812Ms
Output for 5.4.28
runtime:45.433044433594Ms
Output for 5.4.27
runtime:47.772169113159Ms
Output for 5.4.26
runtime:52.72102355957Ms
Output for 5.4.25
runtime:50.875902175903Ms
Output for 5.4.24
runtime:54.150104522705Ms
Output for 5.4.23
runtime:53.081035614014Ms
Output for 5.4.22
runtime:56.800842285156Ms
Output for 5.4.21
runtime:51.023960113525Ms
Output for 5.4.20
runtime:46.025991439819Ms
Output for 5.4.19
runtime:56.971073150635Ms
Output for 5.4.18
runtime:57.608127593994Ms
Output for 5.4.17
runtime:52.347898483276Ms
Output for 5.4.16
runtime:44.986009597778Ms
Output for 5.4.15
runtime:47.292947769165Ms
Output for 5.4.14
runtime:42.702913284302Ms
Output for 5.4.13
runtime:55.469036102295Ms
Output for 5.4.12
runtime:49.559831619263Ms
Output for 5.4.11
runtime:44.38304901123Ms
Output for 5.4.10
runtime:40.908098220825Ms
Output for 5.4.9
runtime:48.058986663818Ms
Output for 5.4.8
runtime:40.170192718506Ms
Output for 5.4.7
runtime:54.939985275269Ms
Output for 5.4.6
runtime:59.976100921631Ms
Output for 5.4.5
runtime:52.209854125977Ms
Output for 5.4.4
runtime:58.708906173706Ms
Output for 5.4.3
runtime:57.754993438721Ms
Output for 5.4.2
runtime:51.13697052002Ms
Output for 5.4.1
runtime:50.431966781616Ms
Output for 5.4.0
runtime:40.926933288574Ms
Output for 5.3.29
runtime:46.144962310791Ms
Output for 5.3.28
runtime:59.920072555542Ms
Output for 5.3.27
runtime:45.788049697876Ms
Output for 5.3.26
runtime:62.29305267334Ms
Output for 5.3.25
runtime:47.362804412842Ms
Output for 5.3.24
runtime:57.465791702271Ms
Output for 5.3.23
runtime:44.376134872437Ms
Output for 5.3.22
runtime:54.28409576416Ms
Output for 5.3.21
runtime:46.665191650391Ms
Output for 5.3.20
runtime:42.309045791626Ms
Output for 5.3.19
runtime:60.630083084106Ms
Output for 5.3.18
runtime:59.47208404541Ms
Output for 5.3.17
runtime:56.365966796875Ms
Output for 5.3.16
runtime:61.446905136108Ms
Output for 5.3.15
runtime:49.470186233521Ms
Output for 5.3.14
runtime:61.197996139526Ms
Output for 5.3.13
runtime:62.083005905151Ms
Output for 5.3.12
runtime:58.095932006836Ms
Output for 5.3.11
runtime:60.215950012207Ms
Output for 5.3.10
runtime:52.732944488525Ms
Output for 5.3.9
runtime:60.468912124634Ms
Output for 5.3.8
runtime:44.962167739868Ms
Output for 5.3.7
runtime:51.964044570923Ms
Output for 5.3.6
runtime:64.380168914795Ms
Output for 5.3.5
runtime:51.186084747314Ms
Output for 5.3.4
runtime:42.870044708252Ms
Output for 5.3.3
runtime:62.242031097412Ms
Output for 5.3.2
runtime:47.429084777832Ms
Output for 5.3.1
runtime:62.412023544312Ms
Output for 5.3.0
runtime:59.180021286011Ms
Output for 5.2.17
runtime:65.144062042236Ms
Output for 5.2.16
runtime:55.661201477051Ms
Output for 5.2.15
runtime:59.775829315186Ms
Output for 5.2.14
runtime:42.448043823242Ms
Output for 5.2.13
runtime:53.936958312988Ms
Output for 5.2.12
runtime:58.946132659912Ms
Output for 5.2.11
runtime:63.508987426758Ms
Output for 5.2.10
runtime:64.200162887573Ms
Output for 5.2.9
runtime:59.01312828064Ms
Output for 5.2.8
runtime:59.524059295654Ms
Output for 5.2.7
runtime:60.828924179077Ms
Output for 5.2.6
runtime:57.905912399292Ms
Output for 5.2.5
runtime:53.351879119873Ms
Output for 5.2.4
runtime:45.680999755859Ms
Output for 5.2.3
runtime:50.246000289917Ms
Output for 5.2.2
runtime:50.60601234436Ms
Output for 5.2.1
runtime:53.844928741455Ms
Output for 5.2.0
runtime:40.892124176025Ms
Output for 5.1.6
runtime:53.261995315552Ms
Output for 5.1.5
runtime:60.014009475708Ms
Output for 5.1.4
runtime:54.513931274414Ms
Output for 5.1.3
runtime:63.850164413452Ms
Output for 5.1.2
runtime:43.41197013855Ms
Output for 5.1.1
runtime:42.487144470215Ms
Output for 5.1.0
runtime:47.198057174683Ms
Output for 5.0.5
runtime:71.460008621216Ms
Output for 5.0.4
runtime:64.639091491699Ms
Output for 5.0.3
runtime:64.625978469849Ms
Output for 5.0.2
runtime:68.435907363892Ms
Output for 5.0.1
runtime:44.392108917236Ms
Output for 5.0.0
runtime:44.652938842773Ms
Output for 4.4.9
runtime:56.22410774231Ms
Output for 4.4.8
runtime:59.247970581055Ms
Output for 4.4.7
runtime:41.065216064453Ms
Output for 4.4.6
runtime:38.926839828491Ms
Output for 4.4.5
runtime:48.163890838623Ms
Output for 4.4.4
runtime:48.925876617432Ms
Output for 4.4.3
runtime:37.055015563965Ms
Output for 4.4.2
runtime:32.478094100952Ms
Output for 4.4.1
runtime:54.14891242981Ms
Output for 4.4.0
runtime:45.667886734009Ms
Output for 4.3.11
runtime:55.5260181427Ms
Output for 4.3.10
runtime:57.379961013794Ms
Output for 4.3.9
runtime:40.98916053772Ms
Output for 4.3.8
runtime:54.193019866943Ms
Output for 4.3.7
runtime:47.691106796265Ms
Output for 4.3.6
runtime:58.948993682861Ms
Output for 4.3.5
runtime:56.453943252563Ms
Output for 4.3.4
runtime:33.600091934204Ms
Output for 4.3.3
runtime:38.750886917114Ms
Output for 4.3.2
runtime:54.58402633667Ms
Output for 4.3.1
runtime:65.031051635742Ms
Output for 4.3.0
runtime:54.564952850342Ms