The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 21, 2019, 02:01:17 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Double vs Single Quotes  (Read 4794 times)
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« on: September 03, 2007, 10:08:11 AM »

I've known this was the case for a while, but I read a blog post recently [sorry no link, forgot where] that gave some actual figures on the speed differences.

For those that dont know single quotes are much faster than double quotes in PHP
This is simply because PHP has to iterate through every character looking for escape characters and variables within a double quote string.  Single quote strings are just treated as plain text.

According to the test this guy did single quotes are about 6 times faster than double quotes.  Which in a large site, where your getting a lot of traffic it could seriously help shave off load times and keep the next big upgrade away!

I'm a huge fan of optimizing sites not buying bigger servers Smiley
Logged

I am Tyler Durden
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: September 03, 2007, 10:16:59 AM »

Exactly MM - single quotes represent static strings, while double represent strings that are to be evaluated. I never use doubles except when I want to evaluate something like you would a sprintf.

Interestingly, the boys that wrote the SMF code do this a lot:

echo 'this is ', $aVar, ' yet another way ', print_r($someArr, true), $anObj->aFunc(), ' to achieve the same ', ' but I think it\'s faster';

Note the combination of things that you want echod back out - it's tiring to look at huge echo statements like this in the code, but from a speed perspective makes sense.

I dunno, interesting technique. I use it occassionally.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #2 on: September 03, 2007, 11:33:41 AM »

aye, using the . concatonation technique is faster than using sprintf to place variables in strings, about 5 times as fast for this particular test, makes sense i suppose
Logged

I am Tyler Durden
mrsdf
Rookie
**
Offline Offline

Posts: 20



View Profile
« Reply #3 on: September 03, 2007, 11:56:49 AM »

This is one of those things I never paid any attention to and it could give me considerably better performance in some scripts... thanks MM. 
Nerd /me goes to rewrite some stuff
Logged

We're sp4mmin', we're sp4mmin', I hope you like sp4mmin' too...
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #4 on: September 03, 2007, 07:09:39 PM »

Joining an array of strings.
This is for python, but the same technique for PHP should be used.
(Any language for that matter and i think i mention it before).

you have an array
A=["aaaa","bbbb","cccc","dddd"]
you want to join all pieces together
go
"".join(A)
and you end up with
"aaaabbbbccccdddd"
you can use variants of that, lets say u have template system etc
with very long arrays improvement is astronomical.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: September 03, 2007, 08:37:48 PM »

aye, using the . concatonation technique is faster than using sprintf to place variables in strings, about 5 times as fast for this particular test, makes sense i suppose

It is indeed - however if you look at my post, in that example it's echo this comma that comma another, not concatenated - the echo directive allows you to echo multiple items *without* concatenation, which is faster still.

@nop: in PHP it is $newString = implode($delimiter, $array) and you're right, works brilliantly
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
m0nkeymafia
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« Reply #6 on: September 04, 2007, 07:44:14 AM »

nice one perk, thats why i love this forum Smiley

Edit: Just started using commas instead of periods. Yay for new knowledge etc

Oh and im guessing that the reason its faster is as follows [purely speculation though - perk?]
When you concatenate PHP has to store the current string in memory, and then finally echo it out.

So if you did this:
echo 'blah' . $var . 'blah';
PHP would have to do this:
store 'blah';
store 'blah<contents of var>'
store 'blah<contents of var>blah'
then echo it out

with the comma your merely telling php to whack the next bit out to the output buffer straight away

maybe im wrong tho, perk? show me the light u george bush lover
« Last Edit: September 04, 2007, 09:13:44 AM by m0nkeymafia » Logged

I am Tyler Durden
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #7 on: September 22, 2007, 11:38:37 PM »

Sorry MM, I just saw this post... I don't know why it didn't show up in my All Unread before. Hmmm... makes me concerned a bit...

Anyhoo, you are correct: if I concatenate may strings, then PHP needs to do a lot of mallocs and frees to build the final string... if you do this:

echo 'this', 'that', 'yet', 'another'

then the strings are absolutely static, there's no concatenation or mallocing going on... it's as fast and clean as can be. A simple thing, but effective and important.

Sorry again man!
/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #8 on: September 23, 2007, 09:13:57 AM »

so comma vs period.

echo 'hi' , 'my ','name ','is: ',$name;
$a = 'hi' , 'my ','name ','is: ',$name;

