473,323 Members | 1,547 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,323 software developers and data experts.

tuning php: variables inside strings

(N.B. - this may be a well-known thing, but it was new to me). I was
reading a script written by someone else, and noticed a coding style
difference - they always concatenated variables into strings, while I
always keep them inline. Out of curiosity, I wrote the following (with
some help from the online php man page on microtime()) to see which was
more efficient. Not only is my way (in-line) worse, it's *much* worse.

If anyone has a different interpretation, let me know.
<?php
/* Values from our dev server:
Concat string (50,000 reps):
1.156
1.127
1.128

In-line string (50,000 reps):
3.524
3.312
3.287

*/

function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}

$val1 = 1;
$val2 = "Hello World";
$start = microtime();
for ($i = 0; $i < 50000; $i++) {
$string1 = "First part of string ".$val1." Second part of
string ".$val2." final part";
// $string2 = "First part of string $val1 Second part of string
$val2 final part";
}
$diff = microtime_diff($start, microtime());
$duration = sprintf("%0.3f", $diff);
echo "Processing took $duration seconds";
?>
Jul 17 '05 #1
3 2300
Greg Bryant wrote:
(N.B. - this may be a well-known thing, but it was new to me).**I*was
reading a script written by someone else, and noticed a coding style
difference - they always concatenated variables into strings, while I
always keep them inline.**Out*of*curiosity,*I*wrote*the*following*( with
some help from the online php man page on microtime()) to see which was
more efficient.**Not*only*is*my*way*(in-line)*worse,*it's*much*worse.


<snip>

You'll also find it's faster if you use single quotes instead of double
quotes, as there's no requirement for variable interpolation with a single
string.

eg use

$foo = 'bar1'.$foo1.'bar2';

rather than

$foo = "bar1".$foo1."bar2";

A lot of this coding style comes down to personal preference, as some people
feel code can look messy and be harder to maintain when concatenating
strings.

From your tests it appears that inline is 3x slower than concatenation but
you're looking a such a small amount of time (1sec for 50k reps and 3sec
for 50k reps) that only mere fractions of a second separate the two when
only doing it once. Obviously if your application will experience huge
numbers of visitors you want to keep it as quick as possible though.

Chris

Chris Hope
The Electric Toolbox Ltd
http://www.electrictoolbox.com/
Jul 17 '05 #2
Chris Hope <bl*******@electrictoolbox.com> wrote in
news:Dj******************@news.xtra.co.nz:
From your tests it appears that inline is 3x slower than concatenation
but you're looking a such a small amount of time (1sec for 50k reps
and 3sec for 50k reps) that only mere fractions of a second separate
the two when only doing it once. Obviously if your application will
experience huge numbers of visitors you want to keep it as quick as
possible though.


My first set of tests was at 500,000 instead of 50,000, and the test seems
to scale - the concat was at 11+ secs, and the inline was over 30 (past the
execution limit on our dev server :-).

Thanks for the tip on the single quotes - I'll play with that, too.

Greg
Jul 17 '05 #3

Uzytkownik "Greg Bryant" <br**********@yahoo.com> napisal w wiadomosci
news:Xn**********************************@199.45.4 9.11...
(N.B. - this may be a well-known thing, but it was new to me). I was
reading a script written by someone else, and noticed a coding style
difference - they always concatenated variables into strings, while I
always keep them inline. Out of curiosity, I wrote the following (with
some help from the online php man page on microtime()) to see which was
more efficient. Not only is my way (in-line) worse, it's *much* worse.

If anyone has a different interpretation, let me know.


Well, you test doesn't really replicate a real-world situation though.
Through every loop the length of the string remains the same, and the string
is freed at the end of the loop so that the same memory block can be reused.

I think in actual use, variable interpolation would be faster than multiple
concatenations, since few memory allocation is involved. For "First part of
string $val1 Second part of string $val2 final part", PHP only needs to
allocate memory once, whereas for 'First part of string '.$val1.' Second
part of string '.$val2.' final part', it has to allocate memory for each of
the concat operation (four in all). This overhead doesn't show up in your
test because there're always that many blocks in the memory cache of
precisely the right size.
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: pee2pee | last post by:
Hi, I have below code: <html> <head> <title>Contacting Worldpay, Please wait.......</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body...
13
by: Robert Smith | last post by:
I'm doing a website development course and during an exercise my teacher gave me to do at home I was confronted with errors. Surprisingly, those that did the exercise in class did not receive...
4
by: FLEB | last post by:
I like PHP for its excellent inline integration into standard HTML files, but I like Perl for its quick-moving syntax and simpler data-processing. To resolve this deep-seated inner turmoil (oh, the...
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
13
by: atlaste | last post by:
Hi, I'm currently developing an application that uses a lot of computational power, disk access and memory caching (to be more exact: an information retrieval platform). In these kind of...
0
by: Medhatithi | last post by:
Hi, I have been in several ways benefiited from this site. I would like to share some sql tuning techniques(simple, but effective) with you all. SQL Tuning Tips Oracle Tips Session #6 ...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.