Page 1 of 1

How to calculate milliseconds?

Posted: Tue Dec 01, 2009 5:07 pm
by polk
Hi everyone,

I have this:

Code: Select all

<?php
 
$start_time = microtime(true);
 
// Do something...
 
$end_time = microtime(true);
 
echo 'Script executed in '. round(($end_time - $start_time), 3) . 'seconds.';
 
 
Now how could I get milliseconds? Since there are 1000 milliseconds in a second, I tried to divide by 1000 but I'm not sure of the result....... :?

Thanks again :oops:

Re: How to calculate milliseconds?

Posted: Tue Dec 01, 2009 6:19 pm
by AlanG

Code: Select all

function utime() {
    return reset(explode(' ', microtime()));
}
 
$start = utime();
 
$end = utime();
 
$difference = $end - $start;
:)

Explanation:

You'll notice if you print the result of the microtime function (echo microtime();), it returns a string such as "0.31732900 1259713502".
The first string is the microseconds. So all I did was 'explode' it into an array and return the first index. :)

Re: How to calculate milliseconds?

Posted: Tue Dec 01, 2009 7:36 pm
by polk
Thank you AlanG for your answer!

But the result of this is in microseconds (there are 1 million microseconds in a second) and I'm trying to get milliseconds (1000 millisecond in a second) with a clean 3 digits result (from 000 to 999)... I can't figure it out :?

Thanks again, your help is really appreciated!

Re: How to calculate milliseconds?

Posted: Tue Dec 01, 2009 7:55 pm
by AlanG
Whoops! :)

You can use the round function to knock off a few digits.

Code: Select all

 
function msec() {
    $uTime = reset(explode(' ', microtime()));
    $mSec = round($uTime, 3); // If you don't want a decimal you could use ceil($uTime * 1000);
 
    return $mSec;
}
 

Re: How to calculate milliseconds?

Posted: Tue Dec 01, 2009 8:06 pm
by polk
no no this is still microseconds :mrgreen: not milliseconds :P

Re: How to calculate milliseconds?

Posted: Tue Dec 01, 2009 8:23 pm
by AlanG
[POST DELETED OUT OF SHAME]

