rocky_88 Posted April 15, 2011 Share Posted April 15, 2011 I am trying to run tests on my pages and see how much time each one of em take to execute. For this purpose, i'm using this code which i got online: <?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; ?> <!-- Rest of the php and validation and form elements --> <?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "This page was created in ".$totaltime." seconds"; ?> microtime() returns microseconds & secs since Unix epoch. So after exploding, why do we add them both? Cant we just use the microseconds part? I've tried this code & the one above & both return same time(almost). <?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[0]; $starttime = $mtime; ?> <!-- Rest of the php and validation and form elements --> <?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); echo "This page was created in ".$totaltime." seconds"; ?> Quote Link to comment Share on other sites More sharing options...
analog Posted April 15, 2011 Share Posted April 15, 2011 If you only used the microseconds part and your script took longer than a second to execute it would give the wrong result wouldn't it? But why are you exploding the output at all? Why not just do: // From of the PHP manual: <?php $time_start = microtime(true); ?> //rest of code <?php $time_end = microtime(true); $time = $time_end - $time_start; echo "This page was created in {$time} seconds";?> Quote Link to comment Share on other sites More sharing options...
rocky_88 Posted April 15, 2011 Author Share Posted April 15, 2011 If you only used the microseconds part and your script took longer than a second to execute it would give the wrong result wouldn't it? I'm not sure if it would show a wrong result, I tried a small code which executed in way less than one second. But why are you exploding the output at all? Why not just do: // From of the PHP manual: <?php $time_start = microtime(true); ?> //rest of code <?php $time_end = microtime(true); $time = $time_end - $time_start; echo "This page was created in {$time} seconds";?> Thanks for the code, i got that previous code from google. Quote Link to comment Share on other sites More sharing options...
analog Posted April 15, 2011 Share Posted April 15, 2011 I think the microseconds part is how many microseconds have elapsed since the seconds part. So if you excluded the seconds and the script took more than 1 second it would be out by however many full seconds it took to execute. I'm not completely sure but I think that's what would happen. Quote Link to comment Share on other sites More sharing options...
rocky_88 Posted April 15, 2011 Author Share Posted April 15, 2011 I think the microseconds part is how many microseconds have elapsed since the seconds part. So if you excluded the seconds and the script took more than 1 second it would be out by however many full seconds it took to execute. I'm not completely sure but I think that's what would happen. Oh, that could be it. But I assumed that its just the microseconds and seconds since unix epoch, even i'm not sure if its microseconds since the seconds. However the other code u gave also works perfectly. But I just want make sure which one of these would give accurate results IF the execution time goes beyond one second. Quote Link to comment Share on other sites More sharing options...
analog Posted April 15, 2011 Share Posted April 15, 2011 Just looked it up in the manual and it says this: By default, microtime() returns a string in the form "msec sec", where sec is the current time measured in the number of seconds since the Unix epoch (0:00:00 January 1, 1970 GMT), and msec is the number of microseconds that have elapsed since sec expressed in seconds. So only the first would be accurate. You can use sleep to check it by making it sleep for more than 1 second. I would use the code from the manual though that way you don't need to explode and then add the results together. Quote Link to comment Share on other sites More sharing options...
rocky_88 Posted April 15, 2011 Author Share Posted April 15, 2011 Just looked it up in the manual and it says this: By default, microtime() returns a string in the form "msec sec", where sec is the current time measured in the number of seconds since the Unix epoch (0:00:00 January 1, 1970 GMT), and msec is the number of microseconds that have elapsed since sec expressed in seconds. So only the first would be accurate. You can use sleep to check it by making it sleep for more than 1 second. I would use the code from the manual though that way you don't need to explode and then add the results together. Even i just saw the same in the manual & was about to post it , u were right, its microseconds since the seconds. However, i'll indeed use the code you provided, small & neat. Thanks for the code & explanation. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.