473,465 Members | 4,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String manipulation, optimize

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

Similar topics

4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
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...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
9
by: Michael D. Ober | last post by:
OK, I can't figure out a way to optimize the following VB 2005 code using StringBuilders: Public Const RecSize as Integer = 105 Private buffer As String Public Sub New() init End Sub...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
17
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
15
by: DL | last post by:
say, we have the following: // declare an array myArray = ; // short hand? same as myArray = new Array ? // populate it myArray = 0; myArray = 0; myArray = 1; myArray = 0;
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.