Home > Backend Development > PHP Tutorial > PHP Program Acceleration Exploration Code Optimization_PHP Tutorial

PHP Program Acceleration Exploration Code Optimization_PHP Tutorial

WBOY
Release: 2016-07-20 11:05:27
Original
812 people have browsed it

After mastering PEAR::BenchMark, now you know how to test your code, know how to determine whether your code is fast or slow, and which part is slower. So what I want to talk about next is how to eliminate or optimize that part of the slow code.

There are only two main points of my personal experience at this point. One is to eliminate wrong or inefficient loops; the other is to optimize database query statements. In fact, there are some other optimization details, such as "str_replace is faster than ereg_replace", "echo is faster than print" and so on. I will put these aside for now, and I will mention using caching to deal with too frequent IO later.

Next we compare the efficiency (time consumed) of three functions with the same function but different program writing methods.

badloops.php

require_once('Benchmark/Iterate.php');
define('MAX_RUN',100);
$data = array(1, 2, 3, 4, 5);

doBenchmark('v1', $data);
doBenchmark('v2', $data);
doBenchmark('v3 ', $data);
function doBenchmark($functionName = null, $arr = null)
{
reset($arr);
$benchmark = new Benchmark_Iterate;
$benchmark- >run(MAX_RUN, $functionName, $arr);
$result = $benchmark->get();
echo '
';
printf("%s ran %d times where average exec time %.5f ms",$functionName,$result['iterations'],$result['mean'] * 1000);
}

function v1($myArray = null ) {
 // Very inefficient loop
for ($i =0; $i < sizeof($myArray); $i )
{
echo ' ';
 }
}

function v2($myArray = null) {
 // Slightly improved efficiency
$max = sizeof($myArray);
for ($i =0; $i < $max ; $i )
{
echo ' ';
}
}

function v3($myArray = null){
 //Best efficiency
echo " ";
}

?>

The result of the program output It probably looks like this:

v1 ran 100 times where average exec time 0.18400 ms
v2 ran 100 times where average exec time 0.15500 ms
v3 ran 100 times where average exec time 0.09100 ms

It can be seen that the execution time of the function is reduced and the efficiency is increased.

Function v1 has an obvious error. The sizeof() function needs to be called to calculate the time of each loop. Function v2 stores the number of elements of the $myArray array into the $max variable outside the loop, avoiding the need to calculate the number of elements of the array in each loop, so the efficiency is improved. Function v3 is the most efficient, using existing functions to avoid loops.

This example just gives you a perceptual familiarity and understanding of what is relatively efficient code. In actual development, I believe that many people will vaguely write a lot of inefficient code. To write code that is concise and efficient, I'm afraid it takes time to practice :-) But this is another topic, let's skip it.

Database applications are basically used in every PHP program. In actual development, I found that the database part has the greatest impact on the efficiency of the entire system.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445122.htmlTechArticleAfter mastering PEAR::BenchMark, now you know how to test your code and how to determine whether your code is Fast or slow, which part is slower. So what I want to say next is...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template