multiple function calls or character array concat?

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

    #1

    multiple function calls or character array concat?

    Hi out there,

    I'd like to know sth about the costs of a function call in php
    and the handling of character arrays (init size, enlargement steps of
    allocated memory, technique on enlargement -> full copy or virtual
    array spread in chunks over mem).
    The reason of my question is the following:

    // many function calls with smaller char arrays and less concats
    echo('wkjksdbjv sdnklvsDVL'.$a. 'vfaadf');
    echo('wkjksdbdn klvsDVL'.$b.'vf aadf');
    echo('wkjksdbjv slvsDVL'.$c.'vd f');
    echo('wkjksdbnk lvsDVL'.$d.'vfd f');

    // single function call with bigger char array and more concat steps
    echo('wkjksdbjv sdnklvsDVL'.$a. 'vfaadf'
    .'wkjksdbdnklvs DVL'.$b.'vfaadf '
    .'wkjksdbjvslvs DVL'.$c.'vdf'
    .'wkjksdbnklvsD VL'.$d.'vfdf');

    So this also arouses the question, wether a function argument that is
    passed as literal or expression will be passed by reference or by value?
    And does it make any difference, that echo is in fact a language
    construct and not a function, though the function-like syntax will be
    accepted? http://www.faqts.com/knowledge_base/...l/aid/1/fid/40
    says the only difference is, that a boolean return value will be set
    by a 'print' function call, while echo does not. But again no details on
    implementation of function calls, character arrays or arrays in general.

    I tried php.net to find out on these not unimportant implementationa l
    details but unfortunately found nocthing.

    TIA

    Robert
  • Michael Fesser

    #2
    Re: multiple function calls or character array concat?

    .oO(rl)
    [color=blue]
    >I'd like to know sth about the costs of a function call in php
    >and the handling of character arrays (init size, enlargement steps of
    >allocated memory, technique on enlargement -> full copy or virtual
    >array spread in chunks over mem).[/color]

    PHP uses reference counting and copies only when necessary. But
    sometimes creating a reference may take longer than a simple copy.
    [color=blue]
    >The reason of my question is the following:
    >
    >// many function calls with smaller char arrays and less concats
    >echo('wkjksdbj vsdnklvsDVL'.$a .'vfaadf');
    >echo('wkjksdbd nklvsDVL'.$b.'v faadf');
    >echo('wkjksdbj vslvsDVL'.$c.'v df');
    >echo('wkjksdbn klvsDVL'.$d.'vf df');
    >
    >// single function call with bigger char array and more concat steps
    >echo('wkjksdbj vsdnklvsDVL'.$a .'vfaadf'
    > .'wkjksdbdnklvs DVL'.$b.'vfaadf '
    > .'wkjksdbjvslvs DVL'.$c.'vdf'
    > .'wkjksdbnklvsD VL'.$d.'vfdf');[/color]

    Third variant without concats (probably the fastest):

    echo 'wkjksdbjvsdnkl vsDVL', $a, 'vfaadf',
    'wkjksdbdnklvsD VL', $b, 'vfaadf',
    'wkjksdbjvslvsD VL', $c, 'vdf',
    'wkjksdbnklvsDV L', $d, 'vfdf';
    [color=blue]
    >I tried php.net to find out on these not unimportant implementationa l
    >details but unfortunately found nocthing.[/color]

    In most cases these things are simply not important in a scripting
    language, because the interpreter handles it all. If you want to know
    such details you would either have to look at PHP's source code or
    simply test it for yourself. Setup a little benchmark and take the time
    for all different variants.

    Micha

    Comment

    • Chung Leong

      #3
      Re: multiple function calls or character array concat?


      "rl" <nospam@nospam. org> wrote in message
      news:crjc2u$3q$ 1@newsreader2.n etcologne.de...[color=blue]
      > Hi out there,
      >
      > I'd like to know sth about the costs of a function call in php
      > and the handling of character arrays (init size, enlargement steps of
      > allocated memory, technique on enlargement -> full copy or virtual
      > array spread in chunks over mem).
      > The reason of my question is the following:
      >
      > // many function calls with smaller char arrays and less concats
      > echo('wkjksdbjv sdnklvsDVL'.$a. 'vfaadf');
      > echo('wkjksdbdn klvsDVL'.$b.'vf aadf');
      > echo('wkjksdbjv slvsDVL'.$c.'vd f');
      > echo('wkjksdbnk lvsDVL'.$d.'vfd f');
      >
      > // single function call with bigger char array and more concat steps
      > echo('wkjksdbjv sdnklvsDVL'.$a. 'vfaadf'
      > .'wkjksdbdnklvs DVL'.$b.'vfaadf '
      > .'wkjksdbjvslvs DVL'.$c.'vdf'
      > .'wkjksdbnklvsD VL'.$d.'vfdf');[/color]

      Using concatenation is probably more expensive, because it requires
      allocation of fresh memory for each step. For "$A . $B . $C . $D", PHP needs
      to allocate memory 3 three times, first, for the tempoary variable holding A
      and B, then for another, holding AB + C, and finally the actual result ABC +
      D. It also wastes time copying the same text from buffer to buffer.

      The difference in this case is too small to worry about. The issue has more
      relevance in loops. Doing this, for instance:

      foreach($dingos as $dingo) $s .= $dingo;

      would usually be slower than doing this:

      $a = array();
      foreach($dingos as $dingo) $a[] = $dingo;
      $s = implode('', $a);

      which in turn is slower than

      function Dingo($obj) { return $obj; }
      $a = array_map('Ding o', $dingos);
      $s = implode('', $a);



      Comment

      • rl

        #4
        Re: multiple function calls or character array concat?

        Chung,

        thanks for your effort, but I fear I still got no answer.
        [color=blue]
        > Using concatenation is probably more expensive, because it requires
        > allocation of fresh memory for each step. For "$A . $B . $C . $D", PHP needs
        > to allocate memory 3 three times, first, for the tempoary variable holding A
        > and B, then for another, holding AB + C, and finally the actual result ABC +
        > D. It also wastes time copying the same text from buffer to buffer.[/color]
        ??!?!??
        Using vars abcd will anyway allocate mem for each. And for the rest,
        what you say is not necessarily true for implementation( and I'm sure
        it's not), it's only the definition of php-semantics. What I wanted was
        implementationa l facts, the rest is part of php-docs.
        [color=blue]
        > The difference in this case is too small to worry about. The issue has more
        > relevance in loops.[/color]
        In fact the question arouse when looping db-resultsets and patching
        html-output from it.
        [color=blue]
        > Doing this, for instance:
        > foreach($dingos as $dingo) $s .= $dingo;
        >
        > would usually be slower than doing this:
        >
        > $a = array();
        > foreach($dingos as $dingo) $a[] = $dingo;
        > $s = implode('', $a);
        >
        > which in turn is slower than
        >
        > function Dingo($obj) { return $obj; }
        > $a = array_map('Ding o', $dingos);
        > $s = implode('', $a);
        >[/color]
        Sorry, my question was explicitly not what you THINK MAY BE (or usually
        be) the fastest, but about implementation.
        And note that I meant CHARACTER arrays, thats a STRING, not a (cell-)
        array of strings.

        Comment

        Working...