Basic maths is obviously a weakness of mine. :( lol

Yeah John Cartwright is probably right (I guess! I'm no authority on this lol)

Re: How to calculate milliseconds?

Posted: Tue Dec 01, 2009 8:24 pm
by John Cartwright

Code: Select all

list($usec, $sec) = explode(" ",microtime());
$milliseconds = ((float)$usec/1000) + (float)$sec;

Re: How to calculate milliseconds?

Posted: Tue Dec 01, 2009 8:47 pm
by polk
Thanks a lot guys, but it doesn't work... :(

Code: Select all

 
<?php
 
function msec() {
    //$uTime = reset(explode(' ', microtime()));
    //$mSec = ceil($uTime * 1000); // also tried with round($uTime, 3);
 
    list($usec, $sec) = explode(" ",microtime());
    $mSec = ((float)$usec/1000) + (float)$sec; 
 
    return $mSec;
}
 
$start_time = msec();
 
// Do something...
 
$end_time = msec();
 
echo 'Execution time: ' . ($end_time - $start_time) . ' milliseconds.';
 
Is there something wrong with my implementation?
I get crazy results like: 7.4148178100586E-5 :dubious:

Thanks again for your time and patience!

Re: How to calculate milliseconds?

Posted: Tue Dec 01, 2009 9:25 pm
by John Cartwright
The result you were getting is called scientific notation. It's a way of representing an abysmally large (or small) numbers. However, I forgot to multiply the seconds by 1000 in my example.

Re: How to calculate milliseconds?

Posted: Tue Dec 01, 2009 11:19 pm
by polk
Thanks for your reply.

Code: Select all

    function millisecond() {
        list($usec, $sec) = explode(" ",microtime());
        $milliseconds = ((float)$usec/1000) + ((float)$sec*1000); 
 
        return $milliseconds;
    }
 
doesn't seem to work either..
I'm not sure if I understand the logic behind this...

Anyway I don't understand why this doesn't work:

Code: Select all

 
$start_time = microtime(true); 
// Do something...
$end_time = microtime(true);
  
echo 'Script executed in: <br />'
       . round( (($end_time - $start_time)/60), 3) . 'minutes. <br />'
       . round( ($end_time - $start_time), 3) . 'seconds. <br />'
       . round( (($end_time - $start_time)*1000), 3) . 'milliseconds. <br />'
       . round( (($end_time - $start_time)*1000000), 3) . 'microseconds.';
 
 
Why the result doesn't make sense? :?: :|

I would really like to stay with the simple structure of time capture at two different moments, and then calculate the way we want to convert it to the unit we want... this should be the right way to do it, no?

Re: How to calculate milliseconds?

Posted: Wed Dec 02, 2009 10:20 am
by pickle
What result are you getting? It makes sense to me when I used test numbers:

Code: Select all

$start_time = 1.0;
$end_time = 122.11111111;
  
 echo 'Script executed in: <br />'
        . round( (($end_time - $start_time)/60), 3) . 'minutes. <br />'
        . round( ($end_time - $start_time), 3) . 'seconds. <br />'
        . round( (($end_time - $start_time)*1000), 3) . 'milliseconds. <br />'
        . round( (($end_time - $start_time)*1000000), 3) . 'microseconds.';

Code: Select all

Script executed in:
2.019minutes.
121.111seconds.
121111.111milliseconds.
121111111.11microseconds.

Re: How to calculate milliseconds?

Posted: Wed Dec 02, 2009 11:56 am
by Apollo
Polk, try this function:

Code: Select all

function msec() 
{
    list($usec, $sec) = explode(' ',microtime());
    return intval(($usec+$sec)*1000.0);
}
This returns the number (integer) of milliseconds since 1-1-1970.

Note that on 32-bit systems, you'll get overflow (because nowadays it's been more than 2³² millisec since 1-1-1970) so the results will only make sense if you subtract them from eachother and use the difference.

For example:

Code: Select all

$start_time = msec();
// ...do something that takes 1.5 seconds...
$stop_time = msec();
$duration = $start_time - $stop_time;
// Hooray, $duration is now approx. 1500

Re: How to calculate milliseconds?

Posted: Wed Dec 02, 2009 12:20 pm
by polk
Thanks everyone for your help!
pickle wrote:What result are you getting? It makes sense to me when I used test numbers:
Thanks everyone for your help!

Code: Select all

$start_time = 1.0;
$end_time = 122.11111111;
  
 echo 'Script executed in: <br />'
        . round( (($end_time - $start_time)/60), 3) . 'minutes. <br />'
        . round( ($end_time - $start_time), 3) . 'seconds. <br />'
        . round( (($end_time - $start_time)*1000), 3) . 'milliseconds. <br />'
        . round( (($end_time - $start_time)*1000000), 3) . 'microseconds.';

Code: Select all

Script executed in:
2.019minutes.
121.111seconds.
121111.111milliseconds.
121111111.11microseconds.
but you've put these values manually, the question is how to get them automatically? :?
Apollo wrote:Polk, try this function:

Code: Select all

function msec() 
{
    list($usec, $sec) = explode(' ',microtime());
    return intval(($usec+$sec)*1000.0);
}
This returns the number (integer) of milliseconds since 1-1-1970.

Note that on 32-bit systems, you'll get overflow (because nowadays it's been more than 2³² millisec since 1-1-1970) so the results will only make sense if you subtract them from eachother and use the difference.

For example:

Code: Select all

$start_time = msec();
// ...do something that takes 1.5 seconds...
$stop_time = msec();
$duration = $start_time - $stop_time;
// Hooray, $duration is now approx. 1500
Yes it works, thanks! but I'm still looking to understand why my example doesn't. And I'm still looking for a simpler method. :oops:

Thanks everyone!

Re: How to calculate milliseconds?

Posted: Thu Dec 03, 2009 9:45 am
by pickle
I manually entered those numbers so the result would be easier to comprehend. You're almost certainly going to get some form of scientific notation as a result when doing this.

Your code probably works - you're just not understanding what it's outputing.