Jump to content

A doubt regarding the use of microtime()


rocky_88

Recommended Posts

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"; 
?>

Link to comment
Share on other sites

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";?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P,

u were right, its microseconds since the seconds.

However, i'll indeed use the code you provided, small & neat.

Thanks for the code & explanation.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.