PHP code optimization test [Benchmark data test]

Keywords: PHP Session Linux less

Benchmark
Before testing, let's look at Benchmark.
Direct download: http://pear.php.net/package/Benchmark/download
The Benchmark toolkit has three files, Timer.php, Iterate.php and Profiler.php. The three toolkits have the same functions, but different emphasis. They are all used to debug code acquisition program execution time.
1. The principle of Benchmark_Timer class is the same as that of obtaining microsecond time by microtime function and then comparing the difference between the two time values.
2. The Benchmark_Iterate class is used to debug the average execution time of the function.
3. The Benchmark_Profiler class is used to count the execution time of the code and function and the number of calls to the function.
We use it to test execution results, and to use it we need to install pear

1) $row ['id']= 0 is faster than $row[id]=0, and the greater the number of times, the more obvious it is to test an order of magnitude under Linux.

First, test the case of writing an id directly:

error_reporting(E_ALL&~E_NOTICE );
include "Benchmark/Timer.php";
header("Content-type: text/html; charset=utf-8");

require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate;

function test(){
    for($i = 0 ;$i < 100000; $i++){
        $arr[id] = 0;
    }
}
$bench->run(10,"test");
echo '<pre>';
print_r($bench->get());

Test results:

Array
(
    [1] => 0.140008
    [2] => 0.123007
    [3] => 0.117007
    [4] => 0.121007
    [5] => 0.114006
    [6] => 0.117007
    [7] => 0.112006
    [8] => 0.118007
    [9] => 0.114006
    [10] => 0.114007
    [mean] => 0.119006
    [iterations] => 10
)

And when we change $arr[id] = 0; to

$arr['id'] = 0

The test results are as follows:

Array
(
    [1] => 0.007001
    [2] => 0.008000
    [3] => 0.007001
    [4] => 0.009000
    [5] => 0.008000
    [6] => 0.007001
    [7] => 0.013001
    [8] => 0.008000
    [9] => 0.008001
    [10] => 0.012000
    [mean] => 0.008700
    [iterations] => 10
)

As you can see, the speed of improvement is obvious, so we need to standardize the fields of arrays, not write risky code, $arr[id] is a risky way to write.

2) Increasing (decreasing) a predefined local variable is faster than increasing (decreasing) an undefined local variable; the difference is large.

Or the above code, we change the loop part to:

$arr[$i]++;     //Increment directly without predefined variables

The execution time is:

Array
(
    [1] => 0.011001
    [2] => 0.011001
    [3] => 0.012000
    [4] => 0.012001
    [5] => 0.011001
    [6] => 0.015000
    [7] => 0.013001
    [8] => 0.017001
    [9] => 0.011001
    [10] => 0.014001
    [mean] => 0.012700
    [iterations] => 10
)

Instead, we changed it to the following: ____________

        $arr[$i] = 0;  //Predefined variables were made
        $arr[$i]++;    

Execution time-consuming:

Array
(
    [1] => 0.005000
    [2] => 0.003000
    [3] => 0.003001
    [4] => 0.003000
    [5] => 0.003000
    [6] => 0.003000
    [7] => 0.004000
    [8] => 0.003000
    [9] => 0.003001
    [10] => 0.004000
    [mean] => 0.003400
    [iterations] => 10
)

The time-consuming reduction is considerable.

3) Avoid using regular expressions when feasible. The difference between str_replace function and preg_replace function is obvious.

First, let's look at the case of using regularization:

error_reporting(E_ALL&~E_NOTICE );
require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate;
$str = "2342674120840540640330461206579780032464020461647003497943133406004690797900978525820650";
function test(){
    for($i = 0 ;$i < 10000; $i++){
        $str = preg_replace('/0/','a',$str);
    }
}
$bench->run(10,"test");
echo '<pre>';
print_r($bench->get());

Time consuming:

Array
(
    [1] => 0.020001
    [2] => 0.019001
    [3] => 0.020001
    [4] => 0.017001
    [5] => 0.021001
    [6] => 0.019001
    [7] => 0.018001
    [8] => 0.017001
    [9] => 0.018001
    [10] => 0.021001
    [mean] => 0.019001
    [iterations] => 10
)

And use:

$str = str_replace('0','a',$str);

The time consumed has decreased considerably:

Array
(
    [1] => 0.006000
    [2] => 0.005000
    [3] => 0.005001
    [4] => 0.004000
    [5] => 0.004000
    [6] => 0.004000
    [7] => 0.004001
    [8] => 0.004000
    [9] => 0.004000
    [10] => 0.004000
    [mean] => 0.004400
    [iterations] => 10
)

Similarly, we can extend that we can use PHP functions to complete the functions as far as possible. The bottom c of those functions is optimized and the execution efficiency is relatively high. Namely: try to use PHP built-in functions, and select efficient functions

4) Make use of reference (&) when necessary, and the test will vary greatly, close to an order of magnitude.

If quoted, instead of duplicating more than one variable as a normal passing variable, you can use the address directly.

Let's not quote:

error_reporting(E_ALL&~E_NOTICE );
require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate;
$str = 'wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww';
function setCon1($c){}

