PHP program accelerated exploration [4] - Test - Script Perform Speed ​​Test

xiaoxiao2021-03-06  95

◆ Script execution speed test

As mentioned earlier, we can optimize only if you find the code that affects the speed. The Benchmark_timer class in the PEAR Benchmark package and the Benchmark_Iterate class can be used to easily test the speed of the script execution. (Please check the relevant information on the installation and configuration of PEAR)

First use the Benchmark_Iiterate class to test the execution time of a certain method of a function or class in the program.

Benchmark1.php

Require_once ('Benchmark / Iterate.php'); $ Benchmark = New Benchmark_iterate (); $ Benchmark-> Run (10, 'MyFunction "; $ results = $ benchmark-> get (); echo"

"; Print_R ($ Result); Echo"

"

;

Exit; function

MyFunction

(

$ VAR

) {

// do something

echo

'Hello'

;

}

?>

Establish a Benchmark Iterate Object $ Benchmark, this object is used to perform a MyFunction function 10 times.

The $ argument variable is passed to myFunction. Multiple operational analysis results are deposited in $ Result, and then take it with the get () method of the Benchmark object. This result is output to the screen with Print_R (). This is usually output:

Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello

Array

(

[1] => 0.000427 [2] => 0.000079 [3] => 0.000072 [4] => 0.000071 [5] => 0.000076 [6] => 0.000070 [7] => 0.000073 [8] => 0.000070 [9 ] => 0.000074 [10] => 0.000072 [mean] => 0.000108 [ity] => 10)

Every time myFunction is executed, the Benchmark object will track execution time. And calculate the average execution time ([mean]). You can get the average running time of this function by running the target function multiple times.

In actual testing, the number of functions should be around at least 1000 times, which can get a more objective result.

转载请注明原文地址:https://www.9cbs.com/read-123845.html

New Post(0)