3v4l.org

run code in 300+ PHP versions simultaneously
<?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", "", $a); } echo $Runtime = "runtime:". ((Array_sum (explode ("", microtime ())) - $start_time) * 1000). "Ms";

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
7.4.10.0100.10516.20
7.4.00.0070.10716.61
7.3.130.0030.08016.55
7.3.120.0030.11316.32
7.3.110.0100.07616.32
7.3.100.0000.08216.64
7.3.90.0030.09516.32
7.3.80.0070.07316.38
7.3.70.0100.09816.30
7.3.60.0070.09616.43
7.3.50.0030.09416.31
7.3.40.0070.07316.40
7.3.30.0130.09416.42
7.3.20.0100.09718.18
7.3.10.0060.07118.27
7.3.00.0030.07518.31
7.2.260.0030.09216.67
7.2.250.0100.10516.86
7.2.240.0130.06516.40
7.2.230.0070.08016.37
7.2.220.0030.07616.51
7.2.210.0000.07716.61
7.2.200.0030.07916.46
7.2.190.0070.09316.61
7.2.180.0100.08616.59
7.2.170.0030.09416.58
7.2.160.0030.07816.62
7.2.150.0070.08418.50
7.2.140.0030.07218.43
7.2.130.0070.07418.48
7.2.120.0070.09018.31
7.2.110.0030.08918.33
7.2.100.0070.07818.54
7.2.90.0130.09118.49
7.2.80.0070.09918.32
7.2.70.0070.08918.44
7.2.60.0030.07718.44
7.2.50.0130.07118.30
7.2.40.0030.07218.37
7.2.30.0030.07918.25
7.2.20.0100.07118.30
7.2.10.0070.07618.34
7.2.00.0070.07918.29
7.1.330.0060.08117.31
7.1.320.0100.08617.49
7.1.310.0130.10617.41
7.1.300.0030.08917.27
7.1.290.0100.07017.46
7.1.280.0070.07417.30
7.1.270.0070.07317.30
7.1.260.0000.07817.38
7.1.250.0000.10217.32
7.1.240.0000.09317.21
7.1.230.0100.07817.36
7.1.220.0000.07517.36
7.1.210.0060.07917.36
7.1.200.0030.10117.20
7.1.190.0100.07517.34
7.1.180.0030.07417.41
7.1.170.0030.09717.25
7.1.160.0130.07017.40
7.1.150.0030.07917.48
7.1.140.0030.08517.50
7.1.130.0000.08917.18
7.1.120.0030.09217.19
7.1.110.0030.07817.34
7.1.100.0030.09317.34
7.1.90.0070.09817.45
7.1.80.0000.08617.23
7.1.70.0030.06517.14
7.1.60.0100.08118.64
7.1.50.0080.08117.04
7.1.40.0070.08617.19
7.1.30.0070.08517.37
7.1.20.0030.08117.26
7.1.10.0000.09517.09
7.1.00.0050.10519.84
7.0.330.0070.07517.09
7.0.320.0000.08217.02
7.0.310.0000.08917.13
7.0.300.0070.07817.02
7.0.290.0100.07417.08
7.0.280.0130.07317.09
7.0.270.0100.07417.05
7.0.260.0070.08417.07
7.0.250.0030.08316.85
7.0.240.0130.07516.88
7.0.230.0030.08517.07
7.0.220.0030.07817.00
7.0.210.0030.08717.03
7.0.200.0020.10016.87
7.0.190.0070.07616.93
7.0.180.0030.08816.84
7.0.170.0060.07817.07
7.0.160.0070.08817.08
7.0.150.0060.07417.12
7.0.140.0100.11619.57
7.0.130.0000.08017.12
7.0.120.0060.08417.11
7.0.110.0030.07916.95
7.0.100.0070.07617.14
7.0.90.0030.08117.06
7.0.80.0000.08017.06
7.0.70.0030.08116.90
7.0.60.0050.09718.56
7.0.50.0100.10917.33
7.0.40.0050.12717.61
7.0.30.0150.13517.53
7.0.20.0050.10217.56
7.0.10.0180.13917.63
7.0.00.0050.13217.54
5.6.400.0030.05715.84
5.6.390.0070.05816.09
5.6.380.0070.05915.83
5.6.370.0100.07215.88
5.6.360.0070.07615.64
5.6.350.0070.06115.88
5.6.340.0030.06715.55
5.6.330.0000.07115.70
5.6.320.0030.07616.02
5.6.310.0060.05215.90
5.6.300.0030.07215.99
5.6.290.0030.06815.90
5.6.280.0050.08618.48
5.6.270.0030.06316.22
5.6.260.0070.05715.77
5.6.250.0060.05815.61
5.6.240.0100.05116.00
5.6.230.0100.05316.00
5.6.220.0100.06315.83
5.6.210.0030.07818.22
5.6.200.0070.06217.04
5.6.190.0100.05918.25
5.6.180.1700.08718.05
5.6.170.0180.07818.12
5.6.160.0020.07318.02
5.6.150.0080.09116.98
5.6.140.0070.07816.96
5.6.130.0050.10516.96
5.6.120.0080.07518.42
5.6.110.0030.08318.40
5.6.100.0100.06218.40
5.6.90.0100.07618.44
5.6.80.0100.08218.04
5.6.70.0070.05115.76
5.6.60.0000.07715.75
5.6.50.0000.07015.69
5.6.40.0070.05815.87
5.6.30.0030.07015.74
5.6.20.0070.06415.63
5.6.10.0070.07015.81
5.6.00.0070.06315.79
5.5.380.0070.05015.97
5.5.370.0000.05815.81
5.5.360.0100.05815.87
5.5.350.0030.09918.10
5.5.340.0050.07416.74
5.5.330.0070.08918.07
5.5.320.0150.09418.11
5.5.310.0170.08618.07
5.5.300.0070.10816.90
5.5.290.0070.07716.77
5.5.280.0080.06518.35
5.5.270.0070.07318.29
5.5.260.0050.09718.33
5.5.250.0030.10518.26
5.5.240.0050.06317.92
5.5.230.0030.06515.60
5.5.220.0100.05515.76
5.5.210.0070.07315.80
5.5.200.0070.06915.63
5.5.190.0130.06615.70
5.5.180.0030.05415.81
5.5.170.0130.05115.80
5.5.160.0100.07815.69
5.5.150.0030.06115.50
5.5.140.0070.05415.54
5.5.130.0000.05815.52
5.5.120.0070.06915.78
5.5.110.0070.05515.76
5.5.100.0130.04815.82
5.5.90.0070.06915.93
5.5.80.0000.06015.47
5.5.70.0000.05915.88
5.5.60.0000.06115.73
5.5.50.0030.07715.52
5.5.40.0030.07515.64
5.5.30.0100.05215.72
5.5.20.0030.05515.72
5.5.10.0070.05115.66
5.5.00.0070.05015.76
5.4.450.0250.07316.06
5.4.440.0330.07115.97
5.4.430.0750.03816.02
5.4.420.0770.02816.04
5.4.410.0100.07715.82
5.4.400.0070.08415.56
5.4.390.0120.07215.59
5.4.380.0150.07915.69
5.4.370.0170.07615.51
5.4.360.0150.07115.47
5.4.350.0150.07515.64
5.4.340.0180.07415.56
5.4.330.0030.05712.51
5.4.320.0040.07612.57
5.4.310.0040.06512.50
5.4.300.0060.05812.46
5.4.290.0060.06212.59
5.4.280.0070.06812.45
5.4.270.0020.07212.44
5.4.260.0040.07012.43
5.4.250.0040.06612.36
5.4.240.0040.06312.30
5.4.230.0050.06012.46
5.4.220.0090.05812.45
5.4.210.0060.05912.43
5.4.200.0050.06412.39
5.4.190.0030.06012.37
5.4.180.0040.06412.36
5.4.170.0040.06412.42
5.4.160.0080.05812.37
5.4.150.0040.06412.46
5.4.140.0060.06012.25
5.4.130.0050.06212.28
5.4.120.0050.06412.17
5.4.110.0070.06012.28
5.4.100.0070.06812.32
5.4.90.0070.06012.22
5.4.80.0050.06112.31
5.4.70.0050.06012.23
5.4.60.0060.06112.23
5.4.50.0060.06112.26
5.4.40.0050.07212.29
5.4.30.0060.06012.23
5.4.20.0030.06512.22
5.4.10.0060.06012.21
5.4.00.0060.06011.93
5.3.290.0020.06912.98
5.3.280.0040.06912.84
5.3.270.0070.06512.88
5.3.260.0050.06812.91
5.3.250.0040.06712.99
5.3.240.0060.06512.82
5.3.230.0040.06612.78
5.3.220.0080.06312.84
5.3.210.0040.07212.91
5.3.200.0040.06512.71
5.3.190.0070.06112.76
5.3.180.0030.07112.87
5.3.170.0100.07112.92
5.3.160.0060.06712.87
5.3.150.0060.06512.83
5.3.140.0040.06812.83
5.3.130.0060.07912.85
5.3.120.0060.06712.81
5.3.110.0020.06912.81
5.3.100.0050.06912.63
5.3.90.0020.06812.50
5.3.80.0060.07012.43
5.3.70.0040.06712.48
5.3.60.0050.07012.42
5.3.50.0040.07712.41
5.3.40.0010.08012.49
5.3.30.0070.06812.48
5.3.20.0050.06512.38
5.3.10.0040.06412.23
5.3.00.0050.06912.32
5.2.170.0040.0629.18
5.2.160.0070.0589.18
5.2.150.0040.0639.18
5.2.140.0050.0659.17
5.2.130.0030.0629.13
5.2.120.0110.0629.13
5.2.110.0070.0619.14
5.2.100.0060.0599.14
5.2.90.0030.0659.14
5.2.80.0060.0599.13
5.2.70.0070.0599.13
5.2.60.0040.0639.08
5.2.50.0040.0649.05
5.2.40.0090.0569.03
5.2.30.0050.0629.01
5.2.20.0060.0589.23
5.2.10.0040.0649.15
5.2.00.0040.0609.02
5.1.60.0030.0588.30
5.1.50.0040.0588.30
5.1.40.0050.0588.29
5.1.30.0070.0598.63
5.1.20.0050.0728.66
5.1.10.0040.0648.38
5.1.00.0040.0668.38
5.0.50.0050.0586.85
5.0.40.0030.0556.72
5.0.30.0030.0676.54
5.0.20.0030.0546.51
5.0.10.0050.0576.48
5.0.00.0030.0656.48
4.4.90.0020.0524.91
4.4.80.0040.0504.89
4.4.70.0040.0544.88
4.4.60.0040.0504.89
4.4.50.0030.0494.90
4.4.40.0050.0604.89
4.4.30.0030.0494.89
4.4.20.0050.0484.95
4.4.10.0030.0504.95
4.4.00.0050.0624.92
4.3.110.0070.0464.83
4.3.100.0020.0494.82
4.3.90.0070.0454.80
4.3.80.0020.0624.80
4.3.70.0030.0504.80
4.3.60.0020.0514.80
4.3.50.0050.0494.80
4.3.40.0020.0594.76
4.3.30.0020.0503.56
4.3.20.0020.0523.55
4.3.10.0050.0483.29
4.3.00.0000.06313.28

preferences:
63.56 ms | 400 KiB | 5 Q