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

String manipulation, optimize

P: n/a
Is it best to use double quotes and let PHP expand variables inside strings,
or is it faster to do the string manipulation yourself manually?

Which is quicker?

1)
$insert = 'To Be';
$sentence = "$insert or not $insert. That is the question.";

or
2)
$insert = 'To Be';
$sentence = $insert . ' or not ' . $insert . '. That is the question.';
Small thing, but might as well get it right.

Thanks
/Rune
Aug 8 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
*** Rune wrote/escribió (Mon, 8 Aug 2005 10:46:18 +0200):
Which is quicker? $sentence = "$insert or not $insert. That is the question."; $sentence = $insert . ' or not ' . $insert . '. That is the question.';


A quick-and-dirty benchmark in my old server showed the second code is
twice as fast as the first one. I suggest you test it in your own server.

However, you'll realize you need several thousand iterations to make a
difference so just use whatever makes your code clearer to understand.
(Unless, of course, you'll be manipulating several thounsand strings ;-)

--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Aug 8 '05 #2

P: n/a
Rune wrote:
Is it best to use double quotes and let PHP expand variables inside
strings, or is it faster to do the string manipulation yourself manually?

Which is quicker?

1)
$insert = 'To Be';
$sentence = "$insert or not $insert. That is the question.";

or
2)
$insert = 'To Be';
$sentence = $insert . ' or not ' . $insert . '. That is the question.';
Small thing, but might as well get it right.

Thanks
/Rune


Hi,

I do not want to sound like your teacher/parent/whatever, BUT...

Why do you care about minimal optimalizations?
In practice, do you expect ANYBODY to notice the difference?

My point being:
Write code YOU like.
Write code YOU think is good readable.
And have a little trust in the language you use.

Almost ALL cases where I saw a performancedegradation is was due to poorly
constructed databasequeries, NOT because of 'slow language' constructs.

I know I am not answering your question, but I just want to point out you
better focus on other issues. :-)

To answer your question:
It depends.
I wouldn't be surprised if you get different results on PHP4.3 than on PHP5.
Or different results on W2000 and Wxp and Debian Sarge, and.. etc.etc.

To get some idea on your current setup: Just run a benchmark.
And even benchmarks can give you a wrong impression because of all kinds of
smart optimizing happing behind your back by the engine.
just my 2 cents.
And again, sorry for sounding pedantic. :P

Regards,
Erwin Moller
Aug 8 '05 #3

P: n/a
Ok thanks both,

You are right I should probably just stick with what seems the most
readable - which is the double quotes for me.

But I have some code which is running too slow and you're right it's the SQL
which is performing badly. But I have been unable to really do anything
about that, so I'm trying to get whatever else performance improvements I
can otherwise. Probably it's not worth it, but on the other hand the string
concatenations are inside a loop which is run up to a thousand times and
sometimes up to a hundred thousand times.

/Rune
Aug 9 '05 #4

P: n/a
Erwin Moller wrote:
Rune wrote: <snip> Why do you care about minimal optimalizations?

<snip>

Sorry, I can't agree with you. In the world of optimization, there is
no minimal or maximal optimization--there is only onething, it's
optimization. Consider a situation/expression in which you can improve
the speed to 0.000001 second--and consider the same in 1000000 loop.
So, IMHO, definitely it is worth to optimize, even it is less than a
nano second thing.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

Aug 9 '05 #5

P: n/a
Ach, the meaningless debate that won't go away! The difference is so
miniscule that no one really knows which one is faster in a real-life
situations. Benchmark codes just give you bogus answers. The following
snippet, for instance, shows that the concatenation is more than an
order of magnitude slower than interpolation:

function getmicrotime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}

$i = "hello";
$a = array_fill(0, 30000, "\$i");
$code1 = "\$s = " . implode(".", $a) . ";";
$code2 = "\$s = \"" . implode("", $a) . "\";";

$time_start = getmicrotime();
eval($code1);
$time_end = getmicrotime();
$time1 = $time_end - $time_start;

$time_start = getmicrotime();
eval($code2);
$time_end = getmicrotime();
$time2 = $time_end - $time_start;

echo "$time1 vs $time2";
The bottomline: it doesn't matter.

Aug 10 '05 #6

P: n/a
R. Rajesh Jeba Anbiah wrote:
Erwin Moller wrote:
Rune wrote: <snip>
Why do you care about minimal optimalizations?

<snip>

