
![]() |
m0nkeymafia
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 PHPThis is simply because PHPhas 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 ![]() perkiset
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. m0nkeymafia
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
mrsdf
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.
![]() nop_90
Joining an array of strings.
This is for python, but the same technique forPHPshould 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. perkiset
quote author=m0nkeymafia link=topic=482.msg3121#msg3121 date=1188844421 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 PHPit is $newString = implode($delimiter, $array) and you're right, works brilliantlym0nkeymafia
nice one perk, thats why i love this forum
![]() 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 PHPhas to store the current string in memory, and then finally echo it out.So if you did this: echo 'blah' . $var . 'blah'; PHPwould 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 phpto whack the next bit out to the output buffer straight awaymaybe im wrong tho, perk? show me the light u george bush lover perkiset
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 PHPneeds 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 nutballs
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? perkiset
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...
timjohn
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! ![]() perkiset
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: <? phpecho '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. phpfile: (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_" : "" ![]() } // ]]></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', '"> <a href="http://www.mysql.com/" target="_blank"><img id="powered-mysql" src="', $settings['images_url'], '/powered-mysql.gif" alt="', $txt['powered_by_mysql'], '" width="54" height="20" style="margin: 5px 16px;" onmou seover="smfFooterHighlight(this, true);" onmouseout="smfFooterHighlight(this, false);" /></a><a href="http://www. php.net/" target="_blank"><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);" /></a></td> <td valign="middle" align="center" style="white-space: nowrap;"> ', theme_copyright(), ' <br/><br/> <span class="smalltext">'. $settings['custom_copyright'] . '</span> </td> <td width="28%" valign="middle" align="', !$context['right_to_left'] ? 'left' : 'right', '"> <a href="http://validator.w3.org/check/referer" target="_blank"><img id="valid-xhtml10" src="', $settings['images_url'], '/valid-xhtml10.gif" alt="', $txt['valid_xhtml'], '" width="54" height="20" style="margin: 5px 16px;" onmou seover="smfFooterHighlight(this, true);" onmouseout="smfFooterHighlight(this, false);" /></a><a href="http://jigsaw.w3.org/css-validator/check/referer" target="_blank"><img id="valid-css" src="', $settings['images_url'], '/valid-css.gif" alt="', $txt['valid_css'], '" width="54" height="20" style="margin: 5px 16px;" onmou seover="smfFooterHighlight(this, true);" onmouseout="smfFooterHighlight(this, false);" /></a></td> </tr> </table>'; Now perhaps you're a better man than I, but that just makes my eyes hurt. Paging NutBalls: Ugly PHPCoder Needs To Be Shot Department on line 2...![]() /p nutballs
hmm... i guess Id better not show what i been working on the past few weeks... lol i have a few of those.
![]() timjohn
damn, that is some heinous looking code!
![]() phpstill, but i at least think that i produce nice looking code with readable formatting. makes debugging so much easier after the fact. |

Thread Categories

![]() |
![]() |
Best of The Cache Home |
![]() |
![]() |
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads