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

PHP Include and Require
Mar 21, 2023 programming

PHP Include and Require

Today we are going to learn about include and require in PHP. they help you organize your code by keeping your code in different files and use them whenever you need without duplicating your code. ...

6 Min Read Read More
PHP range
Feb 15, 2023 programming

PHP range

Today we are going to talk about range function in PHP. It's a very simple yet very powerful tool and can save you a lot of time. it's also more readable than the alternatives. ...

3 Min Read Read More
PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

7 Min Read Read More
PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications
Jul 17, 2025 programming

PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications

PHP is often called the engine room of the modern web. Despite debates about newer technologies like Node.js, Go, or Python’s Django, PHP has remained a consistent favorite among developers. In fact, as of 2025, PHP powers over 75% of all websites that use a known server-side programming language. ...

9 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 ...