Sorry, I can't agree with you. In the world of optimization, there is
no minimal or maximal optimization--there is only onething, it's
optimization. Consider a situation/expression in which you can improve
the speed to 0.000001 second--and consider the same in 1000000 loop.
So, IMHO, definitely it is worth to optimize, even it is less than a
nano second thing.


Hi, Rajesh

Well of course.
If you traverse through a loop zillions times, it is worth it.
I was only pointing out that in almost all cases i see, real optimalization
can be achieved by examining your databasequeries.

For example: The fact that you are looping a zillion times on some results
from a database, is an indication that you better learn SQL a little
better, and let the database do that calculations.
That is faster.

Of course you can find situations where you need it, mostly mathematical
issues, like numerical solutions for certain problems.
But in everyday life for a webdeveloper, in most situations: Make better
queries.

So we do not have a contradiction here.
I was making a statement about looping over databaseresults, where you are
making a more general statement.

Regards,
Erwin Moller

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com


Aug 10 '05 #7

P: n/a
R. Rajesh Jeba Anbiah wrote:
Erwin Moller wrote:
Rune wrote:


<snip>
Why do you care about minimal optimalizations?


<snip>

Sorry, I can't agree with you. In the world of optimization, there is
no minimal or maximal optimization--there is only onething, it's
optimization. Consider a situation/expression in which you can improve
the speed to 0.000001 second--and consider the same in 1000000 loop.
So, IMHO, definitely it is worth to optimize, even it is less than a
nano second thing.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com


Rajesh,

Sorry, this is an overly simplistic view. The IT industry generally recognizes
several levels of optimization.

Optimization is always a trade off. It might be between development time vs.
run time or a trade off between memory, disk space and runtime, for instance.

The problem is there are multiple ways to optimize. In the questioned case,
minimal optimization might be to run samples of both and determine which is
faster in this instance.

A higher level of optimization might be to run benchmarks on all the code to
determine what is taking the longest time and looking for ways to improve code
speed there. This would be important, for instance, if the server is becoming
heavily loaded.

If the server is completely overloaded and something serious has to be done, the
highest level of optimization would be to rewrite all the code in assembler.
Of course, it would take the longest. But it would have the highest level of
optimization possible for the user code.

Obviously there are other optimizations which may be made as well. For
instance, RDB's. Fully normalized databases (5th normal) have the least amount
of duplicate data - but are not the most efficient space wise, and definitely
not performance-wise. Most databases are closer to 3rd normal form which is
more efficient space wise than 2nd form normal, but less efficient in
processing. Where performance is important, sometimes it's better to regress
critical parts of the database to 2nd normal form for performance.

The above examples are just the tip of the iceberg. Optimization is not black
and white, as you indicate. There are many, many levels of optimization involved.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 10 '05 #8

P: n/a
Chung Leong wrote:
Ach, the meaningless debate that won't go away! The difference is so
miniscule that no one really knows which one is faster in a real-life
situations. Benchmark codes just give you bogus answers. The following
snippet, for instance, shows that the concatenation is more than an
order of magnitude slower than interpolation:

function getmicrotime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}

<snip>

I know, Chung Leong is advocating no optimization doctrine for a
long time. But, I would like to point out that your *benchmarking*
procedure is flawed in optimization world. We have APD and other tools,
your microtime() based benchmarking is not acceptable.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

Aug 10 '05 #9

P: n/a
Jerry Stuckle wrote:
R. Rajesh Jeba Anbiah wrote: <snip
Sorry, I can't agree with you. In the world of optimization, there is
no minimal or maximal optimization--there is only onething, it's
optimization. Consider a situation/expression in which you can improve
the speed to 0.000001 second--and consider the same in 1000000 loop.
So, IMHO, definitely it is worth to optimize, even it is less than a
nano second thing.


<snip explanation and examples on optimization>
The above examples are just the tip of the iceberg. Optimization is not black
and white, as you indicate. There are many, many levels of optimization involved.


True that we don't have enterprise level code metrics tools (like
devpartner) for PHP. It is true that there will be a time where the
testing department will do the code metrics, performance/stress testing
and the development team will again sit and try to improve on the next
cycle. But, my point is that, always better to code based on set of
coding standards that even care what is minimal optimization--so, that
in the next cycle you'd be better off spending time in improving other
things than doing what is known (though it proivides minimal effect).

My concern is that newbies shouldn't be impressed to follow "build
crap-remove crap" kind life cycles. FWIW, my humble opinion is to form
a coding standard that even cares these (what is trivial) things first
and work on that.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

Aug 10 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.