Strange data...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zachary Antolak

    Strange data...

    I implemented a timer in my PHP page to see how long it takes to run.
    Here's the code:

    $this->start_time = microtime();
    /* All the code */
    $this->end_time = microtime();
    $this->calc_time = ($this->end_time - $this->start_time);
    print "<tr><td colspan=\"5\">C alculated in: <b>";
    printf("%." . $this->time_precisi on . "f", $this->calc_time);
    print " seconds</b></td></tr>\n";

    It works, but occasionally it returns a negative value, i.e. -0.890163
    seconds. Why does this happen and how can I fix it?
  • 2trax

    #2
    Re: Strange data...

    On Fri, 04 Jul 2003 00:54:49 -0700, Zachary Antolak wrote:
    [color=blue]
    > I implemented a timer in my PHP page to see how long it takes to run.
    > Here's the code:
    >
    > $this->start_time = microtime();
    > /* All the code */
    > $this->end_time = microtime();
    > $this->calc_time = ($this->end_time - $this->start_time); print "<tr><td
    > colspan=\"5\">C alculated in: <b>"; printf("%." . $this->time_precisi on .
    > "f", $this->calc_time); print " seconds</b></td></tr>\n";
    >
    > It works, but occasionally it returns a negative value, i.e. -0.890163
    > seconds. Why does this happen and how can I fix it?[/color]

    It's not working reliably because microtime() returns an array, rather
    than a single value. One part of the array is the time in seconds, the
    other is the decimal places. 0.0015

    Use this code to get the microtime as a usable value:

    function get_microtime() {
    list($micro, $sec) = explode(" ",microtime ()); $mtime = (float)$sec +
    (float)$micro; return $mtime;
    }

    Adding the two components together would result in a number 16 digits
    long, which is too big for a 'double' or 'float'. PHP seems to throw away
    the least significant digits in cases like this, and on my machine the
    result appears to be accurate to at least 5 decimal places (1/10,000 s).
    ---
    Posted via news://freenews.netfront.net
    Complaints to news@netfront.n et

    Comment

    • Zachary Antolak

      #3
      Re: Strange data...

      2trax <2trax@salterpr ojects.com> wrote in message news:<pan.2003. 07.04.09.53.42. 929205@salterpr ojects.com>...[color=blue]
      > On Fri, 04 Jul 2003 00:54:49 -0700, Zachary Antolak wrote:
      >[color=green]
      > > I implemented a timer in my PHP page to see how long it takes to run.
      > > Here's the code:
      > >
      > > $this->start_time = microtime();
      > > /* All the code */
      > > $this->end_time = microtime();
      > > $this->calc_time = ($this->end_time - $this->start_time); print "<tr><td
      > > colspan=\"5\">C alculated in: <b>"; printf("%." . $this->time_precisi on .
      > > "f", $this->calc_time); print " seconds</b></td></tr>\n";
      > >
      > > It works, but occasionally it returns a negative value, i.e. -0.890163
      > > seconds. Why does this happen and how can I fix it?[/color]
      >
      > It's not working reliably because microtime() returns an array, rather
      > than a single value. One part of the array is the time in seconds, the
      > other is the decimal places. 0.0015
      >
      > Use this code to get the microtime as a usable value:
      >
      > function get_microtime() {
      > list($micro, $sec) = explode(" ",microtime ()); $mtime = (float)$sec +
      > (float)$micro; return $mtime;
      > }
      >
      > Adding the two components together would result in a number 16 digits
      > long, which is too big for a 'double' or 'float'. PHP seems to throw away
      > the least significant digits in cases like this, and on my machine the
      > result appears to be accurate to at least 5 decimal places (1/10,000 s).
      > ---
      > Posted via news://freenews.netfront.net
      > Complaints to news@netfront.n et[/color]

      Actually, the normal times returned are usually around 0.1xxxxx and
      the strange ones are around -0.8xxxxx. They're like a normal value,
      but -1. So, I made a test for it:

      if ($this->calc_time < 0)
      {
      $this->calc_time = $this->calc_time + 1;
      }

      It seems to work. Is this okay to use?

      Comment

      • Pedro

        #4
        Re: Strange data...

        Zachary Antolak wrote:[color=blue]
        >Actually, the normal times returned are usually around 0.1xxxxx and
        >the strange ones are around -0.8xxxxx. They're like a normal value,
        >but -1. So, I made a test for it:
        >
        >if ($this->calc_time < 0)
        >{
        > $this->calc_time = $this->calc_time + 1;
        >}
        >
        >It seems to work. Is this okay to use?[/color]

        NO! It is *NOT* okay to use if you want valid data!


        Copied from http://www.php.net/microtime
        --------
        Description

        string microtime ( void)

        Returns the string "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 microseconds
        part. This function is only available on operating systems
        that support the gettimeofday() system call.

        Both portions of the string are returned in units of seconds.
        ========

        So, right now if I do
        <?php
        $a = microtime();
        echo '[ ', $a, ' ]';
        ?>

        I get

        [ 0.18475200 1057352986 ]

        and in exactly five seconds I'd get

        [ 0.18475200 1057352991 ]


        so now I do
        <?php
        $first = '0.18475200 1057352986';
        $second = '0.18475200 1057352991';
        echo '[ ', $second - $first, ' ]';
        ?>

        to get
        [ 0 ]


        I guess this isn't what you want :)

        Check the very first example on the PHP manual to transform the string
        "0.18475200 1057352986" to the float 1057352986.1847 5200


        Happy Coding !!


        --
        "Yes, I'm positive."
        "Are you sure?"
        "Help, somebody has stolen one of my electrons!"
        Two atoms are talking:

        Comment

        • Zachary Antolak

          #5
          Re: Strange data...

          Pedro <hexkid@hotpop. com> wrote in message news:<7dd613845 3a66d44e60ca4b0 c36bb211@news.m eganetnews.com> ...[color=blue]
          > Zachary Antolak wrote:[color=green]
          > >Actually, the normal times returned are usually around 0.1xxxxx and
          > >the strange ones are around -0.8xxxxx. They're like a normal value,
          > >but -1. So, I made a test for it:
          > >
          > >if ($this->calc_time < 0)
          > >{
          > > $this->calc_time = $this->calc_time + 1;
          > >}
          > >
          > >It seems to work. Is this okay to use?[/color]
          >
          > NO! It is *NOT* okay to use if you want valid data!
          >
          >
          > Copied from http://www.php.net/microtime
          > --------
          > Description
          >
          > string microtime ( void)
          >
          > Returns the string "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 microseconds
          > part. This function is only available on operating systems
          > that support the gettimeofday() system call.
          >
          > Both portions of the string are returned in units of seconds.
          > ========
          >
          > So, right now if I do
          > <?php
          > $a = microtime();
          > echo '[ ', $a, ' ]';
          > ?>
          >
          > I get
          >
          > [ 0.18475200 1057352986 ]
          >
          > and in exactly five seconds I'd get
          >
          > [ 0.18475200 1057352991 ]
          >
          >
          > so now I do
          > <?php
          > $first = '0.18475200 1057352986';
          > $second = '0.18475200 1057352991';
          > echo '[ ', $second - $first, ' ]';
          > ?>
          >
          > to get
          > [ 0 ]
          >
          >
          > I guess this isn't what you want :)
          >
          > Check the very first example on the PHP manual to transform the string
          > "0.18475200 1057352986" to the float 1057352986.1847 5200
          >
          >
          > Happy Coding !![/color]

          Here's the function (it's in a class):

          function get_microtime()
          {
          list($this->usec, $this->sec) = explode(" ", microtime());
          return (float)$this->usec + (float)$this->sec;
          }

          Is this okay?

          Comment

          Working...