473,624 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('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
Jul 17 '05 #1
3 2675
.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('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');
Third variant without concats (probably the fastest):

echo 'wkjksdbjvsdnkl vsDVL', $a, 'vfaadf',
'wkjksdbdnklvsD VL', $b, 'vfaadf',
'wkjksdbjvslvsD VL', $c, 'vdf',
'wkjksdbnklvsDV L', $d, 'vfdf';
I tried php.net to find out on these not unimportant implementationa l
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.n etcologne.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('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');


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);

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
implementationa l 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('Ding o', $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
43913
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 will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. */ Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ WSH 5.6 Documentation -...
32
14831
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
2
1516
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 value for an element when processing <vec2 4 8 16 32 64 </vec> because I am getting 2 calls to process the text in this element, one for "2 4 8" and the other for "16 32 64". I have tried appending a blank to the result after each call to this...
28
4302
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
8211
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 ) : Zone Forum Community Introductions Community Community Cafe Community Software Development Community Jobs / Contract Work Community Experts Panel Programming C++ / C Programming Java
14
2606
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 write home about. I followed the previous thread and my situation is very similar. I have an application with users. These users have access to multiple terminals. I am trying to combine the TerminalName field with all terminals the user has access...
9
4086
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 puts() to print to screen ????
0
3102
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td colspan="2"><p>
3
3912
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 messing up. Problems:
0
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8490
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.