Home  >  Article  >  Backend Development  >  How Can I Measure PHP Script Execution Time Within the Script Itself?

How Can I Measure PHP Script Execution Time Within the Script Itself?

Barbara Streisand
Barbara StreisandOriginal
2024-11-26 16:03:16585browse

How Can I Measure PHP Script Execution Time Within the Script Itself?

Determining Script Execution Time in PHP

In PHP, the max_execution_time limit mandates that the amount of CPU time utilized by a script be tracked. This article explores if there's a mechanism for accessing this information within the script itself to facilitate logging of PHP CPU usage in test environments.

For Linux systems, a simple method exists to calculate the elapsed wall-clock time (rather than CPU time) of a script:

// Determine the script's starting time (in microseconds)
$time_start = microtime(true);

// Execute the desired script
// (Replace this block with the code you want to track)
for($i=0; $i<1000; $i++){
 // Perform actions
}

// Determine the script's end time (in microseconds)
$time_end = microtime(true);

// Calculate the execution time (default unit: seconds)
$execution_time = $time_end - $time_start;

// Output the execution time in minutes
echo '<b>

Note that this method includes non-PHP execution time, such as waiting for database or disk responses, which is not considered in max_execution_time calculations.

The above is the detailed content of How Can I Measure 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