function setCon2(&$c){}

function test(){
    for($i = 0 ;$i < 10000; $i++){
        setCon1($str);   //setCon1 is used here, and no reference is used.
    }
}
$bench->run(10,"test");
echo '<pre>';
print_r($bench->get());

Time consuming:

Array
(
    [1] => 0.017001
    [2] => 0.017001
    [3] => 0.017001
    [4] => 0.016001
    [5] => 0.017001
    [6] => 0.016001
    [7] => 0.013001
    [8] => 0.012000
    [9] => 0.011001
    [10] => 0.014001
    [mean] => 0.015000
    [iterations] => 10
)

And when we change in the cycle to:

setCon2($str);

The time is:

Array
(
    [1] => 0.002001
    [2] => 0.001000
    [3] => 0.002000
    [4] => 0.002000
    [5] => 0.001000
    [6] => 0.004000
    [7] => 0.002000
    [8] => 0.002000
    [9] => 0.002000
    [10] => 0.001001
    [mean] => 0.001900
    [iterations] => 10
)

Time has shrunk a lot.

5) Replace time() with $_SERVER['REQUEST_TIME'] when obtaining Unix timestamp; test performance improves a lot

Using the strlen($str) function, the code is as follows:

error_reporting(E_ALL&~E_NOTICE );

require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate;

function test(){
    $str = "Hello World";
    for($i = 0 ;$i < 10000; $i++){
        strlen($str) < 15;
    }
}

$bench->run(10,"test");
echo '<pre>';
print_r($bench->get());

Execution time-consuming:

Array
(
    [1] => 0.202800
    [2] => 0.140400
    [3] => 0.156001
    [4] => 0.140400
    [5] => 0.140400
    [6] => 0.156000
    [7] => 0.140401
    [8] => 0.140400
    [9] => 0.156000
    [10] => 0.140400
    [mean] => 0.151320
    [iterations] => 10
)

And when we use it

isset($str[4]);

Judgment will be much faster and time-consuming to execute:

Array
(
    [1] => 0.000000
    [2] => 0.000000
    [3] => 0.000000
    [4] => 0.000000
    [5] => 0.000000
    [6] => 0.000000
    [7] => 0.015600
    [8] => 0.000000
    [9] => 0.000000
    [10] => 0.000000
    [mean] => 0.001560
    [iterations] => 10
)

6) The $_SERVER ['DOCUMENT_ROOT'] replaces str_replace ('/','/', dirname (_FILE_).'/'); the Wamp test does not make much difference / Linux production environment test performance improves by 500% (5 times)

First, we use $path = str_replace ('/','/', dirname (_FILE_).'/'); test:

error_reporting(E_ALL&~E_NOTICE );

require_once "Benchmark/Iterate.php";
$bench = new Benchmark_Iterate;

function test(){
    for($i = 0 ;$i < 10000; $i++){
        $path = str_replace('//','/',dirname(__FILE__) .'/'); 
    }
}

$bench->run(10,"test");
echo '<pre>';
print_r($bench->get());

Execution time-consuming:

Array
(
    [1] => 0.320001
    [2] => 0.320000
    [3] => 0.320000
    [4] => 0.312002
    [5] => 0.322002
    [6] => 0.310000
    [7] => 0.310001
    [8] => 0.312006
    [9] => 0.322003
    [10] => 0.312002
    [mean] => 0.316001
    [iterations] => 10
)

When changed to:

$path = $_SERVER['DOCUMENT_ROOT'];

Execution time-consuming:

Array
(
    [1] => 0.000000
    [2] => 0.010000
    [3] => 0.000000
    [4] => 0.000000
    [5] => 0.010000
    [6] => 0.000000
    [7] => 0.000000
    [8] => 0.010000
    [9] => 0.000000
    [10] => 0.000000
    [mean] => 0.003000
    [iterations] => 10
)

It's hardly in milliseconds. It takes a lot less time.

 

In fact, there are many other optimization details, such as:

* The foreach function, when no keys are used, does not add keys.

* Use absolute paths as much as possible when you include files, because it avoids the speed of PHP to find files in include_path, and it takes less time to parse operating system paths. [The test difference is actually not obvious]

* Using single quotation marks ('') instead of double quotation marks ("), single quotation marks are of strong type, so all characters are recognized as characters, while double quotation marks are of weak type, it will detect whether there are variables in them. [The test is not very different, but it is risky to use double quotation marks]

* When judging the length of a string, isset($str{15}) can be used instead of strlen ($str) < 15; because isset() is a language structure, and strlen() is a function, the language structure is faster than function.

* Apache handles PHP scripts 2-10 times slower than static pages, so try to use more static pages and fewer scripts; PHP programs use file caching performance will multiply [testing speed is much faster without testing, we know].

* It is generally not recommended to enable auto_start(session.auto_start: whether or not it is automatically enabled) because creating a Session consumes system resources, and we usually use the session_start function to turn on the Session function only when we need to use Sesson.

 

Optimize endlessly....................................................

Posted by abhi_elementx on Fri, 31 May 2019 14:51:40 -0700