You are here

22 August 2011

How to test execution time of any part of the code in PHP. Basically you have to start counting microseconds before you code execution and stop after. This snippet shows how to do the counting.

Source code viewer
  1. // Mark the starting point.
  2. $begin = microtime(true);
  3.  
  4. // The code that needs to be tested should be executed.
  5. for ($i = 1; $i <= 10; $i++) {
  6. echo $i;
  7. }
  8.  
  9. // When it's done, print the time difference.
  10. echo microtime(true) - $begin;
Programming Language: PHP