speed questions

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

    speed questions

    say i was trying to append a % before any occurance of either abc,
    abd, or abe, in a string. which would be faster?:

    $string = str_replace("ab c","%abc",$stri ng);
    $string = str_replace("ab d","%abd",$stri ng);
    $string = str_replace("ab e","%abe",$stri ng);

    or

    $string - preg_replace("/(abc|abd|abe)/","%$1",$string );

    php.net says that preg_replace is slower than one str_replace, but for
    multiple ones, it seems like it would gain a slight speed advantage.

    also, exactly how would the following to code segments compare, speed
    wise?

    for ($num=0;$num<$l ength;$num++) {
    $array1[] = $num;
    $array2[] = $num;
    }

    for ($num=0;$num<$l ength;$num++)
    $array1[] = $num;
    for ($num=0;$num<$l ength;$num++)
    $array2[] = $num;

    it seems like the first one should require 3*$length operations,
    whereas the first one would require 4*$length operations, although i'm
    not 100% sure on that.
  • Chris Hope

    #2
    Re: speed questions

    yawnmoth wrote:
    [color=blue]
    > say i was trying to append a % before any occurance of either abc,
    > abd, or abe, in a string.  which would be faster?:
    >
    > $string = str_replace("ab c","%abc",$stri ng);
    > $string = str_replace("ab d","%abd",$stri ng);
    > $string = str_replace("ab e","%abe",$stri ng);
    >
    > or
    >
    > $string - preg_replace("/(abc|abd|abe)/","%$1",$string );[/color]

    Write a test script and run it in a loop say 1000 times doing the firstand
    then the second and measure it. That'll tell you which is faster.

    --
    Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

    Comment

    • rush

      #3
      Re: speed questions

      "yawnmoth" <terra1024@yaho o.com> wrote in message
      news:7j15d010pe bu1tuk3qmtepshu j1jhtcf12@4ax.c om...[color=blue]
      > say i was trying to append a % before any occurance of either abc,
      > abd, or abe, in a string. which would be faster?:
      >
      > $string = str_replace("ab c","%abc",$stri ng);
      > $string = str_replace("ab d","%abd",$stri ng);
      > $string = str_replace("ab e","%abe",$stri ng);
      >
      > or
      >
      > $string - preg_replace("/(abc|abd|abe)/","%$1",$string );
      >
      > php.net says that preg_replace is slower than one str_replace, but for
      > multiple ones, it seems like it would gain a slight speed advantage.[/color]

      you can also call str_replace with arrays of strings as parameters.

      $string = str_replace(arr ay("abc","abd", "abe") ,
      array("%abc","% abd","%abe"), $string);

      rush
      --



      Comment

      • Christian Fersch

        #4
        Re: speed questions

        yawnmoth wrote:[color=blue]
        > say i was trying to append a % before any occurance of either abc,
        > abd, or abe, in a string. which would be faster?:
        >
        > $string = str_replace("ab c","%abc",$stri ng);
        > $string = str_replace("ab d","%abd",$stri ng);
        > $string = str_replace("ab e","%abe",$stri ng);
        >
        > or
        >
        > $string - preg_replace("/(abc|abd|abe)/","%$1",$string );[/color]

        A correct preg_replace will be the fastest.
        $string = preg_replace("/(ab[cde])/","%$1",$string );

        This will be a bit slower than one str_replace, and a lot faster than 3
        of them.

        [color=blue]
        > also, exactly how would the following to code segments compare, speed
        > wise?
        >
        > for ($num=0;$num<$l ength;$num++) {
        > $array1[] = $num;
        > $array2[] = $num;
        > }
        >
        > for ($num=0;$num<$l ength;$num++)
        > $array1[] = $num;
        > for ($num=0;$num<$l ength;$num++)
        > $array2[] = $num;
        >
        > it seems like the first one should require 3*$length operations,
        > whereas the first one would require 4*$length operations, although i'm
        > not 100% sure on that.[/color]

        fastest here:
        $array1 = $array2 = range(0,$length );

        ;)

        greetings, Christian.

        Comment

        • Chung Leong

          #5
          Re: speed questions

          "yawnmoth" <terra1024@yaho o.com> wrote in message
          news:7j15d010pe bu1tuk3qmtepshu j1jhtcf12@4ax.c om...[color=blue]
          > say i was trying to append a % before any occurance of either abc,
          > abd, or abe, in a string. which would be faster?:
          >
          > $string = str_replace("ab c","%abc",$stri ng);
          > $string = str_replace("ab d","%abd",$stri ng);
          > $string = str_replace("ab e","%abe",$stri ng);
          >
          > or
          >
          > $string - preg_replace("/(abc|abd|abe)/","%$1",$string );
          >
          > php.net says that preg_replace is slower than one str_replace, but for
          > multiple ones, it seems like it would gain a slight speed advantage.[/color]

          I always like strtr(). Looks neater and doesn't involve the regexp engine.

          $table = array(
          "abc" => "%abc",
          "abd" => "%abd",
          "abe" => "%abe"
          );
          $string = strtr($string, $table);
          [color=blue]
          > also, exactly how would the following to code segments compare, speed
          > wise?
          >
          > for ($num=0;$num<$l ength;$num++) {
          > $array1[] = $num;
          > $array2[] = $num;
          > }
          >
          > for ($num=0;$num<$l ength;$num++)
          > $array1[] = $num;
          > for ($num=0;$num<$l ength;$num++)
          > $array2[] = $num;
          >[/color]

          The correct answer is $array1 = $array2 = range(0, $length - 1);


          Comment

          Working...