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

xiaoxiao2021-03-06  89

Now let's take a look at another method of test script running time - using the BenchMark_TIMER class to test a code execution time and this section of the time during this code and the next call.

Benchmark2.php

Require_once 'Benchmark / Timer.php'; $ TIMER = New Benchmark_timer (); $ TIMER-> Start (); $ TIMER-> SetMarker ('start_myfunction'); for ($ I = 0; $ I <10; $ i ) ) {MyFunction; $ TIMER-> SetMarker ('end_myfunction "; $ TIMER-> Stop (); $ profiling = $ timer-> getprofiling (); echo'

Time Elapsed: '. $ Time -> timeelapsed (' start_myfunction ',' end_myfunction ').'

'

;

echo

'

'; Print_R ($ profile); echo'

'

;

Exit; function

MyFunction

(

$ VAR

) {

Static

$ Counter

=

0

;

// do something

echo

$ Counter

.

'' '

;

}

?>

First, create a Benchmark Timer Object $ Timer. Then call the start () method, indicating that the time is started. The setmaker () method is used to mark the code segment to be tested. The MyFunction () function is called in the loop, indicating the code to be executed (of course, it will not be as simple as it is). Then use the setmarker () method of the $ TIMER object to perform the end point. The analysis information is obtained with getprofiling (). The time to perform consumption is calculated in the timeelapsed () method (as the loop in the example). Finally, use the Print_R () output information to the screen:

0 1 2 3 4 5 6 7 8 9

Time ELAPSED: 0.000594

Array

(

[0] => Array

(

[name] => start [Time] => 1085730111.27175200 [DIFF] => - [TOTAL] => 1085730111.271752

)

[1] => Array

(

[name] => start_myfunction [Time] => 1085730111.27203800 [DIFF] => 0.000286 [Total] => 1085730111.272038)

[2] => Array

(

[name] => end_myfunction [Time] => 1085730111.27263200 [DIFF] => 0.000594 [total] => 1085730111.272632)

[3] => Array

(

[name] => stop [Time] => 1085730111.27271800 [DIFF] => 0.000086 [Total] => 1085730111.272718))

In this way, you can set a large amount of time period tag in your code, get the time consumed during each code, it is easy to see which part of the code affects the running efficiency of the entire program. Then start to improve this code.

With the above two methods, you can find some of the most influencing speeds in your code. It can also be used to test the optimized code, and see how much execution speed is improved. Through Test -> Optimization -> Test -> Optimization ... This continues to loop, you can finalize the code that provides the best efficiency.

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

New Post(0)