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++) { str_replace("strncmp", "[replaced]", $a); } echo $Runtime = "runtime:" . ((array_sum(explode(" ", microtime())) - $start_time) * 1000) . "Ms";
Output for 7.1.0
runtime:38.671016693115Ms
Output for 7.0.14
runtime:58.778047561646Ms
Output for 7.0.6
runtime:61.014890670776Ms
Output for 7.0.5
runtime:53.244113922119Ms
Output for 7.0.4
runtime:59.715986251831Ms
Output for 7.0.3
runtime:63.879013061523Ms
Output for 7.0.2
runtime:61.323165893555Ms
Output for 7.0.1
runtime:57.615995407104Ms
Output for 7.0.0
runtime:63.970804214478Ms
Output for hhvm-3.12.0
runtime:38.406848907471Ms
Output for hhvm-3.10.0
runtime:15.188217163086Ms
Output for 5.6.28
runtime:39.525032043457Ms
Output for 5.6.21
runtime:38.858890533447Ms
Output for 5.6.20
runtime:28.743982315063Ms
Output for 5.6.19
runtime:30.816078186035Ms
Output for 5.6.18
runtime:27.194023132324Ms
Output for 5.6.17
runtime:30.564785003662Ms
Output for 5.6.16
runtime:40.884017944336Ms
Output for 5.6.15
runtime:31.93187713623Ms
Output for 5.6.14
runtime:42.011022567749Ms
Output for 5.6.13
runtime:31.841039657593Ms
Output for 5.6.12
runtime:28.978109359741Ms
Output for 5.6.11
runtime:63.024044036865Ms
Output for 5.6.10
runtime:57.377099990845Ms
Output for 5.6.9
runtime:41.794061660767Ms
Output for 5.6.8
runtime:28.306007385254Ms
Output for 5.6.7
runtime:49.727201461792Ms
Output for 5.5.35
runtime:29.932975769043Ms
Output for 5.5.34
runtime:29.233932495117Ms
Output for 5.5.33
runtime:64.823150634766Ms
Output for 5.5.32
runtime:50.573110580444Ms
Output for 5.5.31
runtime:38.172006607056Ms
Output for 5.5.30
runtime:28.344869613647Ms
Output for 5.5.29
runtime:53.020000457764Ms
Output for 5.5.28
runtime:34.517049789429Ms
Output for 5.5.27
runtime:29.339790344238Ms
Output for 5.5.26
runtime:32.898902893066Ms
Output for 5.5.25
runtime:43.943166732788Ms
Output for 5.5.24
runtime:41.611909866333Ms
Output for 5.4.45
runtime:24.099111557007Ms
Output for 5.4.44
runtime:30.144929885864Ms
Output for 5.4.43
runtime:39.376020431519Ms
Output for 5.4.42
runtime:35.676002502441Ms
Output for 5.4.41
runtime:37.42504119873Ms
Output for 5.4.40
runtime:37.281036376953Ms
Output for 5.4.39
runtime:40.308952331543Ms
Output for 5.4.38
runtime:37.770986557007Ms
Output for 5.4.37
runtime:44.300079345703Ms
Output for 5.4.36
runtime:35.559892654419Ms
Output for 5.4.35
runtime:38.287878036499Ms
Output for 5.4.34
runtime:39.54005241394Ms
Output for 5.4.32
runtime:28.081893920898Ms
Output for 5.4.31
runtime:32.415151596069Ms
Output for 5.4.30
runtime:27.982950210571Ms
Output for 5.4.29
runtime:37.802934646606Ms
Output for 5.4.28
runtime:27.806997299194Ms
Output for 5.4.27
runtime:28.229951858521Ms
Output for 5.4.26
runtime:31.058073043823Ms
Output for 5.4.25
runtime:28.481006622314Ms
Output for 5.4.24
runtime:27.983903884888Ms
Output for 5.4.23
runtime:28.117179870605Ms
Output for 5.4.22
runtime:27.949094772339Ms
Output for 5.4.21
runtime:27.898073196411Ms
Output for 5.4.20
runtime:27.646064758301Ms
Output for 5.4.19
runtime:27.814865112305Ms
Output for 5.4.18
runtime:27.356863021851Ms
Output for 5.4.17
runtime:28.878927230835Ms
Output for 5.4.16
runtime:28.056144714355Ms
Output for 5.4.15
runtime:27.531147003174Ms
Output for 5.4.14
runtime:27.972936630249Ms
Output for 5.4.13
runtime:28.920888900757Ms
Output for 5.4.12
runtime:29.646873474121Ms
Output for 5.4.11
runtime:28.918027877808Ms
Output for 5.4.10
runtime:28.243780136108Ms
Output for 5.4.9
runtime:28.803110122681Ms
Output for 5.4.8
runtime:32.371997833252Ms
Output for 5.4.7
runtime:28.945922851562Ms
Output for 5.4.6
runtime:29.121875762939Ms
Output for 5.4.5
runtime:29.283046722412Ms
Output for 5.4.4
runtime:34.970045089722Ms
Output for 5.4.3
runtime:29.421091079712Ms
Output for 5.4.2
runtime:27.682065963745Ms
Output for 5.4.1
runtime:30.478954315186Ms
Output for 5.4.0
runtime:31.514167785645Ms
Output for 5.3.29
runtime:29.428958892822Ms
Output for 5.3.28
runtime:30.241966247559Ms
Output for 5.3.27
runtime:36.516189575195Ms
Output for 5.3.26
runtime:29.773950576782Ms
Output for 5.3.25
runtime:29.590129852295Ms
Output for 5.3.24
runtime:29.802083969116Ms
Output for 5.3.23
runtime:31.075000762939Ms
Output for 5.3.22
runtime:29.963970184326Ms
Output for 5.3.21
runtime:30.862092971802Ms
Output for 5.3.20
runtime:29.989957809448Ms
Output for 5.3.19
runtime:30.164003372192Ms
Output for 5.3.18
runtime:32.440185546875Ms
Output for 5.3.17
runtime:30.075073242188Ms
Output for 5.3.16
runtime:29.99210357666Ms
Output for 5.3.15
runtime:38.289070129395Ms
Output for 5.3.14
runtime:31.213045120239Ms
Output for 5.3.13
runtime:31.486034393311Ms
Output for 5.3.12
runtime:30.135869979858Ms
Output for 5.3.11
runtime:29.875993728638Ms
Output for 5.3.10
runtime:30.215978622437Ms
Output for 5.3.9
runtime:30.391931533813Ms
Output for 5.3.8
runtime:30.431032180786Ms
Output for 5.3.7
runtime:29.571056365967Ms
Output for 5.3.6
runtime:30.159950256348Ms
Output for 5.3.5
runtime:29.996871948242Ms
Output for 5.3.4
runtime:30.834913253784Ms
Output for 5.3.3
runtime:30.687093734741Ms
Output for 5.3.2
runtime:30.472040176392Ms
Output for 5.3.1
runtime:31.403064727783Ms
Output for 5.3.0
runtime:30.423879623413Ms
Output for 5.2.17
runtime:30.961036682129Ms
Output for 5.2.16
runtime:30.211925506592Ms
Output for 5.2.15
runtime:29.968023300171Ms
Output for 5.2.14
runtime:30.910968780518Ms
Output for 5.2.13
runtime:29.284954071045Ms
Output for 5.2.12
runtime:29.803037643433Ms
Output for 5.2.11
runtime:33.493995666504Ms
Output for 5.2.10
runtime:29.809951782227Ms
Output for 5.2.9
runtime:29.289960861206Ms
Output for 5.2.8
runtime:37.28199005127Ms
Output for 5.2.7
runtime:30.261993408203Ms
Output for 5.2.6
runtime:29.834985733032Ms
Output for 5.2.5
runtime:31.499862670898Ms
Output for 5.2.4
runtime:31.672954559326Ms
Output for 5.2.3
runtime:31.335115432739Ms
Output for 5.2.2
runtime:30.553102493286Ms
Output for 5.2.1
runtime:30.093908309937Ms
Output for 5.2.0
runtime:30.745029449463Ms
Output for 5.1.6
runtime:32.353162765503Ms
Output for 5.1.5
runtime:31.623125076294Ms
Output for 5.1.4
runtime:31.61096572876Ms
Output for 5.1.3
runtime:31.266927719116Ms
Output for 5.1.2
runtime:32.035112380981Ms
Output for 5.1.1
runtime:32.295942306519Ms
Output for 5.1.0
runtime:31.164169311523Ms
Output for 5.0.5
runtime:52.531003952026Ms
Output for 5.0.4
runtime:34.503936767578Ms
Output for 5.0.3
runtime:42.214870452881Ms
Output for 5.0.2
runtime:33.0650806427Ms
Output for 5.0.1
runtime:32.508850097656Ms
Output for 5.0.0
runtime:31.764030456543Ms
Output for 4.4.9
runtime:20.742893218994Ms
Output for 4.4.8
runtime:25.747060775757Ms
Output for 4.4.7
runtime:23.789882659912Ms
Output for 4.4.6
runtime:20.853996276855Ms
Output for 4.4.5
runtime:20.334959030151Ms
Output for 4.4.4
runtime:19.711017608643Ms
Output for 4.4.3
runtime:20.231008529663Ms
Output for 4.4.2
runtime:19.185066223145Ms
Output for 4.4.1
runtime:19.3030834198Ms
Output for 4.4.0
runtime:19.631862640381Ms
Output for 4.3.11
runtime:20.221948623657Ms
Output for 4.3.10
runtime:20.439863204956Ms
Output for 4.3.9
runtime:20.180940628052Ms
Output for 4.3.8
runtime:19.806861877441Ms
Output for 4.3.7
runtime:20.181894302368Ms
Output for 4.3.6
runtime:20.450115203857Ms
Output for 4.3.5
runtime:21.806955337524Ms
Output for 4.3.4
runtime:29.424905776978Ms
Output for 4.3.3
runtime:21.450996398926Ms
Output for 4.3.2
runtime:19.500017166138Ms
Output for 4.3.1
runtime:19.527196884155Ms
Output for 4.3.0
runtime:17.711162567139Ms