Jump to content

Recommended Posts

Ok I made a post yesterday but did not get an answer that made sense, so I am going a little deeper this time.

microtime() defines a string as msec and sec. But the msec portion is 8 digits long after the decimal for instance 0.52049300. Yet there are only 1,000,000 microseconds in a second. The zero never gets filled so I assume that it is the representation of a second. So what does 0.52049300 mean? Is that a split time of a microsecond? Or is it a larger measure of time?

Link to comment
https://forums.phpfreaks.com/topic/166635-solved-msec-is-it-really/
Share on other sites

Isn't that just a decimal representation of 1,000,000 microseconds? Let me explain in numbers:

 

<?php

$msec = 0.52049300;
$realmsec = 0.52049300 * 1000000; // I think. This may be miles off the real answer.

?>

 

That's how I see it anyway. Don't see any other way it could work.

It returns the the time passed since the unix epoch in seconds and microseconds. The total time passed since the unix epoch is secs + msecs. Don't know if this has something to do with it but: In the old days they didn't have something like float because those computers couldn't store such long formats therefor they kept it in strings performed some string operation to retrieve both the numbers before the decimal point and the numbers after the decimal point as an integer added some math performed the calculation all within that tiny space and added it back together as a string and displayed it to screen. They even add to store subcalculations in a string format because they lacked space.

Yeah I pretty much was able to read that. But why is msec represented by a decimal with 8 digits after it? Why when there are only 1,000,000 microseconds in a second are there 8 digits? Is the computer taking split microseconds into consideration? I want a definitive answer not just guesses. I am trying to time as precisely as is possible, but this part does not make sense.

Yeah I pretty much was able to read that. But why is msec represented by a decimal with 8 digits after it? Why when there are only 1,000,000 microseconds in a second are there 8 digits? Is the computer taking split microseconds into consideration? I want a definitive answer not just guesses. I am trying to time as precisely as is possible, but this part does not make sense.

 

print microtime() . '<br>';
print microtime(true);//your definitive answer

So if you guys are correct in your assumptions then this should be dead on everytime as far as measuring down to the last unit of measurement capable by the microtime function.

<?php
//Timer
$start=explode(' ',microtime());
//execute information here.
$finish=explode(' ',microtime());
if($start[1]===$finish[1]) {
    $start=explode('.',$start[0]);
    $finish=explode('.',$finish[0]);
    $diff=$finish[1]-$start[1]; 
    if(($start[0]!=='0') || ($finish[0]!=='0')) {
        echo 'We have encountered an exception that was not thought to be possible.';
        exit;
    }
}
else {
    $start=explode('.',$start[0]);
    $finish=explode('.',$finish[0]);
    $finish[1]=$finish[1]+100000000;
    $diff=$finish[1]-$start[1];
    if(($start[0]!=='0') || ($finish[0]!=='0')) {
        echo 'We have encountered an exception that was not thought to be possible.';
        exit;
    }
}
echo 'Time Difference: '.$diff;
echo '<br />';
echo 'Finish Time: '.$finish[1];
echo '<br />';
echo 'Start Time: '.$start[1];
?>

Well so far from running this code many times it seems that the last two digits are never filled out by the computer. Thus it is only measuring down to the microsecond any ways. Sorry if i pissed any one off, but I wanted to know if I was right in my assumption too, just one of those things you have to know. Thank you for your help!

Not sure if any one cares but this is the basic concept that I came up with to report microseconds only. That is so long as we are correct, which I think we are.

 <?php
//Timer
$start=explode(' ',microtime());
//execute information here.
$finish=explode(' ',microtime());
if($start[1]===$finish[1]) {
    $start=explode('.',$start[0]);
    $finish=explode('.',$finish[0]);
    $diff=$finish[1]-$start[1]; 
    if(($start[0]!=='0') || ($finish[0]!=='0')) {
        echo 'We have encountered an exception that was not thought to be possible.';
        exit;
    }
}
else {
    $start=explode('.',$start[0]);
    $finish=explode('.',$finish[0]);
    $finish[1]=$finish[1]+100000000;
    $diff=$finish[1]-$start[1];
    if(($start[0]!=='0') || ($finish[0]!=='0')) {
        echo 'We have encountered an exception that was not thought to be possible.';
        exit;
    }
}
$diff=substr($diff,0,strlen($diff)-2);
echo 'Time Difference: '.$diff.' microseconds.';
echo '<br />';
echo 'Finish Time: '.$finish[1];
echo '<br />';
echo 'Start Time: '.$start[1];
?>

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.