Few notes for PHP Performance Optimization

Source: Internet
Author: User

The following content comes from the network and pro PHP application performance

1. Use require vs require_once

Reference: http://www.laruence.com/2012/09/12/2765.html

Require is faster than require_once due to the high number of operational stat CILS
Made When importing the PHP script. If the file you requested is located within
Directory/var/shared/htdocs/MyApp/models/mymodels/classa. php, the operating system
Will make a stat call within each of the directories before it reaches classa. php. In this
Example, that is a total of six stat cals. While require also issues stat cals, they are fewer
In number. So, the fewer func tion callthere are, the fa ster your code becomes.

Require is faster than require_once because of a large number of stat operations when importing PHP scripts. If you request a file in/var/shared/htdocs/MyApp/models/mymodels/classa. when the file of the PHP address reaches classa. before PHP, the operating system will make a stat call for each directories. Require_once requires six calls. Require also has stat calls, but there are fewer in number. Therefore, it is faster to use it with fewer function calls.

2. Use "comma" instead of "Period"

<?php echo "Hi "."there. "."how are "."you?";  //Slow echo "Hi ","there. ","how are ","you?";  //Faster…slightly 

3. traverse the array. foreach is the fastest, while and for are the second.

Episode: Code not optimized

<?php  $items = array(1,2,3,4,5,6,7,8,9,10); for($i=0; $i<count($items); $i++) {    $x = 10031981 * $i;   }

This Code removes count ($ items) to reduce function calls and computation, improving efficiency.

For, foreach, while code, the most efficient foreach, the reason is not found here

<?php  $items = array_fill(0, 100000, '12345678910');  $start = microtime(); reset($items); foreach($items as $item) {    $x = $item; } echo microtime()-$start; 

4. fread is faster when reading small files. file_get_contents is faster when reading large files. The specific size of the file is Median. You can test the file according to the environment.

5. Read the class property and set the class property to public. The fastest way to directly read the class property is not to mention the reason, you know

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.