ECHO or PRINT or ANYTHING – Can you tell me which is FASTER in PHP?


The PHP logo displaying the Handel Gothic font.

Image via Wikipedia

ECHO or PRINT which is faster? – You can see this kind of questions a lot in every PHP blog-space and most of the interview questions ;-). Most of us must be known the answer, Google it, you will ;-).

Yes you can say “ECHO” but how sure are you? We blindly believe on our experts, I agree I do a lot. But it feels something different when you see it on your naked eyes, don’t you? I do.

Recently while surfing (reading obviously) over the internet on various topics, I came across a nice tool which will exactly tell us WHO (language construct or pre-defined function) is GOOD in PHP.

VLD ( Vulcan Logic Dumper) - by Derrick Rethans, into the Zend Engine and dumps all the opcodes (execution units) of a script. It can be used to see what is going on in the Zend Engine.

After, trying this tool I was quite happy :-) and damn confident that ECHO is faster than PRINT. I will walk you through with the installation of this tool and a simple quick example.

Installing VLD….

The following operation are performed in Ubuntu Linux and please take
 care of the settings for other Linux machine.
  1. Get the source from “svn co svn://svn.xdebug.org/svn/php/vld/trunk vld“.
  2. cd vld
  3. phpize” – you can get this command only when you install PHP-Dev “aptitude install php5-dev
  4. ./configure
  5. make install”

That’s it, you are done let’s try this thing.

Let’s TRY….

Use PHP’s command-line interface (CLI) to try this out.

Try executing the ECHO function:
$ php -d extension=vld.so -d vld.active=1 -d vld.execute=0 -r 'echo 1;'
[OR]
$ php -d extension=vld.so -d vld.active=1 -d vld.execute=0 -f file.php;
[OUTDATED - thanks to Rodrigo for waking me up]
$ php -d vld.active=1 -r 'echo 1;'
Finding entry points
Branch analysis from position: 0
Return found
filename: Command line code
function name: (null)
number of ops:  2
compiled vars:  none
line     #  op                           fetch          ext  return  operands
-------------------------------------------------------------------------------
   1     0  ECHO                                             ~0      1
         1  RETURN                                                   null

Now let's try executing the PRINT function:
$ php -d extension=vld.so -d vld.execute=0 -d vld.active=1 -r 'print 1;'

Finding entry points
Branch analysis from position: 0
Return found
filename: Command line code
function name: (null)
number of ops:  3
compiled vars:  none
line     #  op                           fetch          ext  return  operands
-------------------------------------------------------------------------------
   1     0  PRINT                                            ~0      'foo'
         1  FREE                                                     ~0
         2  RETURN

By looking at the “number of ops” value we can say that PRINT takes an extra 1 operation than ECHO to process. Now, try with your native code and see how Zend Engine process your file.

Happy Debugging!

Leave a comment

13 Comments

  1. Sahal

     /  January 13, 2011

    Good one Rakesh..

    Reply
  2. Sahal

     /  January 13, 2011

    Here can we try the execution time of bunch of php function together ?

    Reply
    • Yes you can, try it. Write various function in a single file and execute that file.

      For ex:
      Create a file functions.php and dump the following code.

      <?php
      echo 1;

      print 1;

      $tmp = array(1,2,3,4);

      $c = count($tmp);

      echo $c;

      ?>

      Now execute this file:

      php -d vld.active=1 functions.php

      Hope this helps.

      Reply
  3. Dinesh

     /  January 14, 2011

    Text written in green background is overlapping with date text.

    Solution:
    In this class “main-title h3″

    margin-right should be 135px instead of 130px;

    Reply
  4. sagar

     /  February 26, 2011

    Soooo nice…. very useful… :)

    Reply
  5. Arafin Rubab

     /  January 23, 2012

    You’re so right. I’m there with you. Your blog is definitely worth a read if anybody comes throughout it. I’m lucky I did because now I’ve obtained a whole new view of this. I didn’t realize that this issue was so important and so universal. You absolutely put it in perspective for me.

    Reply
  6. Greetings, that was a good read. It always great when anyone takes some time to inform, as you have.

    Reply
  7. Just installed VLD in Ubuntu, but when I run “php -d vld.active=1 file.php” I get no output, except for whatever my file (file.php) outputs. Any ideas?

    Reply
    • Hello Rodrigo,
      Please see the updated command on the post, thanks for pointing this out. Looks like you have to educate PHP to read the vld.so extension :-).

      -
      Rakesh.

      Reply
  8. Bernhard Wagner

     /  August 20, 2013

    php -d extension=vld.so -d vld.active=1 -d vld.execute=0 -r 'echo 1';
    should read
    php -d extension=vld.so -d vld.active=1 -d vld.execute=0 -r 'echo 1;'
    i.e. the semicolon should be inside the single quotes.

    Reply
  1. ECHO or PRINT – Which is Faster?? | chandreshmodi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 104 other followers

%d bloggers like this: