Single versus Double Quotes in Smarty - How to Speed Up Your Site a Little

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Locked
kendo451

Single versus Double Quotes in Smarty - How to Speed Up Your Site a Little

Post by kendo451 »

It always helps to read and understand the syntax of the programming and templating languages that you use.

According to Joel Spoelsky, string operations are generally the most resource intensive functions.

Searching a string is comparable to a painter who is told to paint a yellow line on the road, but he leaves the paint bucket at the starting point.  Each time he walks back to the bucket to re-dip his brush, the trip gets longer. If the line is only 20' long, this won't take too long.  But if the line is 300' long, the rate of progress slows linearly with the distance.

Parsing strings unnecessarily slows down the rendering of your web page by the server.  The longer the string, the more cpu time it takes to parse it.

In SMARTY tags, double quotes are parsed for variables, while single quotes are taken as a literal string.

So this:

{content block="Block Number 1" oneline="true" assign="block_1"}

requires three parse operations.

But this:

{content block='Block Number 1' oneline='true' assign='block_1'}

requires none.

The only time that you want to use double quotes in a smarty tag is when the string contains a variable reference such as this:

{assign var='pagetitle' value="This is the $page_alias page."}

Assuming the page alias is "home" the tag above will produce the following value in $pagetitle:
"This is the home page."

The CMSMS default templates that come with {cms_module module="something" template="somethingelse" etc.} will run faster if you convert them to single quotes.
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: Single versus Double Quotes in Smarty - How to Speed Up Your Site a Little

Post by jmcgin51 »

kendo451 wrote: It always helps to read and understand...
WHAT??????  You can't be serious!  You can't really expect me to both read AND understand all that stuff!!!!!!  :D

For real, though - very nice tip!!
SimonSchaufi

Re: Single versus Double Quotes in Smarty - How to Speed Up Your Site a Little

Post by SimonSchaufi »

kendo451 wrote: According to Joel Spoelsky, string operations are generally the most resource intensive functions.
Source?
kendo451

Re: Single versus Double Quotes in Smarty - How to Speed Up Your Site a Little

Post by kendo451 »

Spolsky, Joel, "Joel on Software", Apress, 2004, Chapter 2, Back to Basics, "Shlemiel the Painter's Algorithm"

I paraphrased him a lot.  But his basic argument is that algorithms that search strings generally take more time relative to length squared.

Smarty - Single vs. Double Quotes

The most direct statement of this issue is here, however they do not address the performance hit from using double quotes:
http://www.nusphere.com/php/templates_smarty_functions.htm
Smarty will recognize assigned  variables  embedded in "double quotes" so long as the variable name contains only numbers, letters, under_scores and brackets[]. See naming  for more detail.
http://www.smarty.net/manual/en/language.syntax.quotes.php

Code: Select all

{* wont work as its single quotes ie no variable substitution *}
{include file='$module.tpl'}
http://www.smarty.net/manual/en/language.function.include.php
Regardless, single quotes in SMARTY are not checked for variable substitution, but double quotes ARE.  So, using single quotes simplifies your code, and uses less processing power, making it run faster, if only slightly.
Last edited by kendo451 on Fri Dec 18, 2009 3:40 am, edited 1 time in total.
kendo451

Re: Single versus Double Quotes in Smarty - How to Speed Up Your Site a Little

Post by kendo451 »

If you have page caching, this will not affect performance much.  But for pages that are always dynamic, and get recompiled every time it does.
smansman
Forum Members
Forum Members
Posts: 28
Joined: Wed Apr 23, 2008 6:08 pm

Re: Single versus Double Quotes in Smarty - How to Speed Up Your Site a Little

Post by smansman »

Very interesting thanks for sharing this
hexdj
Power Poster
Power Poster
Posts: 415
Joined: Sat Mar 24, 2007 8:28 am

Re: Single versus Double Quotes in Smarty - How to Speed Up Your Site a Little

Post by hexdj »

Very good tip indeed Kendo!, thanks!  ;D
Locked

Return to “Tips and Tricks”