• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1994
  • Last Modified:

How to measure execution time?

In my MySQL based page I perform series of database queries in very simple manner, not thinking about optimizing. I'd like to measure execution time of this piece of code on server side to detect whether non-optimized code is slowing my page or not.

How do I do it in PHP?
  • 2
1 Solution
Many service-provider offer "benchmarks" for PHP-scripts.

You can do it by yourselft in the following way:

3 pieces of code:

1: take the start time:

$start = microtime();

2: take the end time:

$end = microtime();

3: calculate the difference and echo it:

$start = explode(" ",$start);
$end = explode(" ",$end);
$diff = ($end[0] + $end[1]) - ($start[0] + $start[1]);

echo $diff;

Put the first part before the code you wanna test.
Put the second part after the code you wanna test.
Put the third part after the second part (so the calculation istn counted).

The Value you get is the "execution time" between the $start and the $end in seconds (so for short scripts its normal to get something like "0.00004324234").

Hope that helps, Panther :)
RobsonAuthor Commented:
Thanks! My script takes 0.1 s on average -- quite fast.
with db-accesses? that really fast :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now