I recently needed to see how long something ran for in PHP, with better precision than seconds. Looking in the PHP manual, I found a method called microtime(). As I am on PHP4, I could not use the parameter to get results as a floating-point value, so had to convert it myself - I looked through both documentation and comments and saw several ways of doing it. Now, I just picked one and used it, but then I started wondering about speed and precision. I gathered up the various methods in the manual, and made some improvements of my own, and made a script that ran each method in a tight loop 1,000,000 times, storing the microtime() result right before and after, and then calculated the difference as accurately as I could. I also stored what each of the methods gave as result.

This is what I found.

There is a clear difference in the speed of the fastest and slowest method, though noone is really going to notice anything unless they need a very large amount of conversions, and fast.

There is also a difference in the precision of the various methods, most leaving you with just 2 digits after the decimal point, while some keep all the digits.

Now, I realize these are experimental results, and performed on just one computer, so their accuracy can not be guaranteed. The standard use-as-is,no-guarantee-or-warranty disclaimer applies.

I ran the script 10 times, each time appending the output to a file, and then went through the file and cleaned it up to only keep the results, and went over it to see if the order changed. I unfortunately forgot to save the original results file before it was cleaned up, but all the important information is there. The final set of results looks like this:

  substr+dot-2       : total 3.009953      sec, per loop 3.009953E-06      sec, output: '1147800595.20701100'
  substr+dot         : total 3.074087      sec, per loop 3.074087E-06      sec, output: '1147800595.207011'  
* substr+add-2       : total 3.978755      sec, per loop 3.978755E-06      sec, output: '1147800595.21'      
* strtok+add         : total 3.989223      sec, per loop 3.989223E-06      sec, output: '1147800595.21'      
  substr+add         : total 4.085348      sec, per loop 4.085348E-06      sec, output: '1147800595.21'      
  preg_replace       : total 4.179125      sec, per loop 4.179125E-06      sec, output: '1147800595.20701100'
  explode+substr+dot : total 4.616698      sec, per loop 4.616698E-06      sec, output: '1147800595.20701100'
  strpos+substr+dot  : total 4.680859      sec, per loop 4.680859E-06      sec, output: '1147800595.20701100'
  explode+add        : total 5.153213      sec, per loop 5.153213E-06      sec, output: '1147800595.21'      
  explode+array_sum  : total 5.339561      sec, per loop 5.339561E-06      sec, output: '1147800595.21'      
* explode+list+add   : total 5.44088       sec, per loop 5.44088E-06       sec, output: '1147800595.21'      
* explode+float      : total 5.449802      sec, per loop 5.449802E-06      sec, output: '1147800595.21'      
  explode+list+float : total 5.76818       sec, per loop 5.76818E-06       sec, output: '1147800595.21'      
  total              : total 58.765684     sec, per loop 5.8765684E-05     sec
  average            : total 4.52043723077 sec, per loop 4.52043723077E-06 sec

This list is sorted by how long each algorithm ran before it was done, from fastest on top to slowest on the bottom. At the bottom are two non-sorted rows that show the total runtime of all the methods together, and the average runtime.

The first column contains a star or a space, and signifies wether or not that method moved up or down any during the set of 10 runs. There are two pairs of stars, and each pair had its members switch places at least once. It can also be seen from the numbers that the methods in these pairs have very similar runtimes, meaning they are about equal in terms of speed.

The second column states which method the line is about, and can easily be used to find the code for the method in the script file.

The third column shows how many seconds the method ran for, total over the entire loop.
Note that this includes the time spent in executing the looping code as well.

The fourth colum shows how many seconds the method ran for, per iteration - in other words how long it takes to run it once. Note that this is calculated from the third column.

The fifth column shows what the method gives back to us. As can be seen, most of the methods lose some precision, leaving us with two digits behind the decimal point.
Note that this does not really matter much if you are going to be doing mathematics(such as addition and subtraction) on the number, as this precision loss will then take place anyway, unless you're using special functions with more precision than the built-in operators.

If what you want to do is find out how long something took, I recommend not converting to a straight number until after the subtraction, as this loses the least precision. This can be done with this function, as I have done in the script:

function microtime_used($before,$after) {
    return (