Re: echo or print ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 4/21/07, Stut <stuttle@xxxxxxxxx> wrote:
Tijnema ! wrote:
> On 4/18/07, Richard Lynch <ceo@xxxxxxxxx> wrote:
>> On Tue, April 17, 2007 1:40 am, Christian Haensel wrote:
>> > Whenever I see people put their code up for review, I realize they
>> > mostly
>> > use print instead of echo, while I am using echo 99% of the time.
>> > Actually,
>> > I can't even remember when I last used the regular print.
>>
>> There used to be a difference, but not really any more, I don't think.
>>
>> Or does print still not allow multiple arguments?...
>>
>> > What do you guys use, and what is the advantage (if ther is any) of
>> > print
>> > over echo? And I am not talking about print_r or anything, just the
>> > regular
>> > print. :o)
>>
>> I use echo, because I'm old, and got in the habit, back when print()
>> was a function and echo was a language construct, and only echo let
>> you have as many args with commas as you wanted.
>>
>> But there's no significant difference, as far as I know.
>
> 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.

       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

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.

Tijnema


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux