473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

multiple function calls or character array concat?

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
3 2661
.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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: metzger | last post by:
I am using the function listed below to handle characters events in SAX. It does not handle multiple sequential calls to this function correctly. For example, I am getting "2 4 816 32 64" as a...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
1
NeoPa
by: NeoPa | last post by:
A number of posters have asked to be shown how to produce a list of items from multiple records which are (potentially) grouped together. Take the following data for instance (from a table called )...
14
by: jackiefm | last post by:
I realize the thread I am responding to was posted in January but I am basically having the same issue. I am not familiar with VBA but use Access daily. I have written simple scripts but nothing to...
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
0
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.