The use of dry PHP performance Details _php Tutorial

Source: Internet
Author: User
PHP can be a lot of detail to improve the efficiency of execution, the following is a summary.

Statically called members must be defined as static (PHP5 only)

PHP 5 introduces the concept of static members, which is consistent with the internal static variables of PHP 4, but is used as a member of the class. Static variables are similar to Ruby's class variables (class variable), and instances of all classes share the same static variable.

 
  Bar ();//static Wayfoo::bar ();? >

Static calls to non-static members are more efficient than static calls to the static member 50-60%. Mainly because the former will produce e_strict warning, internal also need to do conversion.

Using class Constants (PHP5 only)

PHP 5 is a new feature, similar to the const C + +.

The benefits of using class constants are:

    • Compile-time resolution with no additional overhead
    • The hash table is smaller, so the internal lookup is faster
    • Class constants exist only in a specific "namespace", so the hash name is shorter
    • Cleaner code to make debugging easier

(temporarily) do not use Require/include_once

Require/include_once will open the target file every time it is called!

If you use the absolute path, PHP 5.2/6.0 does not exist this problem, the new version of the APC cache system has been resolved this problem.

File I/o increase = efficiency is reduced, and if necessary, you can check whether the file has been require/include.

Do not invoke meaningless functions.

Do not use a function when there is a corresponding constant.

 
  

Although the use is not much, but the efficiency increase about 3,500%.

The quickest Win32 check.

 
  
    • No function
    • Win98/nt/2000/xp/vista/longhorn/shorthorn/whistler ... General
    • Always available

Time problem (php>5.1.0 only)

How do you know the current time in your software? Simple, "time () time () again, you ask me...".

But it will always call the function, slow.

Now, with $_server[' Request_time ', you don't have to call the function and save it.

Accelerated PCRE

For the results without saving, do not use (?:). This way PHP does not allocate memory for conforming content, save. Improve efficiency by about 15%.

Can not use regular, do not use the regular, in the analysis of the time carefully read the manual "String function" section. Is there a useful function you missed?

STRPBRK () strncasecmp () Strpos ()/strrpos ()/stripos ()/strripos ()

Accelerated STRTR

If you need to convert all of a single character, use a string instead of an array to do STRTR:

 
  ' E ',                        //...                        )); Bad?>

Efficiency improvement: 10 times times.

Don't make unnecessary substitutions.

Even if there is no substitution, str_replace allocates memory for its parameters. Very slow! Workaround: Use Strpos to find first (very fast), to see if replacement is needed, and then replace if necessary.

If replacement is needed: efficiency is almost equal, the difference is around 0.1%. If you do not need to replace: Use Strpos fast 200%.

The Evil @ operator

Do not abuse the @ operator. Although @ seems simple, there are actually many operations in the background. With @ than without @, efficiency gap: 3 times times.

In particular, do not use @ in the loop, in the 5-cycle test, even if the first use of error_reporting (0) to turn off the error, after the cycle is completed and then open, than with @ faster.

Use strncmp

When you need to compare the "first n characters" is the same time, with strncmp/strncasecmp, not substr/strtolower, not PCRE, not to mention Ereg. STRNCMP/STRNCASECMP is the most efficient (though not much high).

Use Substr_compare with caution (PHP5 only)

According to the above, Substr_compare should be more rapid than the first substr. The answer is no, except that a larger string is compared to a case-insensitive comparison.

Do not use constants instead of strings

Why is it?

    • Hash table needs to be queried two times
    • You need to convert the constant name to lowercase (when you make a second query)
    • Generate E_notice Warning
    • A temporary string is created

Efficiency difference: 700%.

Do not place the count/strlen/sizeof in the conditional statement for the For Loop

My personal approach

 
  

Short code is not necessarily fast

Which one do you think is fast?

-longest:4.27-longer:4.43-short:4.76

Incredible? One more:

 
  Read ())!== false) {if ($entry = = '. ' | | $entry = = ' ... ') {       continue;}} Versusglob ('./* ');//versus (include. and.) Scandir ('. ');? >

Efficiency comparison:

-Original:3.37-glob:6.28-scandir:3.42-original without OO:3.14-SPL (PHP5): 3.95

From then on, we can see that the object-oriented efficiency of PHP5 has been improved a lot, and the efficiency is not much worse than the pure function.

Improve the efficiency of PHP file access

When you need to include other PHP files, use the full path, or the relative path that is easy to convert.

 
  

Best Use

PHP has a lot of extensions and functions available, before you implement a feature, you should see if PHP has this feature? Is there a simpler implementation?

 
  

Tips on quoting

References can simplify access to complex structural data and optimize memory usage.

 
  
 
  

http://www.bkjia.com/PHPjc/752489.html www.bkjia.com true http://www.bkjia.com/PHPjc/752489.html techarticle PHP can be a lot of detail to improve the efficiency of execution, the following is a summary. Static invocation of the member must be defined as static (PHP5 only) PHP 5 introduces the concept of statically member, function and ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.