In PHP, the execution time of the script is important for the purpose of determining the time of formation of the page or its element, for building a safe and reliable site code as a whole.
There are three radically different versions of this goal: the time the entire page was formed, the time the page element was updated, and the execution time was unlimited, including the fact that the visitor ended the session.
Script execution time
In PHP, the execution time of a script is as easy to determine as it was in the first programming languages. By defining timestamps at the beginning and at the end of the code under study, calculating the difference between these values, you can accurately determine how much time it took to execute all the syntactic constructions that are between the final and initial labels.
Simple script: an example of determining the execution time of a cycle.
PHP provides the developer with two functions: microtime () and time (), which can be used to form timestamps. By defining these marks before the beginning of the code under study and upon its completion, you can calculate the difference, that is, get the real time that was spent on code execution.
Microtime () and time () functions
In this example, a different number of iterations is used in the loop: 567 or 56789. In the first case, the loop works faster, in the second - ten times slower.
The microtime () function can be used without parameters, and then its result will be a string of characters of two numbers, separated by a space. The first number is the number of microseconds, the second number is the number of seconds elapsed from the date recognized as the beginning of the Unix era.
If the microtime (true) function is called with the parameter, and its value is true, the result will be a number - as the sum of the number of seconds and microseconds. Using a function without parameters is equivalent to calling: microtime (false).
The time () function does not work with microseconds, because the loop example at fewer iterations gives zero execution time.
The practical value of runtime
PHP is a fast, modern and functionally perfect language. In most cases in PHP, the execution time of the script does not even make sense. But the formation of the page can be performed on the basis of data obtained from the database. In this case, in addition to the delay in selecting information, there may be time spent on data delivery, accounting for server load and other circumstances.
Modern Internet programming is often forced to reckon with the requirements of distributed information processing, the need to collect data from various resources on the Web online. In these and similar cases, evaluating the execution time of a script plays a strategic role and can be of significant practical importance.
In particular, when developing a search system, it may be necessary to systematically view the range of specific network resources for changes made to them over a certain period of time. But some resource may be:
- not available;
- blocked
- be on maintenance.
In these cases, the script will either freeze or wait for the event it needs to be unreasonably long.
By setting the exact time for executing the script, for example, 10 sec., 1 min. or 5 minutes, you can provide your own resource with strict compliance with the rules for processing all remote resources that it needs to solve its problems.
Time and place of script execution
PHP is a server language, but this does not always mean that in PHP the execution time of a script is determined solely by the speed of the server. Only a limited range of tasks requires server processing. Accessing a database or a resource on the Web from a script is going beyond the competence of the server and the additional time that will be spent waiting for the connection to be established and obtaining the necessary data.
The PHP script can be launched using AJAX technology, that is, after a visitor clicks on a page element. This is a common practice when the page is not refreshed entirely, but only its separate element. As a rule, such a decision may be related to the selection of the necessary data. Any delay in execution is critical for the visitor. If time exceeds his expectations, he will simply go to a faster and more comfortable resource.
The developer can estimate how much time the required process takes, and if in PHP the execution time of the script starts to go beyond acceptable limits, he can send an adequate message instead of the response to the visitor. This greatly improves the quality of the resource and is a good solution.