PHP tip 4: profiling with time and microtime

If a PHP script is taking too much time to complete, you can isolate the piece of code responsible for the long execution time using time() and microtime().

[et_pb_section fb_built=”1″ _builder_version=”3.22″ fb_built=”1″ _i=”0″ _address=”0″][et_pb_row _builder_version=”3.25″ custom_padding=”27px|0px|27px|0px” _i=”0″ _address=”0.0″][et_pb_column type=”4_4″ _builder_version=”3.25″ custom_padding=”|||” _i=”0″ _address=”0.0.0″ custom_padding__hover=”|||”][et_pb_image src=”” alt=”Profiling with time() and microtime()” title_text=”Profiling with time() and microtime()” align=”center” align_tablet=”center” align_last_edited=”on|desktop” _builder_version=”3.23″ custom_margin=”||5px|” custom_padding=”|||” _i=”0″ _address=”″][/et_pb_image][/et_pb_column][/et_pb_row][et_pb_row _builder_version=”3.25″ custom_padding=”27px|0px|27px|0px” _i=”1″ _address=”0.1″][et_pb_column type=”4_4″ _builder_version=”3.25″ custom_padding=”|||” _i=”0″ _address=”0.1.0″ custom_padding__hover=”|||”][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″ _i=”0″ _address=”″]


[/et_pb_text][et_pb_text _builder_version=”3.7″ _i=”1″ _address=”″]


[/et_pb_text][et_pb_text disabled_on=”on|off|off” _builder_version=”3.7″ _i=”2″ _address=”″]


[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”||0px|” custom_padding=”|||” _i=”3″ _address=”″]


Code profiling is the analysis of various metrics related to code performance and efficiency.

If a PHP script is taking too much time to complete, it’s a good idea to profile the script to find out the cause.


The first thing to do is to isolate the piece of code (or the different pieces of code) responsible for the long execution time.

It could be an unoptimized function working on a large set of data, a slow SQL query, or a wrong iteration going out of control.


A very easy and straightforward way to perform this analysis is to use the time() and microtime() functions.

Both these functions return the current Unix Timestamp. time() returns it in seconds, while microtime() returns a float with microseconds too.


To check the execution time of a piece of code you need to:

  1. Save the current time with time() or with microtime() before the code you want to profile.
  2. After the code, call time() or microtime() again and calculate the difference from the previously saved value.
  3. That is the execution time in seconds 😉

If the execution time is more than a few seconds you can use time(), otherwise it’s usually more useful to use microseconds.


This is a practical example:

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row _builder_version=”3.25″ custom_padding=”27px|0px|27px|0px” _i=”2″ _address=”0.2″][et_pb_column type=”4_4″ _builder_version=”3.25″ custom_padding=”|||” _i=”0″ _address=”0.2.0″ custom_padding__hover=”|||”][et_pb_text _builder_version=”3.27.3″ text_font=”||||||||” text_font_size=”13px” hover_enabled=”0″ _i=”0″ _address=”″]


$start = microtime(TRUE);

/* Start of the code to profile */
for ($a = 0; $a < 10000000; $a++)
	$b = $a*$a;
/* End of the code to profile */

$end = microtime(TRUE);
echo "The code took " . ($end - $start) . " seconds to complete.";

[/et_pb_text][et_pb_text _builder_version=”3.7″ custom_margin=”|||” _i=”1″ _address=”″]




If you have any questions, feel free to ask in the comments below or on my Facebook Group: Alex PHP café.

If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!

[/et_pb_text][et_pb_text _builder_version=”3.2.2″ _i=”2″ _address=”″]



[/et_pb_text][et_pb_code _i=”3″ _address=”″][/et_pb_code][/et_pb_column][/et_pb_row][/et_pb_section]

Leave a Reply

Your email address will not be published. Required fields are marked *