473,246 Members | 1,386 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,246 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 2656
.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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.