Home >Backend Development >PHP Tutorial >How Can I Monitor PHP Script Execution Time within the Script Itself?

How Can I Monitor PHP Script Execution Time within the Script Itself?

Barbara Streisand
Barbara StreisandOriginal
2024-11-29 01:09:12471browse

How Can I Monitor PHP Script Execution Time within the Script Itself?

Monitoring Script Execution Time in PHP

In PHP, tracking script execution time is crucial for enforcing the max_execution_time limit. However, is it possible to access this information within the script itself?

Solution

To monitor the wall-clock time (rather than CPU execution time), utilize the following code:

// Start time
$time_start = microtime(true); 

// Script execution
for($i=0; $i<1000; $i++){
 // Perform operations
}

// End time
$time_end = microtime(true);

// Execution time computation
$execution_time = ($time_end - $time_start)/60;

// Output
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';

Note:

This method includes time spent waiting for external resources (e.g., database), which is not considered in the max_execution_time limit.

The above is the detailed content of How Can I Monitor PHP Script Execution Time within the Script Itself?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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