The Proper Way to Measure Execution Time of a PHP Script

Hey there!

I’m going to give you a very useful snippet that you can use in all your php projects.

Execution time of a PHP Script is very important for numerous reasons.

The main reason that you should track your php script execution time is because Time matters when programming.

This can help you be more efficient in your programming and make sure your code runs smoothly and fast.

Instagram image from @motivational.coder

How to do it?

The idea is pretty simple, so, no worries!

You will need to define a variable at the very beginning of your project microtime() and one at the end.

After that you will need to subtract the end microtime variable from the start microtime variable and thats it.Also you can set the output value to only show the first 2 decimals.

 

Example

Lets take a quick example of a use case.

You are building a project and something is slowing you down and you want to find out what exactly first.

You can calculate this with the script above to measure the php execution time of your script programatically and start from there.

You can wrap this snippet around any php script and get your execution time for that specific portion.

Here is a good example on what you could do with it:

 

Some tips to improve performance

Here are some tips to improve your overall php scripts performance and get the best out of your project.

  • Make sure you use the latest stable release of PHP
  • Try to use less calls to the Database ( if using a database )
  • Refactor deprecated functions of php with new versions of those functions
  • Stay up to date with PHP and latest functions and methods to use

 

Thank you for reading, make sure to check out my socials below if you want to see more from my side 🙂

 

Grohs Fabian

I'm a 21 year old developer from Romania who is always on the grind. Find me on my Instagram @ Motivational.Coder and on Twitter @ grohsfabian

 

Leave a Reply

Your email address will not be published. Required fields are marked *