both of those would work right? or does the comma only work for straight to echo?
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #9 on: September 23, 2007, 11:08:01 AM »

echo yes, var assignment no - under the hood, echo is really a function, even though it has no parens. Comma-d variable assignment would not make sense IMO...
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
timjohn
Rookie
**
Offline Offline

Posts: 15


View Profile
« Reply #10 on: October 15, 2007, 11:22:32 PM »

hermm, interesting about the comas versus periods. i switched a couple of months ago from using

echo "hello there $name";

to

echo 'hello there '.$name;

i am all for optimizing as you write, whether or not you think speed or performance is an issue at that juncture. so now i will be switching to

echo 'hello there ', $name;

thanks for the tip!  Grin
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #11 on: October 15, 2007, 11:47:16 PM »

Perfect evolution TJ -

Of course, the "$string" method has it's place, as does concatenation, the echo item,item,item method is probably the fastest of all and a great way to do it... albeit probably the least readable a year later LOL... and the SMF boys do that technique NO favors in the way that they use it throughout their code. Horrible to look at, simply horrible.

Recommendation:
If you're going to do this a lot, use a programmatic style that is REALLY readable a long time from now. My personal favorite is like this:

Code:
<?php
echo 
'This,',
' That',
' And Another';
?>


because looking at "single notions" on a single line is easier for me later, and the CRLF doesn't dick with compiler time at all.

Here, for example, is a line from a typical smf index.php file: (This is a SINGLE ECHO STATEMENT)

   echo '
   <div id="footerarea" style="text-align: center; padding-bottom: 1ex;', $context['browser']['needs_size_fix'] && !$context['browser']['is_ie6'] ? ' width: 100%;' : '', '">
      <script language="JavaScript" type="text/javascript"><!-- // --><![CDATA[
         function smfFooterHighlight(element, value)
         {
            element.src = smf_images_url + "/" + (value ? "h_" : "") + element.id + ".gif";
         }
      // ]]></script>
      <table cellspacing="0" cellpadding="3" border="0" align="center" width="100%">
         <tr>
            <td width="28%" valign="middle" align="', !$context['right_to_left'] ? 'right' : 'left', '">
               <img id="powered-mysql" src="', $settings['images_url'], '/powered-mysql.gif" alt="', $txt['powered_by_mysql'], '" width="54" height="20" style="margin: 5px 16px;" onmouseover="smfFooterHighlight(this, true);" onmouseout="smfFooterHighlight(this, false);" />
               <img id="powered-php" src="', $settings['images_url'], '/powered-php.gif" alt="', $txt['powered_by_php'], '" width="54" height="20" style="margin: 5px 16px;" onmouseover="smfFooterHighlight(this, true);" onmouseout="smfFooterHighlight(this, false);" />
            </td>
            <td valign="middle" align="center" style="white-space: nowrap;">
               ', theme_copyright(), '
               


               <span class="smalltext">'. $settings['custom_copyright'] . '</span>
            </td>
            <td width="28%" valign="middle" align="', !$context['right_to_left'] ? 'left' : 'right', '">
               <img id="valid-xhtml10" src="', $settings['images_url'], '/valid-xhtml10.gif" alt="', $txt['valid_xhtml'], '" width="54" height="20" style="margin: 5px 16px;" onmouseover="smfFooterHighlight(this, true);" onmouseout="smfFooterHighlight(this, false);" />
               <img id="valid-css" src="', $settings['images_url'], '/valid-css.gif" alt="', $txt['valid_css'], '" width="54" height="20" style="margin: 5px 16px;" onmouseover="smfFooterHighlight(this, true);" onmouseout="smfFooterHighlight(this, false);" />
            </td>
         </tr>
      </table>';

Now perhaps you're a better man than I, but that just makes my eyes hurt.

Paging NutBalls: Ugly PHP Coder Needs To Be Shot Department on line 2...


/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #12 on: October 16, 2007, 07:31:02 AM »

hmm... i guess Id better not show what i been working on the past few weeks... lol i have a few of those.  ROFLMAO
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
timjohn
Rookie
**
Offline Offline

Posts: 15


View Profile
« Reply #13 on: October 17, 2007, 08:00:46 AM »

damn, that is some heinous looking code!  Vomit i may be a semi-hack at php still, but i at least think that i produce nice looking code with readable formatting. makes debugging so much easier after the fact.
Logged

No links in signatures please
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!