How to measure the script execution time?

Sometimes I need to know the time spend on performing operations to analyze potential optimization improvements. How do I do this?

I just use super simple calculation that uses microtime function for getting the timestamp at the beginning and on the end of the operation.

$start = microtime(true);

get_posts([
  'post_type' => 'post',
  'posts_per_page' => 1000,
]);

printf(microtime(true) - $start);

Put the code between the functions and you'll get the results. Simple, but really useful! You can save this simple code snippet in your vscode settings and use measure keyword for adding this immediately to your code.

"measure": {
  "prefix": "measure",
  "body": [
    "\\$start = microtime(true);",
    "printf(microtime(true) - \\$start);"
  ]
},
avatar

Looking for a developer who
truly cares about your business?

My team and I provide expert consultations, top-notch coding, and comprehensive audits to elevate your success.

Feedback

How satisfied you are after reading this article?