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

Ultimate guide to RESTFul API Versioning in PHP Laravel: 2 solutions
Feb 15, 2023 programming

Ultimate guide to RESTFul API Versioning in PHP Laravel: 2 solutions

API versioning is what you need if you want to change the behavior of your API or the structure and format of requests and responses. ...

17 Min Read Read More
PHP Exec
Mar 22, 2023 programming

PHP Exec

Today we are going to talk about exec in PHP. Did you know we can run system commands through PHP? Well not only it’s possible but also it’s very easy. You can do it with exec function ...

3 Min Read Read More
PHP Method Chaining
Mar 22, 2023 programming

PHP Method Chaining

Today we are going to talk about chaining methods in PHP. It’s cleaner and more readable and I don’t have to refer to my object every time I want to run one of the methods. ...

7 Min Read Read More
PHP Password Hashing
Mar 22, 2023 programming

PHP Password Hashing

Today we are going to talk about hashing passwords in PHP. Storing and managing passwords is very important. And in PHP it’s very easy. ...

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