Home >Backend Development >PHP Tutorial >How Can I Measure PHP Script Execution Time?

How Can I Measure PHP Script Execution Time?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-26 08:39:09604browse

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!

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