Home >Backend Development >PHP Tutorial >How Can I Measure PHP Script Execution Time?
Getting Script Execution Time in PHP
In PHP, tracking script execution time is crucial for enforcing the max_execution_time limit. But is there a way to access this information from within a script? This question arises when you want to track CPU usage during PHP execution, excluding time spent waiting for database responses.
For Linux-based systems, if you only require wall-clock time (total time since the script's execution started), here's how you can calculate it:
// Start the timer at the beginning of the script $time_start = microtime(true); // Execute your script here // Stop the timer $time_end = microtime(true); // Calculate the execution time $execution_time = ($time_end - $time_start) / 60; // Display the execution time echo 'Total Execution Time: ' . $execution_time . ' Mins';
Please note that this method includes the time spent waiting for external resources, unlike the max_execution_time which only considers PHP execution time.
The above is the detailed content of How Can I Measure PHP Script Execution Time?. For more information, please follow other related articles on the PHP Chinese website!