How to Measure Script Execution Time in PHP? Last Updated : 10 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Measuring the script execution time in PHP is the time required to execute the PHP script. To calculate script execution time use clock time rather than the CPU execution time. Measuring script execution time is useful to check and improve the execution time for better application performance.Measuring Script Execution using microtime() FunctionThe most commonly used method to measure script execution time in PHP is the microtime() function. This function returns the current Unix timestamp in microseconds, which allows for precise timing of code execution.Example: Measuring script execution timeSample PHP Script php <?php for($i = 1; $i <= 1000; $i++) { echo "Hello Geeks!"; } ?> First use the microtime() function before starts the script and at the end of the script. Then using formula (End_time - Start_time). The microtime() function returns time in seconds. Execution time is not fixed, it depends upon the processor. php <?php // Starting Clock Time in Seconds $start_time = microtime(true); $a = 1; // Start Loop for($i = 1; $i <= 1000; $i++) { $a++; } // End Clock Time in Seconds $end_time = microtime(true); // Calculate the Script Execution Time $execution_time = ($end_time - $start_time); echo "Script Execution Time = " . $execution_time . " sec"; ?> OutputScript Execution Time = 2.1934509277344E-5 secMeasuring Script Execution using hrtime() Function (High-Resolution Timer)PHP 7.3 introduced the hrtime() function, which provides nanosecond-level precision for measuring execution time. This method is ideal for situations where microsecond precision isn't enough, and you need even more detailed performance data.Example: Measuring script execution time with hrtime() function. PHP <?php // Start Measuring Time $start_time = hrtime(true); // Example Script for ($i = 0; $i < 1000000; $i++) { $x = sqrt($i); } // End Measuring Time $end_time = hrtime(true); // Calculate Execution time (in Nanoseconds) $execution_time = $end_time - $start_time; // Convert to Seconds (1 second = 1 billion nanoseconds) $execution_time_in_seconds = $execution_time / 1e9; echo "Execution time: " . $execution_time_in_seconds . " seconds"; ?> OutputExecution time: 0.020944947 seconds Comment More infoAdvertise with us Next Article How to Measure Script Execution Time in PHP? K Komal Agarwal11 Follow Improve Article Tags : Technical Scripter Web Technologies PHP Similar Reads How to increase execution time of a PHP script ? Increasing the execution time of a PHP script means extending the maximum time a script is allowed to run before the server terminates it. This is often necessary for resource-intensive tasks, like data processing or large file uploads, to prevent timeouts.Here we have some common approaches to incr 2 min read How to terminate execution of a script in PHP ? In this article, we are going to discuss how to terminate the execution of a PHP script. In PHP, a coder can terminate the execution of a script by specifying a method/function called the exit() method in a script. Even if the exit() function is called, the shutdown functions and object destructors 2 min read How to Measure Execution Time of Function in R ? In this article, we will learn how to measure the execution or running time of a function in the R programming language. Method 1: Using Sys.time For this first create a sample function that runs for a specific duration. For doing so pass the duration to Sys.sleep() function. Syntax:   startTime 4 min read How to Measure Elapsed Time in Python In Python, we can measure the elapsed time (time taken for a specific task to complete) on executing a code segment or a Python script. It's useful when we are benchmarking the code, debugging or optimizing performance. Python provides several built-in modules to measure the execution time of code b 4 min read How to check the execution time of Python script ? In this article, we will discuss how to check the execution time of a Python script. There are many Python modules like time, timeit and datetime module in Python which can store the time at which a particular section of the program is being executed. By manipulating or getting the difference betwee 6 min read How is it possible to set an infinite execution time for PHP script ? Yes, it is possible to set an infinite execution time for the PHP Script. We can do it by adding the set_time_limit() function at the beginning of the PHP script. The set_time_limit() function takes only one parameter that is int value which is in seconds and it returns a boolean value. If the scrip 2 min read How to Measure Elapsed Time in C++? Measuring elapsed time is a common requirement for most software development packages. It is used to determine the efficiency of the program and gives an idea of which parts of the program takes which much time. This helps in optimizing the code, such that improves its execution time. In this articl 3 min read How to Measure Method Execution Time in Android Debug Build? We use a variety of approaches to apply certain features to our Android applications whenever we create them. We create one method and apply it to all of our projects. These strategies are responsible for growing the number of people who use our software by delivering a variety of unique features. H 3 min read How to Set Time Delay in PHP ? To set a time delay in PHP, you have several approaches depending on your requirements. The time delay is useful for various purposes like creating a pause in script execution or scheduling tasks. These are the following different approaches: Table of Content Using sleep() FunctionUsing usleep() Fun 2 min read Like