Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Measuring Script Execution Time in PHP
In PHP, measuring script execution time is crucial for performance optimization and debugging. PHP provides several builtin functions to measure how long your code takes to execute, each with different levels of precision.
Using microtime() Function
The most commonly used method for measuring execution time with microsecond precision ?
<?php
// Start the timer
$start = microtime(true);
// Simulate some work
for ($i = 0; $i < 1000000; $i++) {
$dummy = sqrt($i);
}
// End the timer
$end = microtime(true);
// Calculate the execution time
$executionTime = $end - $start;
// Output the result
echo "Script execution time: " . number_format($executionTime, 6) . " seconds";
?>
Script execution time: 0.045123 seconds
Using time() Function
For basic timing with secondlevel precision ?
<?php // Start the timer $start = time(); // Simulate some work sleep(2); // End the timer $end = time(); // Calculate the execution time $executionTime = $end - $start; // Output the result echo "Script execution time: " . $executionTime . " seconds"; ?>
Script execution time: 2 seconds
Using hrtime() Function (PHP 7.3+)
The most precise method available, providing nanosecond accuracy ?
<?php
// Start the timer
$start = hrtime(true);
// Simulate some work
for ($i = 0; $i < 500000; $i++) {
$dummy = $i * $i;
}
// End the timer
$end = hrtime(true);
// Calculate the execution time in seconds
$executionTime = ($end - $start) / 1e9;
// Output the result
echo "Script execution time: " . number_format($executionTime, 9) . " seconds";
?>
Script execution time: 0.012345678 seconds
Measuring Time and Memory Usage
Combine timing with memory usage monitoring for comprehensive performance analysis ?
<?php
// Start measurements
$start = microtime(true);
$startMemory = memory_get_usage();
// Create a large array to demonstrate memory usage
$largeArray = array();
for ($i = 0; $i < 10000; $i++) {
$largeArray[] = str_repeat('x', 100);
}
// End measurements
$end = microtime(true);
$endMemory = memory_get_usage();
// Calculate results
$executionTime = $end - $start;
$memoryUsed = $endMemory - $startMemory;
// Output results
echo "Execution time: " . number_format($executionTime, 4) . " seconds<br>";
echo "Memory used: " . number_format($memoryUsed / 1024, 2) . " KB";
?>
Execution time: 0.0123 seconds Memory used: 976.56 KB
Comparison
| Method | Precision | PHP Version | Best For |
|---|---|---|---|
time() |
1 second | All versions | Longrunning scripts |
microtime(true) |
Microseconds | All versions | General purpose timing |
hrtime(true) |
Nanoseconds | PHP 7.3+ | Highprecision measurements |
Conclusion
Use microtime(true) for most timing needs, hrtime(true) for highprecision requirements, and combine with memory functions for comprehensive performance analysis. Choose the method based on your precision requirements and PHP version compatibility.
