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

5 essential commands to interact with an online git repository: push, pull, clone, fetch, remote (video)
Nov 27, 2022 programming

5 essential commands to interact with an online git repository: push, pull, clone, fetch, remote (video)

5 commands to interact with and online git repository ...

13 Min Read Read More
PHP array map
Feb 14, 2023 programming

PHP array map

Today we are going to learn about array map function in PHP. It lets you apply a function to the elements of the given arrays and returns an array with changed elements. ...

4 Min Read Read More
PHP simple REST API
Feb 15, 2023 programming

PHP simple REST API

Today we are going to create a simple REST API in PHP. we are going to do it with pure PHP and with no frameworks. you can create a RESTful API in 70 lines of code. ...

25 Min Read Read More
PHP array filter
Feb 14, 2023 programming

PHP array filter

Today we are going to talk about array filter in PHP. array filter is a very useful function and it helps you filter an array by key, value or both. ...

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