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.';
Thanks again
Moderator: General Moderators
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.';
Code: Select all
function utime() {
return reset(explode(' ', microtime()));
}
$start = utime();
$end = utime();
$difference = $end - $start;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;
}
Code: Select all
list($usec, $sec) = explode(" ",microtime());
$milliseconds = ((float)$usec/1000) + (float)$sec;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.';
Code: Select all
function millisecond() {
list($usec, $sec) = explode(" ",microtime());
$milliseconds = ((float)$usec/1000) + ((float)$sec*1000);
return $milliseconds;
}
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.';
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.Code: Select all
function msec()
{
list($usec, $sec) = explode(' ',microtime());
return intval(($usec+$sec)*1000.0);
}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. 1500but you've put these values manually, the question is how to get them automatically?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.
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.Apollo wrote:Polk, try this function:This returns the number (integer) of milliseconds since 1-1-1970.Code: Select all
function msec() { list($usec, $sec) = explode(' ',microtime()); return intval(($usec+$sec)*1000.0); }
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