.= very slow !

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

    #1

    .= very slow !

    Hi,

    I discovered something which surprised me today:

    version 1/
    $toto = "";
    while (mysql->fetchrow) (about 400 times)
    {
    $toto .= "row1 row2 row3";
    }

    => took 60s

    version 2/
    array $toto;
    while (mysql->fetchrow) (about 400 times)
    {
    $toto[] = "row1 row2 row3";
    }
    is veryyyy faster ... 3s !

    I discovered a difference with perl too but version 1 takes 8s only


  • Hayden Kirk

    #2
    Re: .= very slow !

    Thats why I never use that method... easier to read, nicer code... does not
    perform very well..


    "Pascal H" <mmm@mmm.com> wrote in message
    news:405f7a1b$0 $314$636a15ce@n ews.free.fr...[color=blue]
    > Hi,
    >
    > I discovered something which surprised me today:
    >
    > version 1/
    > $toto = "";
    > while (mysql->fetchrow) (about 400 times)
    > {
    > $toto .= "row1 row2 row3";
    > }
    >
    > => took 60s
    >
    > version 2/
    > array $toto;
    > while (mysql->fetchrow) (about 400 times)
    > {
    > $toto[] = "row1 row2 row3";
    > }
    > is veryyyy faster ... 3s !
    >
    > I discovered a difference with perl too but version 1 takes 8s only
    >
    >[/color]


    Comment

    • Eric Bohlman

      #3
      Re: .= very slow !

      "Pascal H" <mmm@mmm.com> wrote in
      news:405f7a1b$0 $314$636a15ce@n ews.free.fr:
      [color=blue]
      > I discovered something which surprised me today:
      >
      > version 1/
      > $toto = "";
      > while (mysql->fetchrow) (about 400 times)
      > {
      > $toto .= "row1 row2 row3";
      > }
      >
      > => took 60s
      >
      > version 2/
      > array $toto;
      > while (mysql->fetchrow) (about 400 times)
      > {
      > $toto[] = "row1 row2 row3";
      > }
      > is veryyyy faster ... 3s !
      >
      > I discovered a difference with perl too but version 1 takes 8s only[/color]

      String concatenation will generally be slower than adding an element to an
      array because it's usually implemented by allocating a new block of memory
      big enough to hold both strings, copying the original string over to it,
      and then copying the new string to it. Adding an element to an array is
      mostly just a matter of allocating memory for the new string and copying
      it; the original contents of the array don't have to be copied.

      This description is obviously oversimplified, but the main point is that
      when you're concatenating, the original string has to be copied, and the
      time this takes increases with each successive concatenation since the
      original string is getting longer.

      To get a little mathematical and use an oversimplified example (namely
      dealing with N strings, all of the same length M characters), adding them
      to an array will require N*M characters to be copied, so the run time is
      proportional to N. We say that it "runs in linear time."

      With concatenation, the first string has to be copied N times, the second
      string N-1 times, and so on until the last string has to be copied only
      once. This means that (N+(N-1)+(N-2)+...+1)*M characters have to be
      copied, which works out to (N**2+N)*M (using "N**2" to mean "N squared"),
      so the run time is now proportional to the square of N. We say that it
      "runs in quadratic time."

      The result is that if you double the number of strings, the array-insertion
      method will take twice as long, but the concatenation method will take 6
      times as long.

      Of course an interpreter can optimize things a bit, for example by
      allocating extra memory for each string so that in some cases that
      concatenated string can just be appended onto the end of the original, but
      there's a limit to how much this can be done. The main problem is that in
      realistic scenarios, the interpreter has no idea how big the next string to
      concatenate is going to be.

      Comment

      • Hayden Kirk

        #4
        Re: .= very slow !

        Very good responce,

        thanks :P

        "Eric Bohlman" <ebohlman@earth link.net> wrote in message
        news:Xns94B4BE4 1874D0ebohlmano msdevcom@130.13 3.1.4...[color=blue]
        > "Pascal H" <mmm@mmm.com> wrote in
        > news:405f7a1b$0 $314$636a15ce@n ews.free.fr:
        >[color=green]
        > > I discovered something which surprised me today:
        > >
        > > version 1/
        > > $toto = "";
        > > while (mysql->fetchrow) (about 400 times)
        > > {
        > > $toto .= "row1 row2 row3";
        > > }
        > >
        > > => took 60s
        > >
        > > version 2/
        > > array $toto;
        > > while (mysql->fetchrow) (about 400 times)
        > > {
        > > $toto[] = "row1 row2 row3";
        > > }
        > > is veryyyy faster ... 3s !
        > >
        > > I discovered a difference with perl too but version 1 takes 8s only[/color]
        >
        > String concatenation will generally be slower than adding an element to an
        > array because it's usually implemented by allocating a new block of memory
        > big enough to hold both strings, copying the original string over to it,
        > and then copying the new string to it. Adding an element to an array is
        > mostly just a matter of allocating memory for the new string and copying
        > it; the original contents of the array don't have to be copied.
        >
        > This description is obviously oversimplified, but the main point is that
        > when you're concatenating, the original string has to be copied, and the
        > time this takes increases with each successive concatenation since the
        > original string is getting longer.
        >
        > To get a little mathematical and use an oversimplified example (namely
        > dealing with N strings, all of the same length M characters), adding them
        > to an array will require N*M characters to be copied, so the run time is
        > proportional to N. We say that it "runs in linear time."
        >
        > With concatenation, the first string has to be copied N times, the second
        > string N-1 times, and so on until the last string has to be copied only
        > once. This means that (N+(N-1)+(N-2)+...+1)*M characters have to be
        > copied, which works out to (N**2+N)*M (using "N**2" to mean "N squared"),
        > so the run time is now proportional to the square of N. We say that it
        > "runs in quadratic time."
        >
        > The result is that if you double the number of strings, the[/color]
        array-insertion[color=blue]
        > method will take twice as long, but the concatenation method will take 6
        > times as long.
        >
        > Of course an interpreter can optimize things a bit, for example by
        > allocating extra memory for each string so that in some cases that
        > concatenated string can just be appended onto the end of the original, but
        > there's a limit to how much this can be done. The main problem is that in
        > realistic scenarios, the interpreter has no idea how big the next string[/color]
        to[color=blue]
        > concatenate is going to be.[/color]


        Comment

        • Jason

          #5
          Re: .= very slow !

          "Pascal H" <mmm@mmm.com> wrote in message news:<405f7a1b$ 0$314$636a15ce@ news.free.fr>.. .[color=blue]
          > Hi,
          >
          > I discovered something which surprised me today:
          >
          > version 1/
          > $toto = "";
          > while (mysql->fetchrow) (about 400 times)
          > {
          > $toto .= "row1 row2 row3";
          > }
          >
          > => took 60s
          >
          > version 2/
          > array $toto;
          > while (mysql->fetchrow) (about 400 times)
          > {
          > $toto[] = "row1 row2 row3";
          > }
          > is veryyyy faster ... 3s !
          >
          > I discovered a difference with perl too but version 1 takes 8s only[/color]

          Yeah, but if you really want a string, you have to add in the time it
          would take to implode() your array.

          Try this:
          $text = '';
          for ($i=0; $i<10000; $i++) {
          $text .= ' '.$i;
          }
          echo '<p>'.$text;

          Versus this:

          $array = array();
          for ($i=0; $i<10000; $i++) {
          $array[] = $i;
          }
          $text = implode(' ', $array);
          echo '<p>'.$text;

          Comment

          Working...