Re: echo or print ?
| From: | Stut | Date: | Sat, 21 Apr 2007 21:59:41 +0000 |
| Subject: | Re: echo or print ? | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-253450@lists.php.net to get a copy of this message | ||
Tijnema ! wrote:
On 4/21/07, Stut <stuttle@gmail.com> wrote:I wouldn't go that far. It is definitely possible to write horribly inefficient code with PHP. Believe me, I've inherited enough crap code in my lifetime to testify to that. My point was simply that you need to look at the numbers from benchmarks in perspective, and when efficiency is concerned there's almost always far bigger gains to be made than 0.009 seconds per 100,000 calls to output something. -StutTijnema ! wrote:There is a difference, echo is slightly faster. code used for benchmark: <? $start = microtime(TRUE); for ($i=0; $i<100000; ++$i) { print "ABC"; } echo sprintf("With print ($i): %0.3f\n",microtime(TRUE) - $start); $start = microtime(TRUE); for ($i=0; $i<100000; ++$i) { echo "ABC"; } echo sprintf("With echo ($i): %0.3f\n",microtime(TRUE) - $start); ?> it displays 100000 times ABC, first with the print command, and second with the echo command. Result: ABCABCABC<snip> print (100000): 0.085 ABCABCABC<snip> echo (100000): 0.076 It's not a lot, but since we are displaying data a lot, (most used function?) it will make a difference in really big scripts.This has been covered before. The difference actually depends on how you're using it, rather than whether you use print or echo. For example, your benchmark shows echo to be slightly faster, but the the following script that I wrote last time this came up shows the opposite. The only difference is that you're outputting a literal whereas I'm printing a variable.Interesting :) I see there's no big difference between echo and print, but that <?=$x?> is faster :) I've learned (not only from this) that whatever you do in PHP is fast, and that you don't need to optimize your code for speed. Unless you're hitting 100k+ hits per hour. But even then it would only save you maybe one hour per year.http://dev.stut.net/phpspeed/At the end of the day there are more important things to worry about, especially when you're talking in the region of 0.009 seconds per 100,000 calls it's not going to make anywhere near a significant difference to any script you write, even really really big ones scripts. To put it another way, you would need to make 10,000,000 calls for it to extend the runtime of your script by 1 second. Granted you might have a script that calls it 1000 times, meaning 10,000 requests to that script would "waste" 1 second. But unless you're getting twitter-like levels of traffic (they spike at over 11k hits a second) it's not worth worrying about, and I'm guessing (hoping) their devs probably wouldn't care either. Get over it and concentrate on the functionality and usability of your code rather than insignificant details like this. -Stut