speed: by reference or by value?

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

    speed: by reference or by value?

    -------
    $a = array(......... ...);
    $b = $a;
    -------
    php says that $b points to $a until $b or $a changes.
    this means that the code above wants the same time with the code below:
    -------
    $a = $a = array(......... ...);
    $b = & $a;
    -------

    but when I check this with a script, I found this:
    when I use & (ref) the script is faster.

    in the code below: (php 5. WinXP. command line script)
    first part needs 2.9 secs
    second part (in "this line" & exists) needs 3.0 secs
    second part (in "this line" & not exist) needs 6.5 secs but not $a nor
    $b changes. So I believe that php creates a whole copy of $a in $b.

    THE QUESTION:
    If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
    $v); )

    ---------THE CODE------------------------------------------
    <?
    $a =
    array(42,345,32 45,3245,2345,45 64,51346,345723 4,13762,4561,34 65,23,451,345,3 2,45,23,45,23,4 ,76,3,45,14,6,4 5632,46,32,4);

    $a['ddd'] = array(345,34,53 2,45,3245,3,45, 2345,7643345,26 45612,6423,562) ;

    //------------------first
    $t = microtime(true) ;
    for($z = 0; $z < 1000000; $z++) {
    foreach($a['ddd'] as $v);
    }
    $t = microtime(true) - $t;
    echo "$t\n";
    //------------------second
    $t = microtime(true) ;
    for($z = 0; $z < 1000000; $z++) {
    $b = & $a['ddd']; // <--------- THIS LINE
    foreach($b as $v);
    }
    $t = microtime(true) - $t;
    echo "$t\n";
    ?>
    ----------------------------------------------------
  • Chameleon

    #2
    Re: speed: by reference or by value?

    Chameleon wrote:[color=blue]
    > -------
    > $a = array(......... ...);
    > $b = $a;
    > -------
    > php says that $b points to $a until $b or $a changes.
    > this means that the code above wants the same time with the code below:
    > -------[/color]
    [color=blue]
    > $a = $a = array(......... ...);[/color]

    error: I mean
    $a = array(......... ...);
    [color=blue]
    > $b = & $a;
    > -------[/color]

    Comment

    • Berimor

      #3
      Re: speed: by reference or by value?

      On Sat, 03 Dec 2005 19:22:29 +0200, Chameleon
      <cham_gss@hotma il.NOSPAM.com> wrote:
      [color=blue]
      > -------
      > $a = array(......... ...);
      > $b = $a;
      > -------
      > php says that $b points to $a until $b or $a changes.
      > this means that the code above wants the same time with the code below:
      > -------
      > $a = $a = array(......... ...);
      > $b = & $a;
      > -------
      >
      > but when I check this with a script, I found this:
      > when I use & (ref) the script is faster.
      >
      > in the code below: (php 5. WinXP. command line script)
      > first part needs 2.9 secs
      > second part (in "this line" & exists) needs 3.0 secs
      > second part (in "this line" & not exist) needs 6.5 secs but not $a nor
      > $b changes. So I believe that php creates a whole copy of $a in $b.
      >
      > THE QUESTION:
      > If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
      > $v); )
      >
      > ---------THE CODE------------------------------------------
      > <?
      > $a =
      > array(42,345,32 45,3245,2345,45 64,51346,345723 4,13762,4561,34 65,23,451,345,3 2,45,23,45,23,4 ,76,3,45,14,6,4 5632,46,32,4);
      >
      > $a['ddd'] = array(345,34,53 2,45,3245,3,45, 2345,7643345,26 45612,6423,562) ;
      >
      > //------------------first
      > $t = microtime(true) ;
      > for($z = 0; $z < 1000000; $z++) {
      > foreach($a['ddd'] as $v);
      > }
      > $t = microtime(true) - $t;
      > echo "$t\n";
      > //------------------second
      > $t = microtime(true) ;
      > for($z = 0; $z < 1000000; $z++) {
      > $b = & $a['ddd']; // <--------- THIS LINE
      > foreach($b as $v);
      > }
      > $t = microtime(true) - $t;
      > echo "$t\n";
      > ?>
      > ----------------------------------------------------[/color]



      No, you're wrong in your research. The reson is very unnoing though. You
      treat the microtime() function wrong. Please read PHP manual - beacuse it
      can hurt you once. My fixings shown that second part is really faster -
      but only about 8% then first one.
      Here is code:

      <?
      function getmicrotime(){
      list($usec, $sec) = explode(" ",microtime ());
      return ((float)$usec + (float)$sec);
      }

      $a =
      array(42,345,32 45,3245,2345,45 64,51346,345723 4,13762,4561,34 65,23,451,345,3 2,45,23,45,23,4 ,76,3,45,14,6,4 5632,46,32,4);

      $a['ddd'] = array(345,34,53 2,45,3245,3,45, 2345,7643345,26 45612,6423,562) ;

      //------------------first
      $time_start = getmicrotime();
      for($z = 0; $z < 1000000; $z++) {
      $b = & $a['ddd'];// <--------- THIS LINE
      foreach($b as $v);
      }


      $time_end = getmicrotime();
      $time = $time_end - $time_start;

      echo "$time\n ";
      //------------------second
      $time_start = getmicrotime();
      for($z = 0; $z < 1000000; $z++) {
      foreach($a['ddd'] as $v);
      }

      $time_end = getmicrotime();
      $time = $time_end - $time_start;

      echo "$time\n";




      ?>



      --



      Exact Meta Search | Major Search Engine http://exactsearcher.com
      Web Design Essex | Multimedia | Printing http://nextwave.co.uk

      Comment

      • Chung Leong

        #4
        Re: speed: by reference or by value?

        Chameleon wrote:[color=blue]
        > THE QUESTION:
        > If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
        > $v); )[/color]

        You should only use references when necessary, i.e. when you want the
        referenced variable to be modifed somewhere else down the line.
        Improper use of references will slow down your code considerably. For
        example, if you time the following loops, you'll find that the second
        one is much slower:

        //------------------first
        $t = microtime(true) ;
        for($z = 0; $z < 1000000; $z++) {
        $b = $a;
        $c = $b;
        }

        //------------------second
        $t = microtime(true) ;
        for($z = 0; $z < 1000000; $z++) {
        $b = $a;
        $c =& $b;
        }

        Why is the second loop slow? That's because the reference assignment to
        $c forces a copy of the array to be made. The by-reference and by-value
        mechanism in PHP is quite unlike that in C/C++. By-value in PHP means
        share-read while by-reference means share-read-write. A variable cannot
        do both at the same time. If $c and $b are sharing read-write access,
        then $a and $b cannot be sharing just read access, as $a and $c are not
        sharing read-write access. A good analogy is Jack lending Jill a
        picture book. She is supposed to just read it. If she wants to give it
        to her little brother to draw on, she had better get her your copy,
        since that wasn't the agreement between her and Jack.

        If you can ensure that given piece of data is always passed
        by-reference, then you might gain some performance benefits. If
        somewhere along the line though, it was passed by-value, then you take
        a performance hit. That's one of the reason why in PHP 5 objects are
        always passed by-reference. Because invoking a method implies passing
        the object by reference ($this), objects should never be passed
        by-value.

        Comment

        • Chung Leong

          #5
          Re: speed: by reference or by value?

          It's oft imprudent to tell others to read the manual without reading it
          first ;-)

          Comment

          • Berimor

            #6
            Re: speed: by reference or by value?

            On 3 Dec 2005 13:41:09 -0800, Chung Leong <chernyshevsky@ hotmail.com>
            wrote:
            [color=blue]
            > It's oft imprudent to tell others to read the manual without reading it
            > first ;-)
            >[/color]


            Explain please.


            --
            Exact Meta Search | Major Search Engine http://exactsearcher.com
            Web Design Essex | Multimedia | Printing http://nextwave.co.uk

            Comment

            • Chung Leong

              #7
              Re: speed: by reference or by value?

              See http://fi.php.net/microtime. Note the new parameter available for
              PHP 5.

              Comment

              • Berimor

                #8
                Re: speed: by reference or by value?

                On 3 Dec 2005 13:28:55 -0800, Chung Leong <chernyshevsky@ hotmail.com>
                wrote:
                [color=blue]
                > Chameleon wrote:[color=green]
                >> THE QUESTION:
                >> If I want a FAST script, I must use & everywhere? (e.g. foreach ($b as &
                >> $v); )[/color]
                >
                > You should only use references when necessary, i.e. when you want the
                > referenced variable to be modifed somewhere else down the line.
                > Improper use of references will slow down your code considerably. For
                > example, if you time the following loops, you'll find that the second
                > one is much slower:
                >
                > //------------------first
                > $t = microtime(true) ;
                > for($z = 0; $z < 1000000; $z++) {
                > $b = $a;
                > $c = $b;
                > }
                >
                > //------------------second
                > $t = microtime(true) ;
                > for($z = 0; $z < 1000000; $z++) {
                > $b = $a;
                > $c =& $b;
                > }
                >
                > Why is the second loop slow?[/color]

                To teach someone try it first by yourself!.
                The first part (twice!!!!) slower then second one.
                And do not compare C and PHP - its like to compare jet and aerostat.

                Do try this code (but under php translator :) ) What will you say then?


                <?
                function getmicrotime(){
                list($usec, $sec) = explode(" ",microtime ());
                return ((float)$usec + (float)$sec);
                }




                $a =
                array(42,345,32 45,3245,2345,45 64,51346,345723 4,13762,4561,34 65,23,451,345,3 2,45,23,45,23,4 ,76,3,45,14,6,4 5632,46,32,4);

                $a['ddd'] = array(345,34,53 2,45,3245,3,45, 2345,7643345,26 45612,6423,562) ;

                //------------------first
                $time_start = getmicrotime();
                for($z = 0; $z < 1000000; $z++) {
                $b = & $a['ddd'];// <--------- THIS LINE
                $b = $a;
                $c = $b;
                }


                $time_end = getmicrotime();
                $time = $time_end - $time_start;

                echo "$time\n ";

                //------------------second
                $time_start = getmicrotime();
                for($z = 0; $z < 1000000; $z++) {
                $b = $a;
                $c = &$b;
                }

                $time_end = getmicrotime();
                $time = $time_end - $time_start;

                echo "$time\n";

                ?>





                --
                Exact Meta Search | Major Search Engine http://exactsearcher.com
                Web Design Essex | Multimedia | Printing http://nextwave.co.uk

                Comment

                • Berimor

                  #9
                  Re: speed: by reference or by value?

                  On 3 Dec 2005 14:28:21 -0800, Chung Leong <chernyshevsky@ hotmail.com>
                  wrote:
                  [color=blue]
                  > See http://fi.php.net/microtime. Note the new parameter available for
                  > PHP 5.
                  >[/color]

                  hmmm... Really. Well it's always hard to leave lovely things.







                  --
                  Exact Meta Search | Major Search Engine http://exactsearcher.com
                  Web Design Essex | Multimedia | Printing http://nextwave.co.uk

                  Comment

                  • Berimor

                    #10
                    Re: speed: by reference or by value?

                    ah sorry,
                    shame on me :(







                    --
                    Exact Meta Search | Major Search Engine http://exactsearcher.com
                    Web Design Essex | Multimedia | Printing http://nextwave.co.uk

                    Comment

                    • Chameleon

                      #11
                      Re: speed: by reference or by value?

                      > example, if you time the following loops, you'll find that the second[color=blue]
                      > one is much slower:
                      >
                      > //------------------first
                      > $t = microtime(true) ;
                      > for($z = 0; $z < 1000000; $z++) {
                      > $b = $a;
                      > $c = $b;
                      > }
                      >
                      > //------------------second
                      > $t = microtime(true) ;
                      > for($z = 0; $z < 1000000; $z++) {
                      > $b = $a;
                      > $c =& $b;
                      > }
                      >
                      > Why is the second loop slow? That's because the reference assignment to
                      > $c forces a copy of the array to be made. The by-reference and by-value
                      > mechanism in PHP is quite unlike that in C/C++. By-value in PHP means
                      > share-read while by-reference means share-read-write. A variable cannot
                      > do both at the same time. If $c and $b are sharing read-write access,
                      > then $a and $b cannot be sharing just read access, as $a and $c are not
                      > sharing read-write access. A good analogy is Jack lending Jill a
                      > picture book. She is supposed to just read it. If she wants to give it
                      > to her little brother to draw on, she had better get her your copy,
                      > since that wasn't the agreement between her and Jack.
                      >
                      > If you can ensure that given piece of data is always passed
                      > by-reference, then you might gain some performance benefits. If
                      > somewhere along the line though, it was passed by-value, then you take
                      > a performance hit. That's one of the reason why in PHP 5 objects are
                      > always passed by-reference. Because invoking a method implies passing
                      > the object by reference ($this), objects should never be passed
                      > by-value.
                      >[/color]

                      indeed.
                      your explanation sounds logical.

                      but I believe this is wrong in php core.
                      why php must copy the object (in our script: array) even if the pointed
                      data don't change?

                      I believe it is simple to implement this in C (php core) and I have
                      opinion but its hard to my to explain in english:

                      object $a: array
                      object $b: read pointer to $a
                      object $c: pointer to object $b

                      $b is always in the same place in heap, so even if data of $b is a
                      pointer to $a or copied data of $a has no sense for $c. $c always points
                      to $b object.
                      And objects which point to another objects have a flag that means: read
                      or read-write.

                      I never look for php core code. I imagine about the structure of it. So
                      maybe I am totally wrong.

                      Simply I think it is a bad architecture of php core if without changing
                      data we have data copy.

                      Comment

                      • Chung Leong

                        #12
                        Re: speed: by reference or by value?

                        Chameleon wrote:[color=blue]
                        > but I believe this is wrong in php core.
                        > why php must copy the object (in our script: array) even if the pointed
                        > data don't change?[/color]

                        Perhaps it's possible to defer the copying until the write operation
                        actually, but I suspect the logic would be quite complicated. It's like
                        Jill trying into slip a new picture book under the hand of her little
                        brother start as he starts drawing.

                        But as I said, the use of reference usually denotes that changes will
                        occur. If you don't want variable separation to occur, then don't use
                        references.

                        Comment

                        Working...