
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.
- Get the source from “svn co svn://svn.xdebug.org/svn/php/vld/trunk vld“.
- “cd vld“
- “phpize” – you can get this command only when you install PHP-Dev “aptitude install php5-dev“
- “./configure“
- “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!
Good one Rakesh..
Here can we try the execution time of bunch of php function together ?
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.
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;
[…] echo-print-which-is-fast-php […]
Soooo nice…. very useful… 🙂
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.
Greetings, that was a good read. It always great when anyone takes some time to inform, as you have.
Thanks, I will try my best to give more information.
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?
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.
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.
Thanks Bernhard for pointing the mistake.