run code in 200+ 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", "", $ a); } echo $ Runtime = "runtime:". ((Array_sum (explode ("", microtime ())) - $ start_time) * 1000). "Ms";
Output for 5.4.0 - 5.4.27
Parse error: syntax error, unexpected 'A' (T_STRING), expecting variable (T_VARIABLE) or '$' in /in/9dO7r on line 3
Process exited with code 255.
Output for 5.3.0 - 5.3.28
Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in /in/9dO7r on line 3
Process exited with code 255.