-6

Whats the speed difference, out of curiosity, of doing this:

$a = 0;
echo "<html><body>$a</body></html>";

versus

<html><body><?php echo $a; ?></body></html> 

in a PHP file?

AlliterativeAlice
  • 11,841
  • 9
  • 52
  • 69
Sam Adamsh
  • 3,331
  • 8
  • 32
  • 53
  • 6
    Negligible. Statistically irrelevant unless you do it about a million times, probably more. – Niels Keurentjes Oct 08 '14 at 21:35
  • 1
    Pretty much the same, both are somewhere in the 5-10ms range. – APAD1 Oct 08 '14 at 21:36
  • 1
    unless you're running a facebook-sized operation that's doing that sort of thing a few trillion times a day, you'll never notice the difference. – Marc B Oct 08 '14 at 21:37
  • 1
    @APAD1 you mean for a million iterations hopefully or your server definitely needs a replacement for its 386 CPU. – Niels Keurentjes Oct 08 '14 at 21:38
  • thanks for the comments, not sure why this gets downvoted. the first syntax with heredoc is much easier to read than php tags. – Sam Adamsh Oct 08 '14 at 21:38
  • 6
    Neither one of your examples contains [heredoc syntax](http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc). And the downvotes are probably because you could've benchmarked this faster yourself than write the question. – Niels Keurentjes Oct 08 '14 at 21:39
  • As others have stated, the difference is probably negligible. However, in the long run the first route is probably slower as it requires string interpolation. – Mr. Llama Oct 08 '14 at 21:39
  • Your first example isn't [heredoc](http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc), it's simply a [double quoted string](http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double) – Mark Baker Oct 08 '14 at 21:40
  • 1
    @Niels i'm aware of that. And my point stays the same. My question is relevant and so are the comments. Benchmarking doesn't take 1 minute. These extremely helpful answers came in 1 minute. So i call that a good question because i got it answered faster. – Sam Adamsh Oct 08 '14 at 21:41
  • 1
    so you think "whats 1+1=" would be a good question? –  Oct 08 '14 at 21:42
  • 1
    That 1 minute of your time is also 1 minute of several other people's time – Mark Baker Oct 08 '14 at 21:43
  • @MarkBaker Ya well i phrased it incorrectly, more like 10 minutes versus 10 seconds. – Sam Adamsh Oct 08 '14 at 21:44
  • 1
    @PaulCrovella Stack overflow can be used however the eff people want to use it to get questions answered. This kind of arrogance is what makes asking questions here a pain in the butt to deal with sometimes. Stack overflow is all about helping programmers learn FASTER. – Sam Adamsh Oct 08 '14 at 21:45
  • 3
    so your saying, "I can answer this question myself, but my time is more important than yours, so you do it for me" –  Oct 08 '14 at 21:45
  • @Dagon lets wait and see how many programmers come here from google and get their 10 minutes saved. If you are all about mass time saving the time saved by all the visitors landing from google would probably vastly outnumber the number of people answering the question. – Sam Adamsh Oct 08 '14 at 21:47
  • 1
    It's amusing how you guys are spending more time arguing with him, over whether this is a valid question, than it took to answer the question. If you're so concerned about your time, what are you still doing here? – APAD1 Oct 08 '14 at 21:48
  • 1
    i leant to program before google existed - your selfish arrogance is rather sad. –  Oct 08 '14 at 21:49
  • @Dagon its not selfish arrogance its taking advantage of the awesomeness of stack overflow. – Sam Adamsh Oct 08 '14 at 21:49
  • So your question got sufficiently answered with a benchmarking howto answer then? – mario Oct 08 '14 at 21:54
  • @mario sortof. since you are here what is the technical difference between the two methods in terms of how php executes and interpolates variables in them? – Sam Adamsh Oct 08 '14 at 21:58
  • 2
    As already said, there are only quite insignificant differences. One of the two approaches yields only EXT_STMT and ECHO bytecode, while the other boils down to ADD_STRING and PRINT. Use VLD and [proper profiling](http://www.xdebug.org/docs/profiler) to get a picture. – mario Oct 08 '14 at 22:03
  • @mario not a duplicate. That question uses print while this one uses plain html. – Sam Adamsh Oct 08 '14 at 22:14

1 Answers1

5

Let's find out:

<?php
ob_start();
$a = 0;
$time1 = microtime(true);
for ($i = 0; $i < 100000; $i++) {
    echo "<html><body>$a</body></html>";
}
$time2 = microtime(true);
for ($i = 0; $i < 100000; $i++) {
?>
<html><body><?php echo $a; ?></body></html> 
<?php
}
$time3 = microtime(true);

ob_end_clean();
echo 'Just echo: ' . ($time2 - $time1) . '<br>';
echo 'Inline PHP: ' . ($time3 - $time2) . '<br>';
?>

Result:

Just echo: 0.037185907363892
Inline PHP: 0.040054082870483

Looks like the first method is slightly faster. But the difference is so small it's negligible and definitely not a reason to output huge blocks of HTML code by echoing strings.

AlliterativeAlice
  • 11,841
  • 9
  • 52
  • 69
  • I'm genuinely curious, what about if you do `echo '', $a ,'';` (ninja edit: too lazy to put it in an eval) – Ohgodwhy Oct 08 '14 at 21:43
  • theres a site that has already done it for you: http://www.phpbench.com/ –  Oct 08 '14 at 21:43
  • 1
    Your code does not produce conclusive results in a single attempt, see https://eval.in/203304. F5 a few times to see both approaches 'win'. This is because it's fairly identical at the opcode level in PHP internally. – Niels Keurentjes Oct 08 '14 at 21:45
  • 1
    I immediately envisioned the Tootsie Pop commercial from the 70s when I read the opening of your answer. "Let's find out, a-wuhun, a-twohu, a-three. Three." – APAD1 Oct 08 '14 at 21:45
  • @NielsKeurentjes The first approach is consistently faster in my environment (Apache running on Windows) but I can definitely imagine the results being equal or reverse in other environments. – AlliterativeAlice Oct 08 '14 at 21:46
  • @Ohgodwhy Indistinguishable from the first method (everything in one string) for me. – AlliterativeAlice Oct 08 '14 at 21:47