Current Location: Home> Latest Articles> Is Measuring PHP Script Performance with getrusage() and microtime() Really Reliable?

Is Measuring PHP Script Performance with getrusage() and microtime() Really Reliable?

M66 2025-06-15

In PHP performance tuning, accurately measuring script execution time and resource consumption is essential. A common approach is to use both microtime() and getrusage() functions to monitor the script's runtime and system resource usage. But is this method truly reliable? This article will explore the usage and accuracy of these two functions in detail.

1. Introduction to microtime()

microtime() returns the microsecond part of the current Unix timestamp, commonly used for high-precision timing. A typical usage is to call it once at the beginning of the script and once at the end, subtracting the two values to get the execution time of the script.

<?php
$start = microtime(true);
<p>// Simulate a time-consuming operation<br>
usleep(500000); // 500 milliseconds</p>
<p>$end = microtime(true);<br>
$elapsed = $end - $start;<br>
echo "Script execution time: {$elapsed} seconds\n";<br>
?><br>

The advantage of this approach is its simplicity and directness, and the precision is sufficient to measure the performance of most PHP scripts.

2. Introduction to getrusage()

getrusage() returns an array of resource usage for the current process, including user CPU time, system CPU time, memory usage, etc. It is often used for more granular performance analysis.

<?php
$usage_start = getrusage();
<p>// Simulate a time-consuming operation<br>
usleep(500000);</p>
<p>$usage_end = getrusage();</p>
<p>$user_cpu_start = $usage_start["ru_utime.tv_sec"] + $usage_start["ru_utime.tv_usec"] / 1e6;<br>
$user_cpu_end = $usage_end["ru_utime.tv_sec"] + $usage_end["ru_utime.tv_usec"] / 1e6;</p>
<p>$user_cpu_time = $user_cpu_end - $user_cpu_start;<br>
echo "User CPU time: {$user_cpu_time} seconds\n";<br>
?><br>

By calculating the difference in user CPU time, we can determine the actual time the CPU spent processing the script, excluding waiting times and total runtime.

3. Combining getrusage() and microtime()

Some developers use both functions together to assess script performance:

  • Use microtime() to calculate total elapsed time (wall-clock time)

  • Use getrusage() to calculate CPU time (user time + system time)

Example code:

<?php
$start_time = microtime(true);
$start_usage = getrusage();
<p>// Simulate a time-consuming operation<br>
usleep(500000);</p>
<p>$end_time = microtime(true);<br>
$end_usage = getrusage();</p>
<p>$elapsed_time = $end_time - $start_time;</p>
<p>$user_cpu_start = $start_usage["ru_utime.tv_sec"] + $start_usage["ru_utime.tv_usec"] / 1e6;<br>
$user_cpu_end = $end_usage["ru_utime.tv_sec"] + $end_usage["ru_utime.tv_usec"] / 1e6;<br>
$user_cpu_time = $user_cpu_end - $user_cpu_start;</p>
<p>$system_cpu_start = $start_usage["ru_stime.tv_sec"] + $start_usage["ru_stime.tv_usec"] / 1e6;<br>
$system_cpu_end = $end_usage["ru_stime.tv_sec"] + $end_usage["ru_stime.tv_usec"] / 1e6;<br>
$system_cpu_time = $system_cpu_end - $system_cpu_start;</p>
<p>echo "Total elapsed time: {$elapsed_time} seconds\n";<br>
echo "User CPU time: {$user_cpu_time} seconds\n";<br>
echo "System CPU time: {$system_cpu_time} seconds\n";<br>
?><br>

4. Is This Measurement Method Reliable?

Advantages

  • microtime() measures the real time span, including all waiting and blocking times.

  • getrusage() reflects the actual CPU processing time, excluding time spent waiting for I/O, more accurately representing the script's use of CPU resources.

  • Combining both provides a more comprehensive understanding of script execution efficiency and system load.

Limitations

  • getrusage() is not supported on all operating systems, and the PHP function is not fully supported on Windows systems.

  • getrusage() only measures the resource usage of the current process, and cannot account for resources used by external calls or child processes.

  • If a script has a lot of I/O waiting or network requests, the difference between CPU time and actual elapsed time may be large, and getrusage() will not reflect I/O bottlenecks.

  • microtime() is affected by system clock precision, and very short measurements may not be accurate.

Practical Suggestions

  • For CPU-intensive PHP scripts, combining both methods can provide more accurate performance data.

  • For I/O-intensive scripts, using getrusage() alone is insufficient to reflect overall performance, and microtime() is more important.

  • It is recommended to combine other performance tools (such as Xdebug, Blackfire, etc.) for a more comprehensive analysis.

  • When using getrusage(), be aware of compatibility issues, especially in cross-platform environments.

5. Conclusion

getrusage() and microtime() each have their strengths, and when used together, they can provide a more detailed assessment of PHP script performance. However, their reliability depends on the specific use case, script nature, and testing environment. They are foundational tools for performance analysis, but not a silver bullet. Only by considering business characteristics and more performance metrics can we scientifically evaluate and optimize script performance.