1

I'm starting to incorporate heredocs into my code because of the benefits described herein: What is the advantage of using Heredoc in PHP ?

I was wondering if heredocs are slower than using echo with escapes, especially when it comes to thousands of lines of code and other operations such as a DB query, file I/O, a loop over a moderately-sized collection, etc. (thanks @delnan). By "much" I mean if it's enough of a performance issue that most seasoned programmers don't use it for big projects [e.g. creating a CMS]. I've attempted a test below.

EDIT

Granted the difference is microseconds and the test below is not a great example (I was just trying to figure it out myself), but my question is not specifically referring to just echoing strings.

$echoStmt = "The point of the \"argument\" was to illustrate the use of here documents";

$l = 100;
$start = microtime(TRUE);

while( $l-- ) {
    echo $echoStmt;
}

$end = microtime(TRUE);
$diff = $end - $start;

echo $diff;
// prints 24 microseconds

echo "  |  ";

$l = 100;
$start = microtime(TRUE);

$heredocStmt = <<<EOF
The point of the "argument" was to illustrate the use of here documents
EOF;

while( $l-- ) {
    echo $heredocStmt;
}

$end = microtime(TRUE);
$diff = $end - $start;
echo $diff;
// prints 220 microseconds
Community
  • 1
  • 1
Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120
  • 1
    While you're at it, try measuing a DB query, file I/O, a loop over a moderately-sized collection, etc. –  Nov 26 '11 at 14:27
  • 2
    The tokenizer/parser will convert both to string values. So it couldn't even technically make a measurable difference for the runtime. – mario Nov 26 '11 at 14:30
  • An example for benchmark :- http://www.phpbench.com/source/test11/6/ – ajreal Nov 26 '11 at 15:56

6 Answers6

3

If it's PHP, then guaranteed there are other more important things worth optimizing . i wouldn't sweat HEREDOC sttrings ..

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
3

You're using microtime() wrong anyways, so your results are totally useless/meaningless. When you use a bare microtime(), you get back a STRING, which is usec sec in format. You need to use microtime(TRUE) to get back a true floating point number that you can directly subtract.

e.g. you might get

$start = '999 100'  // 100.999 seconds
$end  = '001 101' // 101.001 seconds

$diff = 001 - 999 = -998 microseconds; // huh?

$start = microtime(TRUE); // 100.999
$end = microtime(TRUE) ; // 101.001;
$diff = 101.001 - 100.999 = 0.002; // proper result.
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • At last! One really correct and useful comment in this thread :) – RReverser Nov 26 '11 at 14:34
  • @RReverser Marc is right of course, but does it change the end result that *it doesn't matter* performance-wise which one you use? I don't think so. – Pekka Nov 26 '11 at 14:54
3

Do the math: A microsecond is one millionth of a second.

Even with hundreds of lines, any performance difference (if there really is one - see @NikiC's and @Marc's answers) will not matter. Use whichever form is most readable, understandable, and best documentable instead.

There are much more expensive operations in a PHP script that are really worth optimizing - on top of the list is usually database calls.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Also note that he is a) measuring incorrectly and b) string type won't affect runtime but only compile time (and microtime measures at RT) – NikiC Nov 26 '11 at 14:29
2

Strings are parsed at compile time, so what your code is testing is just the time to look up a variable and echo it - nothing more. If you'd want to measure the parse time difference you'd need to hook into the zend_language_scanner - and would find that there is no measurable difference.

NikiC
  • 100,734
  • 37
  • 191
  • 225
0

If you're using an optimizer such as eaccelerator, there's absolutely no difference at all, since the compiled version of the document will treat both as strings anyway. In compiling the script there may be a millionth of a second in time difference, but I wouldn't care too much about that.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
-1

I don't understand what "benefits" you are talking about.

Every benefit in the yonder answer is merely a cheating - the guy took the worst possible alternative and compared it with heredoc! And even that way heredoc become uglier.

In fact, all of the examples are more concise and readable using strings, not heredoc:

$sql = <<<SQL
select *
  from $tablename
 where id in [$order_ids_list]
   and product_name = "widgets"
SQL;

vs.

$sql = "select * from $tablename
          where id in [$order_ids_list]
           and product_name = 'widgets'";

or. one-liner

$x = 'The point of the "argument" was to illustrate the use of here documents';

vs 3-liner

$x = <<<EOF
The point of the "argument" was to illustrate the use of here documents
EOF;

Where are these benefits?!

So, heredoc's unnecessary uglyness is the only reason of it's very limited use.

And performance difference is always negligible.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345