@ 2014-04-28T12:05:51Z <?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";
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.4.11 runtime:69.488048553467Ms Output for 7.4.10 runtime:69.990158081055Ms Output for 7.4.9 runtime:77.065944671631Ms Output for 7.4.8 runtime:84.800958633423Ms Output for 7.4.7 runtime:64.754009246826Ms Output for 7.4.6 runtime:60.418128967285Ms Output for 7.4.5 runtime:72.132110595703Ms Output for 7.4.4 runtime:87.884902954102Ms Output for 7.4.3 runtime:85.407018661499Ms Output for 7.4.2 runtime:72.17001914978Ms Output for 7.4.1 runtime:61.048984527588Ms Output for 7.4.0 runtime:69.114923477173Ms Output for 7.3.23 runtime:62.266826629639Ms Output for 7.3.22 runtime:66.291093826294Ms Output for 7.3.21 runtime:78.227996826172Ms Output for 7.3.20 runtime:62.314987182617Ms Output for 7.3.19 runtime:63.498020172119Ms Output for 7.3.18 runtime:62.221050262451Ms Output for 7.3.17 runtime:82.386016845703Ms Output for 7.3.16 runtime:72.005987167358Ms Output for 7.3.15 runtime:63.870191574097Ms Output for 7.3.14 runtime:62.625885009766Ms Output for 7.3.13 runtime:62.210083007812Ms Output for 7.3.12 runtime:65.573930740356Ms Output for 7.3.11 runtime:120.39995193481Ms Output for 7.3.10 runtime:72.330951690674Ms Output for 7.3.9 runtime:85.041999816895Ms Output for 7.3.8 runtime:72.844982147217Ms Output for 7.3.7 runtime:66.332817077637Ms Output for 7.3.6 runtime:67.387819290161Ms Output for 7.3.5 runtime:82.764148712158Ms Output for 7.3.4 runtime:75.788021087646Ms Output for 7.3.3 runtime:85.2370262146Ms Output for 7.3.2 runtime:77.433109283447Ms Output for 7.3.1 runtime:76.714992523193Ms Output for 7.3.0 runtime:62.502861022949Ms Output for 7.2.34 runtime:61.766147613525Ms Output for 7.2.33 runtime:76.545000076294Ms Output for 7.2.32 runtime:61.45191192627Ms Output for 7.2.31 runtime:71.521997451782Ms Output for 7.2.30 runtime:83.737134933472Ms Output for 7.2.29 runtime:60.465097427368Ms Output for 7.2.28 runtime:71.769952774048Ms Output for 7.2.27 runtime:97.57399559021Ms Output for 7.2.26 runtime:60.997009277344Ms Output for 7.2.25 runtime:64.813852310181Ms Output for 7.2.24 runtime:59.586048126221Ms Output for 7.2.23 runtime:71.135997772217Ms Output for 7.2.22 runtime:73.050975799561Ms Output for 7.2.21 runtime:92.79203414917Ms Output for 7.2.20 runtime:76.743841171265Ms Output for 7.2.19 runtime:76.898097991943Ms Output for 7.2.18 runtime:59.730052947998Ms Output for 7.2.17 runtime:78.366994857788Ms Output for 7.2.16 runtime:68.937063217163Ms Output for 7.2.15 runtime:60.122966766357Ms Output for 7.2.14 runtime:66.134929656982Ms Output for 7.1.10 , 7.2.13 runtime:59.638023376465Ms Output for 7.2.12 runtime:60.811996459961Ms Output for 7.2.11 runtime:75.569868087769Ms Output for 7.2.10 runtime:68.747043609619Ms Output for 7.2.9 runtime:59.708118438721Ms Output for 7.2.8 runtime:59.974908828735Ms Output for 7.2.7 runtime:69.115161895752Ms Output for 7.2.6 runtime:87.306022644043Ms Output for 7.2.5 runtime:79.187154769897Ms Output for 7.2.4 runtime:60.519933700562Ms Output for 7.2.3 runtime:66.035032272339Ms Output for 7.2.2 runtime:69.811105728149Ms Output for 7.2.1 runtime:68.795919418335Ms Output for 7.2.0 runtime:81.309080123901Ms Output for 7.1.33 runtime:59.798955917358Ms Output for 7.1.32 runtime:85.301160812378Ms Output for 7.1.31 runtime:100.09098052979Ms Output for 7.1.30 runtime:78.634023666382Ms Output for 7.1.29 runtime:81.286907196045Ms Output for 7.1.28 runtime:72.779893875122Ms Output for 7.1.27 runtime:73.617935180664Ms Output for 7.1.26 runtime:84.07998085022Ms Output for 7.1.25 runtime:81.408977508545Ms Output for 7.1.24 runtime:69.350004196167Ms Output for 7.1.23 runtime:72.043180465698Ms Output for 7.1.22 runtime:70.197820663452Ms Output for 7.1.21 runtime:71.924924850464Ms Output for 7.1.20 runtime:87.780952453613Ms Output for 7.1.19 runtime:68.91393661499Ms Output for 7.1.18 runtime:70.30200958252Ms Output for 7.1.17 runtime:72.04794883728Ms Output for 7.1.16 runtime:83.666801452637Ms Output for 7.1.15 runtime:66.210985183716Ms Output for 7.1.14 runtime:73.431015014648Ms Output for 7.1.13 runtime:70.708990097046Ms Output for 7.1.12 runtime:81.687927246094Ms Output for 7.1.11 runtime:63.518047332764Ms Output for 7.1.9 runtime:67.014932632446Ms Output for 5.6.37 , 7.1.8 runtime:77.471971511841Ms Output for 7.1.7 runtime:60.60791015625Ms Output for 7.1.6 runtime:58.89105796814Ms Output for 7.1.5 runtime:68.928003311157Ms Output for 7.1.4 runtime:78.320026397705Ms Output for 7.1.3 runtime:65.441131591797Ms Output for 7.1.2 runtime:78.386068344116Ms Output for 7.1.1 runtime:66.404104232788Ms Output for 7.1.0 runtime:69.586038589478Ms Output for 7.0.33 runtime:116.37496948242Ms Output for 7.0.32 runtime:114.21608924866Ms Output for 7.0.31 runtime:104.93898391724Ms Output for 7.0.30 runtime:104.58111763Ms Output for 7.0.29 runtime:113.3029460907Ms Output for 7.0.28 runtime:119.90404129028Ms Output for 7.0.27 runtime:96.311092376709Ms Output for 7.0.26 runtime:109.48610305786Ms Output for 7.0.25 runtime:116.52398109436Ms Output for 7.0.24 runtime:103.19900512695Ms Output for 7.0.23 runtime:107.81407356262Ms Output for 7.0.22 runtime:113.13986778259Ms Output for 7.0.21 runtime:104.55012321472Ms Output for 7.0.20 runtime:104.52604293823Ms Output for 7.0.19 runtime:116.16706848145Ms Output for 7.0.18 runtime:116.19281768799Ms Output for 7.0.17 runtime:107.09190368652Ms Output for 7.0.16 runtime:109.50112342834Ms Output for 7.0.15 runtime:133.76307487488Ms Output for 7.0.14 runtime:112.20097541809Ms Output for 7.0.13 runtime:129.7869682312Ms Output for 7.0.12 runtime:105.91912269592Ms Output for 7.0.11 runtime:114.01987075806Ms Output for 7.0.10 runtime:110.40997505188Ms Output for 7.0.9 runtime:100.60501098633Ms Output for 7.0.8 runtime:109.60507392883Ms Output for 7.0.7 runtime:108.18696022034Ms Output for 7.0.6 runtime:100.35586357117Ms Output for 7.0.5 runtime:110.02779006958Ms Output for 7.0.4 runtime:111.05704307556Ms Output for 7.0.3 runtime:110.28504371643Ms Output for 7.0.2 runtime:110.1770401001Ms Output for 7.0.1 runtime:110.72993278503Ms Output for 7.0.0 runtime:112.47897148132Ms Output for 5.6.40 runtime:96.904993057251Ms Output for 5.6.39 runtime:97.85795211792Ms Output for 5.6.38 runtime:85.830926895142Ms Output for 5.6.36 runtime:77.847957611084Ms Output for 5.6.35 runtime:68.832874298096Ms Output for 5.6.34 runtime:85.219860076904Ms Output for 5.6.33 runtime:66.267967224121Ms Output for 5.6.32 runtime:89.035987854004Ms Output for 5.6.31 runtime:65.372943878174Ms Output for 5.6.30 runtime:75.659036636353Ms Output for 5.6.29 runtime:72.527885437012Ms Output for 5.6.28 runtime:68.961143493652Ms Output for 5.6.27 runtime:83.252191543579Ms Output for 5.6.26 runtime:82.602977752686Ms Output for 5.6.25 runtime:96.395969390869Ms Output for 5.6.24 runtime:87.462902069092Ms Output for 5.6.23 runtime:74.445009231567Ms Output for 5.6.22 runtime:94.676971435547Ms Output for 5.6.21 runtime:71.031808853149Ms Output for 5.6.20 runtime:81.682205200195Ms Output for 5.6.19 runtime:86.621999740601Ms Output for 5.6.18 runtime:73.1041431427Ms Output for 5.6.17 runtime:72.77512550354Ms Output for 5.6.16 runtime:77.920913696289Ms Output for 5.6.15 runtime:74.682950973511Ms Output for 5.6.14 runtime:73.596954345703Ms Output for 5.6.13 runtime:71.253061294556Ms Output for 5.6.12 runtime:73.285102844238Ms Output for 5.6.11 runtime:76.719045639038Ms Output for 5.6.10 runtime:68.29309463501Ms Output for 5.6.9 runtime:96.073150634766Ms Output for 5.6.8 runtime:72.439193725586Ms Output for 5.6.7 runtime:97.815990447998Ms Output for 5.6.6 runtime:81.171989440918Ms Output for 5.6.5 runtime:66.492080688477Ms Output for 5.6.4 runtime:71.108102798462Ms Output for 5.6.3 runtime:75.193881988525Ms Output for 5.6.2 runtime:74.450969696045Ms Output for 5.6.1 runtime:77.29697227478Ms Output for 5.6.0 runtime:68.427085876465Ms Output for 5.5.38 runtime:66.762924194336Ms Output for 5.5.37 runtime:70.181131362915Ms Output for 5.5.36 runtime:69.168090820312Ms Output for 5.5.35 runtime:68.881988525391Ms Output for 5.5.34 runtime:70.689916610718Ms Output for 5.5.33 runtime:71.501016616821Ms Output for 5.5.32 runtime:68.583011627197Ms Output for 5.5.31 runtime:74.460983276367Ms Output for 5.5.30 runtime:79.216003417969Ms Output for 5.5.29 runtime:69.628953933716Ms Output for 5.5.28 runtime:85.453987121582Ms Output for 5.5.27 runtime:82.289934158325Ms Output for 5.5.26 runtime:107.1560382843Ms Output for 5.5.25 runtime:76.722860336304Ms Output for 5.5.24 runtime:78.764915466309Ms Output for 5.5.23 runtime:69.092988967896Ms Output for 5.5.22 runtime:84.104061126709Ms Output for 5.5.21 runtime:77.746868133545Ms Output for 5.5.20 runtime:71.812868118286Ms Output for 5.5.19 runtime:78.654050827026Ms Output for 5.5.18 runtime:89.866876602173Ms Output for 5.5.17 runtime:72.759866714478Ms Output for 5.5.16 runtime:93.656063079834Ms Output for 5.5.15 runtime:96.729040145874Ms Output for 5.5.14 runtime:68.116188049316Ms Output for 5.5.13 runtime:66.892862319946Ms Output for 5.5.12 runtime:76.933145523071Ms Output for 5.5.11 runtime:74.296951293945Ms Output for 5.5.10 runtime:64.520835876465Ms Output for 5.5.9 runtime:71.64192199707Ms Output for 5.5.8 runtime:73.909044265747Ms Output for 5.5.7 runtime:100.27194023132Ms Output for 5.5.6 runtime:67.676782608032Ms Output for 5.5.5 runtime:71.904897689819Ms Output for 5.5.4 runtime:76.575040817261Ms Output for 5.5.3 runtime:109.39788818359Ms Output for 5.5.2 runtime:104.76088523865Ms Output for 5.5.1 runtime:100.93188285828Ms Output for 5.5.0 runtime:116.17398262024Ms Output for 5.4.45 runtime:68.789005279541Ms Output for 5.4.44 runtime:80.142974853516Ms Output for 5.4.43 runtime:78.751087188721Ms Output for 5.4.42 runtime:77.725172042847Ms Output for 5.4.41 runtime:75.972080230713Ms Output for 5.4.40 runtime:74.00107383728Ms Output for 5.4.39 runtime:88.093042373657Ms Output for 5.4.38 runtime:74.591875076294Ms Output for 5.4.37 runtime:67.25001335144Ms Output for 5.4.36 runtime:67.765951156616Ms Output for 5.4.35 runtime:67.653894424438Ms Output for 5.4.34 runtime:69.610118865967Ms Output for 5.4.33 runtime:93.106985092163Ms Output for 5.4.32 runtime:70.539951324463Ms Output for 5.4.31 runtime:92.978000640869Ms Output for 5.4.30 runtime:70.739030838013Ms Output for 5.4.29 runtime:71.170091629028Ms Output for 5.4.28 runtime:68.970918655396Ms Output for 5.4.27 runtime:80.678939819336Ms Output for 5.4.26 runtime:65.760135650635Ms Output for 5.4.25 runtime:70.743083953857Ms Output for 5.4.24 runtime:81.595897674561Ms Output for 5.4.23 runtime:99.112033843994Ms Output for 5.4.22 runtime:87.821006774902Ms Output for 5.4.21 runtime:96.863031387329Ms Output for 5.4.20 runtime:95.214128494263Ms Output for 5.4.19 runtime:83.590030670166Ms Output for 5.4.18 runtime:77.240943908691Ms Output for 5.4.17 runtime:83.50396156311Ms Output for 5.4.16 runtime:68.382978439331Ms Output for 5.4.15 runtime:77.121019363403Ms Output for 5.4.14 runtime:76.930046081543Ms Output for 5.4.13 runtime:108.89601707458Ms Output for 5.4.12 runtime:110.20803451538Ms Output for 5.4.11 runtime:90.206861495972Ms Output for 5.4.10 runtime:117.34008789062Ms Output for 5.4.9 runtime:74.57709312439Ms Output for 5.4.8 runtime:76.292037963867Ms Output for 5.4.7 runtime:78.195095062256Ms Output for 5.4.6 runtime:87.487936019897Ms Output for 5.4.5 runtime:91.670989990234Ms Output for 5.4.4 runtime:74.36203956604Ms Output for 5.4.3 runtime:97.61118888855Ms Output for 5.4.2 runtime:79.653024673462Ms Output for 5.4.1 runtime:71.576118469238Ms Output for 5.4.0 runtime:95.606803894043Ms Output for 5.3.29 runtime:125.99110603333Ms Output for 5.3.28 runtime:133.85009765625Ms Output for 5.3.27 runtime:123.14200401306Ms Output for 5.3.26 runtime:188.39311599731Ms Output for 5.3.25 runtime:153.6819934845Ms Output for 5.3.24 runtime:174.61800575256Ms Output for 5.3.23 runtime:159.37900543213Ms Output for 5.3.22 runtime:145.10917663574Ms Output for 5.3.21 runtime:126.46698951721Ms Output for 5.3.20 runtime:181.40721321106Ms Output for 5.3.19 runtime:157.15098381042Ms Output for 5.3.18 runtime:149.7540473938Ms Output for 5.3.17 runtime:128.96800041199Ms Output for 5.3.16 runtime:141.41821861267Ms Output for 5.3.15 runtime:140.99812507629Ms Output for 5.3.14 runtime:131.12020492554Ms Output for 5.3.13 runtime:119.7772026062Ms Output for 5.3.12 runtime:190.14120101929Ms Output for 5.3.11 runtime:135.29109954834Ms Output for 5.3.10 runtime:136.76714897156Ms Output for 5.3.9 runtime:155.39717674255Ms Output for 5.3.8 runtime:121.09518051147Ms Output for 5.3.7 runtime:142.09914207458Ms Output for 5.3.6 runtime:132.35402107239Ms Output for 5.3.5 runtime:238.95907402039Ms Output for 5.3.4 runtime:163.29121589661Ms Output for 5.3.3 runtime:147.65405654907Ms Output for 5.3.2 runtime:124.90510940552Ms Output for 5.3.1 runtime:127.56705284119Ms Output for 5.3.0 runtime:131.26516342163Ms Output for 5.2.17 runtime:119.86804008484Ms Output for 5.2.16 runtime:142.0681476593Ms Output for 5.2.15 runtime:128.43918800354Ms Output for 5.2.14 runtime:112.79821395874Ms Output for 5.2.13 runtime:112.63108253479Ms Output for 5.2.12 runtime:105.42917251587Ms Output for 5.2.11 runtime:117.57016181946Ms Output for 5.2.10 runtime:129.54211235046Ms Output for 5.2.9 runtime:113.78622055054Ms Output for 5.2.8 runtime:141.35003089905Ms Output for 5.2.7 runtime:109.45916175842Ms Output for 5.2.6 runtime:122.58720397949Ms Output for 5.2.5 runtime:102.93817520142Ms Output for 5.2.4 runtime:130.46312332153Ms Output for 5.2.3 runtime:108.88409614563Ms Output for 5.2.2 runtime:123.67987632751Ms Output for 5.2.1 runtime:117.52605438232Ms Output for 5.2.0 runtime:123.96287918091Ms Output for 5.1.6 runtime:103.87015342712Ms Output for 5.1.5 runtime:88.876008987427Ms Output for 5.1.4 runtime:102.43821144104Ms Output for 5.1.3 runtime:116.12606048584Ms Output for 5.1.2 runtime:97.458839416504Ms Output for 5.1.1 runtime:160.50291061401Ms Output for 5.1.0 runtime:116.48106575012Ms Output for 5.0.5 runtime:205.44791221619Ms Output for 5.0.4 runtime:104.11310195923Ms Output for 5.0.3 runtime:129.67896461487Ms Output for 5.0.2 runtime:135.45513153076Ms Output for 5.0.1 runtime:154.36697006226Ms Output for 5.0.0 runtime:107.84006118774Ms Output for 4.4.9 runtime:85.371971130371Ms Output for 4.4.8 runtime:73.453903198242Ms Output for 4.4.7 runtime:82.474946975708Ms Output for 4.4.6 runtime:94.384908676147Ms Output for 4.4.5 runtime:94.65503692627Ms Output for 4.4.4 runtime:104.77495193481Ms Output for 4.4.3 runtime:102.47397422791Ms Output for 4.4.2 runtime:66.843032836914Ms Output for 4.4.1 runtime:94.951152801514Ms Output for 4.4.0 runtime:146.63887023926Ms Output for 4.3.11 runtime:112.67304420471Ms Output for 4.3.10 runtime:124.74179267883Ms Output for 4.3.9 runtime:128.67307662964Ms Output for 4.3.8 runtime:114.06397819519Ms Output for 4.3.7 runtime:134.78994369507Ms Output for 4.3.6 runtime:97.09095954895Ms Output for 4.3.5 runtime:126.37114524841Ms Output for 4.3.4 runtime:110.69297790527Ms Output for 4.3.3 runtime:89.797973632812Ms Output for 4.3.2 runtime:95.511913299561Ms Output for 4.3.1 runtime:83.969116210938Ms Output for 4.3.0 runtime:84.100961685181Ms preferences:dark mode live preview
259.8 ms | 401 KiB | 353 Q