tuning php: variables inside strings

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

    tuning php: variables inside strings

    (N.B. - this may be a well-known thing, but it was new to me). I was
    reading a script written by someone else, and noticed a coding style
    difference - they always concatenated variables into strings, while I
    always keep them inline. Out of curiosity, I wrote the following (with
    some help from the online php man page on microtime()) to see which was
    more efficient. Not only is my way (in-line) worse, it's *much* worse.

    If anyone has a different interpretation, let me know.


    <?php
    /* Values from our dev server:
    Concat string (50,000 reps):
    1.156
    1.127
    1.128

    In-line string (50,000 reps):
    3.524
    3.312
    3.287

    */

    function microtime_diff( $a, $b) {
    list($a_dec, $a_sec) = explode(" ", $a);
    list($b_dec, $b_sec) = explode(" ", $b);
    return $b_sec - $a_sec + $b_dec - $a_dec;
    }

    $val1 = 1;
    $val2 = "Hello World";
    $start = microtime();
    for ($i = 0; $i < 50000; $i++) {
    $string1 = "First part of string ".$val1." Second part of
    string ".$val2." final part";
    // $string2 = "First part of string $val1 Second part of string
    $val2 final part";
    }
    $diff = microtime_diff( $start, microtime());
    $duration = sprintf("%0.3f" , $diff);
    echo "Processing took $duration seconds";
    ?>
  • Chris Hope

    #2
    Re: tuning php: variables inside strings

    Greg Bryant wrote:
    [color=blue]
    > (N.B. - this may be a well-known thing, but it was new to me).  I was
    > reading a script written by someone else, and noticed a coding style
    > difference - they always concatenated variables into strings, while I
    > always keep them inline.  Out of curiosity, I wrote the following (with
    > some help from the online php man page on microtime()) to see which was
    > more efficient.  Not only is my way (in-line) worse, it's much worse.[/color]

    <snip>

    You'll also find it's faster if you use single quotes instead of double
    quotes, as there's no requirement for variable interpolation with a single
    string.

    eg use

    $foo = 'bar1'.$foo1.'b ar2';

    rather than

    $foo = "bar1".$foo1."b ar2";

    A lot of this coding style comes down to personal preference, as some people
    feel code can look messy and be harder to maintain when concatenating
    strings.

    From your tests it appears that inline is 3x slower than concatenation but
    you're looking a such a small amount of time (1sec for 50k reps and 3sec
    for 50k reps) that only mere fractions of a second separate the two when
    only doing it once. Obviously if your application will experience huge
    numbers of visitors you want to keep it as quick as possible though.

    Chris

    Chris Hope
    The Electric Toolbox Ltd

    Comment

    • Greg Bryant

      #3
      Re: tuning php: variables inside strings

      Chris Hope <blackhole@elec trictoolbox.com > wrote in
      news:Dj49c.1130 $Tf3.22228@news .xtra.co.nz:
      [color=blue]
      > From your tests it appears that inline is 3x slower than concatenation
      > but you're looking a such a small amount of time (1sec for 50k reps
      > and 3sec for 50k reps) that only mere fractions of a second separate
      > the two when only doing it once. Obviously if your application will
      > experience huge numbers of visitors you want to keep it as quick as
      > possible though.[/color]

      My first set of tests was at 500,000 instead of 50,000, and the test seems
      to scale - the concat was at 11+ secs, and the inline was over 30 (past the
      execution limit on our dev server :-).

      Thanks for the tip on the single quotes - I'll play with that, too.

      Greg

      Comment

      • Chung Leong

        #4
        Re: tuning php: variables inside strings


        Uzytkownik "Greg Bryant" <bryantgHELLO@y ahoo.com> napisal w wiadomosci
        news:Xns94B8BA9 46F6E1bryantgHE LLOyahoocom@199 .45.49.11...[color=blue]
        > (N.B. - this may be a well-known thing, but it was new to me). I was
        > reading a script written by someone else, and noticed a coding style
        > difference - they always concatenated variables into strings, while I
        > always keep them inline. Out of curiosity, I wrote the following (with
        > some help from the online php man page on microtime()) to see which was
        > more efficient. Not only is my way (in-line) worse, it's *much* worse.
        >
        > If anyone has a different interpretation, let me know.[/color]

        Well, you test doesn't really replicate a real-world situation though.
        Through every loop the length of the string remains the same, and the string
        is freed at the end of the loop so that the same memory block can be reused.

        I think in actual use, variable interpolation would be faster than multiple
        concatenations, since few memory allocation is involved. For "First part of
        string $val1 Second part of string $val2 final part", PHP only needs to
        allocate memory once, whereas for 'First part of string '.$val1.' Second
        part of string '.$val2.' final part', it has to allocate memory for each of
        the concat operation (four in all). This overhead doesn't show up in your
        test because there're always that many blocks in the memory cache of
        precisely the right size.


        Comment

        Working...