Free cookie consent management tool by TermsFeed Generator PHP execution time | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP execution time

PHP execution time

Last Updated on Feb 15, 2023

Microtime

Sometimes it’s a good idea to calculate the execution time of your functions and script as a whole.

Especially for debugging slow scripts, if you calculate the execution time of each function you can analyze which function should be improved.

It’s very easy to do

  1. Before your function starts you store the microtime()
  2. Make sure you set true as the argument
  3. After the function ends you store the microtime again
  4. And then you have your execution time 

Let me show you an example:

function hello(){
     sleep(2); // wait for 2 seconds
     echo "hello world";
}

$start = microtime(true);
hello();
// hello world
$end = microtime(true);

$executionTime = $end - $start;
echo 'running the function took '. $executionTime;
// running the function took 2.0002160072327

Conclusion

Now you know about calculating the execution time of a script in PHP.

I recommend you to open a PHP files and write some functions. then try to calculate how long each one of them take to run.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • calculate execution time
  • microtime

Category: programming

Tags: #php #tips and tricks

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Deploy PHP to Heroku
Feb 14, 2023 programming

Deploy PHP to Heroku

Today we are going to talk about deploying your PHP on heroku. Having your code on your local machine is one thing and deploying it on a server and sharing it with the world is another thing. ...

7 Min Read Read More
PHP Array Search
Feb 14, 2023 programming

PHP Array Search

Today we are going to talk about array search function in PHP. array search is a very useful function that helps you find the key of a value you’re looking for. ...

3 Min Read Read More
PHP Array Intersect
Feb 14, 2023 programming

PHP Array Intersect

Today we are going to talk about array intersect functions in PHP .If you want to find the items in your array that are present in the other arrays, array intersect is your friend. ...

5 Min Read Read More
A Programmer's Guide to Debugging: Essential Steps to Follow
Mar 23, 2024 programming

A Programmer's Guide to Debugging: Essential Steps to Follow

Debugging is a crucial skill for any programmer, as it helps identify and fix issues in the code. Effective debugging not only improves the overall quality of your software but can also save you time and frustration. ...

10 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...