The compiled and cached template always use double quotes to output text.
Example: echo "<td class="blank"></td>"
As far as I know, text in single quotes is performing faster because PHP does not need to scan for variables inside it.
Also it would remove the need to escape the double quotes in the text which is very common in HTML.
I'd like to know why twig still uses doubles quotes to output static text?
have you benchmark for this approval?
I didn't do benchmark tests myself but this issue has been discussed and benchmarked quite often by others. Just do a search. It has been shown that 'Foo ' . $var . ' bar' is not faster than "Foo $var bar".
So there is no real difference between these two styles. But in Twigs case the situation is different.
Twig does not concatenate strings and echos every static and every variable part solely. This means using single quotes will definitely increase performance as PHP does not need to search for variables inside them.
This topic has already been discussed some time ago. Have a look here #156 and here #304.
I see. The performance boost seems to be marginal. But you also need to consider disk space.
I tested the changes in #304. This small change reduced the overall template cache size in my project by 1.4%. The reason is that often used double quotes and tabs do not get escaped.
In big projects it will save several megabytes disk space. I could imagine it might also reduce the memory footprint of the application because PHP has to deal with less source code.
Considering the facts that single quotes result in slightly better performance and reduced disk space I don't understand why twig insists in double quotes.
It will save megabytes of disk space? How is that an issue for anyone anywhere?
Since practically forever the performance difference between single and double quotes has been virtually non-existent. The decision between one or the other is nothing more than personal preference.
Big -1 here.
@Anomareh is right, and there's been tons of benchmarks that have disproven this. There is _no_ conclusive proof as to single quotes or double quotes being better for performance. It's virtually the same; iirc, there's only been a difference of microseconds (EDIT: actually, nanoseconds as I dig up the articles again) discovered in high-repetition benchmarks, which easily falls within the margin of error.
As far as cache size, that will vary between the templates themselves as well; it's very possible that some templates would be significantly larger if single quotes were used.
Let's be honest, storage is very cheap. We're dealing with bytes here (in the end, it would probably be a single megabyte saved at the very most, if you included dozens of copies of stale templates); twig doesn't have to perform like a minifier, and it really shouldn't.
And finally, most users are running an opcache anyways, which makes the size of the source code stored irrelevant.
echo "$a" < echo $a
echo "$a test" < echo $a,' test'
echo "hello" = echo 'hello'
@stealth35 you should probably state how many iterations you used to get the benchmark times, and how many times you ran the benchmark to get the averaged result.
a test with 10000000 iterations
test : echo "$a" vs echo $a
float(4.0952851772308)
float(3.4572939872742)
test : echo "$a test" vs echo $a,' test'
float(4.7237920761108)
float(3.4448008537292)
test : echo "hello" vs echo 'hello'
float(3.0642449855804)
float(3.0639321899414)
with vld :
echo "$a" : eop 3
echo $a : eop 2
echo "$a test" : eop 4
echo $a,' test' : eop 3
echo "hello" : eop 2
echo 'hello' : eop 2
Just to clear up all the misinformation you find on the internet about the performance of single vs. double quotes:
When a PHP program executes there are two phases, first compile time, where the source code is tokenized, parsed and converted to an opcode stream, and second run time, where the code is executed.
The choice of quotes now has absolutely no effect on run time. That's why @stealth35 couldn't measure a difference in execution time in his benchmark - there just is none.
The choice of quotes only affects the time required for the first step, the compilation. More precisely it even only affects the first step of the compilation, the tokenization.
And even there the difference is minimal. (For those who want to know exactly: Tokenization of single quotes does pretty much exactly the same as tokenization of double quotes, the only difference being the the switch construct has to handle two more cases and a call to zend_scan_escape_string is done.)
To sum up:
a) There is no difference in run time. I.e. if you use an opcode cache (you should!) there can't be even a theoretical difference.
b) Even if you consider compile time the difference will not be measurable.
Thus: Really, don't try to optimize code by using a certain quote type. It won't help!
Thanks @nikic for the explanation and the great references.
@nikic you just skipped to argument disk space. I also wonder why currently tabs are encoded as t instead of letting them untouched.
Anyway, even if compile time, which is part of the run time for most people (for example in dev environment or on shared host with no opcode cache), is negligible and disk usage doesn't matter, I don't understand why this tiny optimization is not applied? It's just a one-liner to change and is BC. So we have 2 tiny pros and zero cons...
To satisfy my inquisitiveness, I tried this.
function timeFunc($function, $runs)
{
$times = array();
for ($i = 0; $i < $runs; $i++)
{
$time = microtime();
call_user_func($function);
$times[$i] = microtime() - $time;
}
return array_sum($times) / $runs;
}
function Method1()
{
$config["test"] = "http://$_SERVER[HTTP_HOST]";
$foo = "some words $config[test]";
for ($i = 0; $i < 1000000; $i++)
$t = "these are $foo";
}
function Method2()
{
$config['test'] = 'http://' . $_SERVER['HTTP_HOST'];
$foo = 'some words' . $config['test'];
for ($i = 0; $i < 1000000; $i++)
$t = 'these are ' . $foo;
}
print timeFunc('Method1', 1000) . "\n<br>";
print timeFunc('Method2', 1000) . '\n<br>';