Jump to content

Recommended Posts

NOTE:

 

HEREDOC is very inefficient. Avoid using it.

 

I'm not saying your wrong, but why do you say that?

Yeah, explain please.

 

There is a benchmark on the HEREDOC. The numbers point to heredoc being faster with string replacement.

 

test_double_quote_replace : 147ms

test_eot_replace : 130ms

Link to comment
https://forums.phpfreaks.com/topic/84522--/#findComment-430834
Share on other sites

They are more intensive for PHP to parse than single or double-quoted strings, resulting in slower code execution and increased memory usage (memory usage part isn't always noticable).

 

http://www.awpti.org/test_heredoc/ - >Result: ~0.178 -> 0.233

http://www.awpti.org/test_heredoc/double_quote/ - >Result: ~ 0.133 -> 0.145

 

Take into consideration that this test was built using the CodeIgniter Benchmark class/Framework.

 

The actual times on this will be lower, but double_quote is still faster.

 

Load the page and scroll to the bottom.

 

And the code..

 

<?php
class Test_heredoc extends Controller {

        function Test_heredoc()
        {
                parent::Controller();
        }

        function index()
        {
                $this->benchmark->mark('code_start');
                $tests = 10000;
                for($i=0;$i<$tests;++$i)
                {
                        $string = 'blah-'.$i;
echo <<<EOF
This is a test $string. This should run 10000 times.<br />
EOF;
                }
                $this->benchmark->mark('code_end');
                echo 'Time taken: '.$this->benchmark->elapsed_time('code_start', 'code_end').'<br />';
                $this->view->load('test/mem');
        }

        function double_quote()
        {
                $this->benchmark->mark('code_start');
                $tests = 10000;
                for($i=0;$i<$tests;++$i)
                {
                        $string = 'blah-'.$i;
                        echo "This is a string with $string in it.<br />";
                }
                $this->benchmark->mark('code_end');
                echo 'Time taken: '.$this->benchmark->elapsed_time('code_start', 'code_end').'<br />';
                $this->view->load('test/mem');
        }
}
?>

 

In retrospect, I should have named this test_perf :)

Link to comment
https://forums.phpfreaks.com/topic/84522--/#findComment-430853
Share on other sites

When I ran your tests just now, there was a four tenths of a second difference, and memory usage was the exact same. I think it is VERY unlikely that the difference is great enough to effect the user's experience of any of my pages. While I do agree there your tests prove that heredoc is slower than double quotes when looped 10000 times, in the case of normal usage the difference is probably indistinguishable. Let's not quibble over such a minute amount of time! Saying , "heredoc is very inefficient. Avoid using it.", is a bit of an exaggeration.

Link to comment
https://forums.phpfreaks.com/topic/84522--/#findComment-430862
Share on other sites

Interesting result but I couldn't help notice how the HEREDOC test uses a longer string. Could you do the same test but with both strings exactly the same?

 

echo <<<EOF

This is a test . This should run 10000 times.<br />

EOF;

 

$string = 'blah-'.$i;

echo "This is a string with  in it.<br />";

Link to comment
https://forums.phpfreaks.com/topic/84522--/#findComment-430863
Share on other sites

I shrunk the string, now they're both about the same. I didn't think about it earlier, but these results may be poisoned by the fact my hosting server is a Virtual instance among a few others. They both swing pretty big in the results - ranging from 0.3x to 0.9x+

 

I'll actually be shifting off to a dedicated server soon - be able to do a more reliable test then.

 

As far as the difference being minor?

 

Any savings is a saving. I'll also assume you ment 4 hundredths and not 4 tenths. 4 tenths would be a significant savings (and an odd result :) ).

 

It may be a -slight- exaggeration to say 'very' on short HEREDOC's.. start pumping out serious content w/ variables using HEREDOC and you'll feel the hurt when the site gets active. I had to deal with a site that did such a thing and it was AWFUL. The performance was horrible and the memory usage difference was visible (still insignificant compared to the processing time).

 

Every page was, pretty much, a HEREDOC with about 30-40 variables being dumped to it, with breaks out of the HEREDOC to take care of 'for' loops.

 

Start running load tests. You'll see load times/processing times increase exponentially. Every little tweak matters when your site is live and high-traffic. That .04 can be the difference between crushing your server's CPU or not during a spike.

Link to comment
https://forums.phpfreaks.com/topic/84522--/#findComment-430881
Share on other sites

For comparison, here is the code and results I used / got:

 

function microtime_float() {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$heredoc_times = array();
$echo_times = array();

for ($i = 0; $i < 10000; $i++) {
$start = microtime_float();

echo <<<LINE

test test test $start test test test

LINE;

$heredoc_times[] = (microtime_float() - $start);

}

for ($i = 0; $i < 10000; $i++) {
$start = microtime_float();

echo "

test test test $start test test test

";

$echo_times[] = (microtime_float() - $start);

}

echo '<pre>
HEREDOC:
Total: 		' . array_sum($heredoc_times) . '
Average:	' . (array_sum($heredoc_times) / count($heredoc_times)) . '

echo:
Total: 		' . array_sum($echo_times) . '
Average:	' . (array_sum($echo_times) / count($echo_times));

 

HEREDOC:
Total: 		0.210633277893
Average:	2.10633277893E-5

echo:
Total: 		0.221266508102
Average:	2.21266508102E-5

 

I'm using a Core2 Duo Laptop with 2 GB RAM running the latest version of XAMPP (PHP 5.2.5, Apache 2.2.6).

Link to comment
https://forums.phpfreaks.com/topic/84522--/#findComment-430915
Share on other sites

Seems like everyone is getting different results.

 

At the console (php test.php)

 

HEREDOC:

        Total:          0.28366875648499

        Average:        2.8366875648499E-5

 

echo:

        Total:          0.22118401527405

        Average:        2.2118401527405E-5

 

 

Via apache (2.2.6 w/ php 5.2.5):

 

HEREDOC:

        Total:          0.28366875648499

        Average:        2.8366875648499E-5

 

echo:

        Total:          0.22118401527405

        Average:        2.2118401527405E-5

 

???

Link to comment
https://forums.phpfreaks.com/topic/84522--/#findComment-430935
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.