@ 2014-04-28T12:06:37Z <?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";
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.4.1 runtime:67.821979522705Ms Output for 7.4.0 runtime:68.046092987061Ms Output for 7.3.13 runtime:68.714141845703Ms Output for 7.3.12 runtime:68.209171295166Ms Output for 7.3.11 runtime:67.355155944824Ms Output for 7.3.10 runtime:45.24302482605Ms Output for 7.3.9 runtime:61.077833175659Ms Output for 7.3.8 runtime:44.600963592529Ms Output for 7.3.7 runtime:48.028945922852Ms Output for 7.3.6 runtime:56.163787841797Ms Output for 7.3.5 runtime:45.408010482788Ms Output for 7.3.4 runtime:45.135021209717Ms Output for 7.3.3 runtime:46.567916870117Ms Output for 7.3.2 runtime:45.298099517822Ms Output for 7.3.1 runtime:60.823917388916Ms Output for 7.3.0 runtime:48.415899276733Ms Output for 7.2.26 runtime:65.614938735962Ms Output for 7.2.25 runtime:66.066980361938Ms Output for 7.2.24 runtime:51.018953323364Ms Output for 7.2.23 runtime:49.22890663147Ms Output for 7.2.22 runtime:45.192956924438Ms Output for 7.2.21 runtime:46.151161193848Ms Output for 7.2.20 runtime:46.83518409729Ms Output for 7.2.19 runtime:62.981128692627Ms Output for 7.2.18 runtime:46.623945236206Ms Output for 7.2.17 runtime:45.374155044556Ms Output for 5.6.40 , 7.2.16 runtime:48.277139663696Ms Output for 7.2.15 runtime:64.173936843872Ms Output for 7.2.14 runtime:45.025110244751Ms Output for 7.2.13 runtime:49.391031265259Ms Output for 7.2.12 runtime:49.426078796387Ms Output for 7.2.11 runtime:45.105934143066Ms Output for 7.2.10 runtime:45.582056045532Ms Output for 7.2.9 runtime:51.964998245239Ms Output for 7.2.8 runtime:45.508146286011Ms Output for 7.2.7 runtime:46.59104347229Ms Output for 7.2.6 runtime:47.077894210815Ms Output for 7.2.5 runtime:47.900915145874Ms Output for 7.2.4 runtime:44.986009597778Ms Output for 7.2.3 runtime:46.54598236084Ms Output for 7.2.2 runtime:44.412851333618Ms Output for 7.2.1 runtime:45.570850372314Ms Output for 7.2.0 runtime:45.37296295166Ms Output for 7.1.33 runtime:45.698165893555Ms Output for 7.1.32 runtime:49.666166305542Ms Output for 7.1.31 runtime:49.900054931641Ms Output for 7.1.30 runtime:51.129102706909Ms Output for 7.1.29 runtime:47.469139099121Ms Output for 7.1.28 runtime:48.563003540039Ms Output for 7.1.27 runtime:50.117969512939Ms Output for 7.1.26 runtime:49.334049224854Ms Output for 7.1.25 runtime:53.607940673828Ms Output for 7.1.24 runtime:45.783042907715Ms Output for 7.1.23 runtime:52.655935287476Ms Output for 7.1.22 runtime:50.386905670166Ms Output for 7.1.21 runtime:47.264099121094Ms Output for 7.1.20 runtime:47.956943511963Ms Output for 7.1.19 runtime:47.311067581177Ms Output for 7.1.18 runtime:51.175117492676Ms Output for 7.1.17 runtime:47.554016113281Ms Output for 7.1.16 runtime:47.089099884033Ms Output for 7.1.15 runtime:47.929048538208Ms Output for 7.1.14 runtime:45.971870422363Ms Output for 7.1.13 runtime:47.508955001831Ms Output for 7.1.12 runtime:48.172950744629Ms Output for 7.1.11 runtime:48.478841781616Ms Output for 7.1.10 runtime:45.94087600708Ms Output for 7.1.9 runtime:46.627044677734Ms Output for 7.1.8 runtime:46.811103820801Ms Output for 7.1.7 runtime:48.766851425171Ms Output for 7.1.6 runtime:46.786069869995Ms Output for 7.1.5 runtime:45.930147171021Ms Output for 7.1.4 runtime:45.926094055176Ms Output for 7.1.3 runtime:52.366971969604Ms Output for 7.1.2 runtime:45.383930206299Ms Output for 7.1.1 runtime:46.376943588257Ms Output for 7.1.0 runtime:47.984838485718Ms Output for 7.0.33 runtime:50.103902816772Ms Output for 7.0.32 runtime:50.767183303833Ms Output for 5.6.8 , 7.0.31 runtime:48.17795753479Ms Output for 7.0.30 runtime:49.323081970215Ms Output for 7.0.29 runtime:51.532030105591Ms Output for 7.0.28 runtime:49.475908279419Ms Output for 7.0.27 runtime:51.944017410278Ms Output for 7.0.26 runtime:53.065061569214Ms Output for 7.0.25 runtime:52.294969558716Ms Output for 7.0.24 runtime:51.468133926392Ms Output for 7.0.23 runtime:51.831007003784Ms Output for 7.0.22 runtime:50.964117050171Ms Output for 7.0.21 runtime:49.819946289062Ms Output for 7.0.20 runtime:49.21293258667Ms Output for 7.0.19 runtime:50.884008407593Ms Output for 7.0.18 runtime:47.842979431152Ms Output for 7.0.17 runtime:52.808046340942Ms Output for 7.0.16 runtime:51.882982254028Ms Output for 7.0.15 runtime:50.865888595581Ms Output for 7.0.14 runtime:49.441814422607Ms Output for 7.0.13 runtime:49.679040908813Ms Output for 7.0.12 runtime:50.523996353149Ms Output for 7.0.11 runtime:53.104877471924Ms Output for 7.0.10 runtime:51.072120666504Ms Output for 7.0.9 runtime:48.120975494385Ms Output for 7.0.8 runtime:47.788143157959Ms Output for 7.0.7 runtime:49.626111984253Ms Output for 7.0.6 runtime:50.589084625244Ms Output for 7.0.5 runtime:52.497148513794Ms Output for 7.0.4 runtime:49.373865127563Ms Output for 7.0.3 runtime:47.529935836792Ms Output for 7.0.2 runtime:50.652980804443Ms Output for 7.0.1 runtime:51.584959030151Ms Output for 7.0.0 runtime:52.31499671936Ms Output for 5.6.39 runtime:50.3089427948Ms Output for 5.6.38 runtime:48.22301864624Ms Output for 5.4.25 , 5.6.37 runtime:49.682855606079Ms Output for 5.6.36 runtime:50.915002822876Ms Output for 5.6.35 runtime:51.211833953857Ms Output for 5.6.34 runtime:48.243999481201Ms Output for 5.6.33 runtime:51.28002166748Ms Output for 5.6.32 runtime:48.7060546875Ms Output for 5.6.31 runtime:57.456016540527Ms Output for 5.6.30 runtime:48.87318611145Ms Output for 5.6.29 runtime:48.211097717285Ms Output for 5.6.28 runtime:47.907829284668Ms Output for 5.6.27 runtime:52.628040313721Ms Output for 5.6.26 runtime:54.940938949585Ms Output for 5.6.25 runtime:59.783935546875Ms Output for 5.6.24 runtime:54.317951202393Ms Output for 5.6.23 runtime:51.001071929932Ms Output for 5.6.22 runtime:53.306818008423Ms Output for 5.6.21 runtime:50.292015075684Ms Output for 5.6.20 runtime:51.400899887085Ms Output for 5.6.19 runtime:51.074981689453Ms Output for 5.6.18 runtime:63.786029815674Ms Output for 5.6.17 runtime:50.204038619995Ms Output for 5.6.16 runtime:52.487850189209Ms Output for 5.6.15 runtime:49.172878265381Ms Output for 5.6.14 runtime:52.290916442871Ms Output for 5.6.13 runtime:50.440073013306Ms Output for 5.6.12 runtime:82.294940948486Ms Output for 5.6.11 runtime:52.330017089844Ms Output for 5.6.10 runtime:47.886848449707Ms Output for 5.6.9 runtime:68.055868148804Ms Output for 5.6.7 runtime:51.680088043213Ms Output for 5.6.6 runtime:64.712047576904Ms Output for 5.6.5 runtime:55.140972137451Ms Output for 5.6.4 runtime:48.725128173828Ms Output for 5.6.3 runtime:50.539970397949Ms Output for 5.6.2 runtime:54.102182388306Ms Output for 5.6.1 runtime:49.86310005188Ms Output for 5.6.0 runtime:52.821159362793Ms Output for 5.5.38 runtime:58.331966400146Ms Output for 5.5.37 runtime:53.280115127563Ms Output for 5.5.36 runtime:56.253910064697Ms Output for 5.5.35 runtime:50.33802986145Ms Output for 5.5.34 runtime:53.042888641357Ms Output for 5.5.33 runtime:54.971933364868Ms Output for 5.5.32 runtime:72.27611541748Ms Output for 5.5.31 runtime:61.064004898071Ms Output for 5.5.30 runtime:59.723854064941Ms Output for 5.5.29 runtime:48.820972442627Ms Output for 5.5.28 runtime:51.672220230103Ms Output for 5.5.27 runtime:51.326036453247Ms Output for 5.5.26 runtime:60.660123825073Ms Output for 5.5.25 runtime:52.506923675537Ms Output for 5.5.24 runtime:48.632144927979Ms Output for 5.5.23 runtime:71.660041809082Ms Output for 5.5.22 runtime:74.812889099121Ms Output for 5.5.21 runtime:50.97508430481Ms Output for 5.5.20 runtime:54.760932922363Ms Output for 5.5.19 runtime:48.369884490967Ms Output for 5.5.18 runtime:49.504995346069Ms Output for 5.5.17 runtime:49.053192138672Ms Output for 5.5.16 runtime:48.834085464478Ms Output for 5.5.15 runtime:62.118053436279Ms Output for 5.5.14 runtime:50.248146057129Ms Output for 5.5.13 runtime:65.485000610352Ms Output for 5.5.12 runtime:49.214124679565Ms Output for 5.5.11 runtime:54.754972457886Ms Output for 5.5.10 runtime:48.738956451416Ms Output for 5.5.9 runtime:50.689935684204Ms Output for 5.5.8 runtime:52.46376991272Ms Output for 5.5.7 runtime:48.86794090271Ms Output for 5.5.6 runtime:49.619913101196Ms Output for 5.5.5 runtime:48.269033432007Ms Output for 5.5.4 runtime:49.19695854187Ms Output for 5.5.3 runtime:53.337097167969Ms Output for 5.5.2 runtime:49.392938613892Ms Output for 5.5.1 runtime:54.192066192627Ms Output for 5.5.0 runtime:50.442934036255Ms Output for 5.4.45 runtime:57.251930236816Ms Output for 5.4.44 runtime:50.983905792236Ms Output for 5.4.28 , 5.4.43 runtime:52.901983261108Ms Output for 5.4.42 runtime:47.960996627808Ms Output for 5.4.41 runtime:49.84188079834Ms Output for 5.4.40 runtime:62.328100204468Ms Output for 5.4.39 runtime:51.099061965942Ms Output for 5.4.38 runtime:48.218011856079Ms Output for 5.4.37 runtime:49.917936325073Ms Output for 5.4.36 runtime:51.198959350586Ms Output for 5.4.35 runtime:51.768064498901Ms Output for 5.4.34 runtime:50.265073776245Ms Output for 5.4.33 runtime:50.227880477905Ms Output for 5.4.32 runtime:53.8489818573Ms Output for 5.4.31 runtime:54.187059402466Ms Output for 5.4.30 runtime:59.125900268555Ms Output for 5.4.29 runtime:52.2301197052Ms Output for 5.4.27 runtime:52.544116973877Ms Output for 5.4.26 runtime:54.69799041748Ms Output for 5.4.24 runtime:50.960063934326Ms Output for 5.4.23 runtime:52.706003189087Ms Output for 5.4.22 runtime:49.48878288269Ms Output for 5.4.21 runtime:51.190137863159Ms Output for 5.4.20 runtime:52.950859069824Ms Output for 5.4.19 runtime:51.589965820312Ms Output for 5.4.18 runtime:49.778938293457Ms Output for 5.4.17 runtime:51.132202148438Ms Output for 5.4.16 runtime:55.388927459717Ms Output for 5.4.15 runtime:51.926136016846Ms Output for 5.4.14 runtime:51.700115203857Ms Output for 5.4.13 runtime:52.231073379517Ms Output for 5.4.12 runtime:52.102088928223Ms Output for 5.4.11 runtime:52.219867706299Ms Output for 5.4.10 runtime:59.888124465942Ms Output for 5.4.9 runtime:51.827907562256Ms Output for 5.4.8 runtime:50.249814987183Ms Output for 5.4.7 runtime:52.165031433105Ms Output for 5.4.6 runtime:49.887895584106Ms Output for 5.4.5 runtime:53.210020065308Ms Output for 5.4.4 runtime:50.733804702759Ms Output for 5.4.3 runtime:49.730062484741Ms Output for 5.4.2 runtime:51.661014556885Ms Output for 5.4.1 runtime:53.536176681519Ms Output for 5.4.0 runtime:52.304029464722Ms Output for 5.3.29 runtime:56.62202835083Ms Output for 5.3.28 runtime:56.948184967041Ms Output for 5.3.27 runtime:58.372020721436Ms Output for 5.3.26 runtime:55.342197418213Ms Output for 5.3.25 runtime:56.310176849365Ms Output for 5.3.24 runtime:52.700042724609Ms Output for 5.3.23 runtime:57.411193847656Ms Output for 5.3.22 runtime:50.600051879883Ms Output for 5.3.21 runtime:54.239988327026Ms Output for 5.3.20 runtime:56.07008934021Ms Output for 5.3.19 runtime:52.942991256714Ms Output for 5.3.18 runtime:55.910110473633Ms Output for 5.3.17 runtime:58.053016662598Ms Output for 5.3.16 runtime:55.148124694824Ms Output for 5.3.15 runtime:55.054187774658Ms Output for 5.3.14 runtime:56.279182434082Ms Output for 5.3.13 runtime:53.18808555603Ms Output for 5.3.12 runtime:55.158138275146Ms Output for 5.3.11 runtime:60.799121856689Ms Output for 5.3.10 runtime:55.505037307739Ms Output for 5.3.9 runtime:50.571203231812Ms Output for 5.3.8 runtime:55.913209915161Ms Output for 5.3.7 runtime:52.127122879028Ms Output for 5.3.6 runtime:56.204080581665Ms Output for 5.3.5 runtime:54.228067398071Ms Output for 5.3.4 runtime:56.177139282227Ms Output for 5.3.3 runtime:56.498050689697Ms Output for 5.3.2 runtime:54.527997970581Ms Output for 5.3.1 runtime:57.847023010254Ms Output for 5.3.0 runtime:54.001092910767Ms 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 preferences:dark mode live preview
78.03 ms | 847 KiB | 5 Q