By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

multiple function calls or character array concat?

P: n/a
rl
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('wkjksdbjvsdnklvsDVL'.$a.'vfaadf');
echo('wkjksdbdnklvsDVL'.$b.'vfaadf');
echo('wkjksdbjvslvsDVL'.$c.'vdf');
echo('wkjksdbnklvsDVL'.$d.'vfdf');

// single function call with bigger char array and more concat steps
echo('wkjksdbjvsdnklvsDVL'.$a.'vfaadf'
.'wkjksdbdnklvsDVL'.$b.'vfaadf'
.'wkjksdbjvslvsDVL'.$c.'vdf'
.'wkjksdbnklvsDVL'.$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 implementational
details but unfortunately found nocthing.

TIA

Robert
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
.oO(rl)
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).
PHP uses reference counting and copies only when necessary. But
sometimes creating a reference may take longer than a simple copy.
The reason of my question is the following:

// many function calls with smaller char arrays and less concats
echo('wkjksdbjvsdnklvsDVL'.$a.'vfaadf');
echo('wkjksdbdnklvsDVL'.$b.'vfaadf');
echo('wkjksdbjvslvsDVL'.$c.'vdf');
echo('wkjksdbnklvsDVL'.$d.'vfdf');

// single function call with bigger char array and more concat steps
echo('wkjksdbjvsdnklvsDVL'.$a.'vfaadf'
.'wkjksdbdnklvsDVL'.$b.'vfaadf'
.'wkjksdbjvslvsDVL'.$c.'vdf'
.'wkjksdbnklvsDVL'.$d.'vfdf');
Third variant without concats (probably the fastest):

echo 'wkjksdbjvsdnklvsDVL', $a, 'vfaadf',
'wkjksdbdnklvsDVL', $b, 'vfaadf',
'wkjksdbjvslvsDVL', $c, 'vdf',
'wkjksdbnklvsDVL', $d, 'vfdf';
I tried php.net to find out on these not unimportant implementational
details but unfortunately found nocthing.


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
Jul 17 '05 #2

P: n/a

"rl" <no****@nospam.org> wrote in message
news:cr*********@newsreader2.netcologne.de...
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('wkjksdbjvsdnklvsDVL'.$a.'vfaadf');
echo('wkjksdbdnklvsDVL'.$b.'vfaadf');
echo('wkjksdbjvslvsDVL'.$c.'vdf');
echo('wkjksdbnklvsDVL'.$d.'vfdf');

// single function call with bigger char array and more concat steps
echo('wkjksdbjvsdnklvsDVL'.$a.'vfaadf'
.'wkjksdbdnklvsDVL'.$b.'vfaadf'
.'wkjksdbjvslvsDVL'.$c.'vdf'
.'wkjksdbnklvsDVL'.$d.'vfdf');


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('Dingo', $dingos);
$s = implode('', $a);

Jul 17 '05 #3

P: n/a
rl
Chung,

thanks for your effort, but I fear I still got no answer.
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. ??!?!??
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
implementational facts, the rest is part of php-docs.
The difference in this case is too small to worry about. The issue has more
relevance in loops. In fact the question arouse when looping db-resultsets and patching
html-output from it.
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('Dingo', $dingos);
$s = implode('', $a);

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.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.