1
2
3
4
5
<?php
print 'Tagabukid™<br/>';
echo 'Tagabukid™<br/>';
//returns 'Tagabukid™' on two separate lines.
?>
1
2
3
4
5<?php
print ('Tagabukid™<br/>');
echo ('Tagabukid™<br/>');
//returns 'Tagabukid™' on two separate lines.
?>
Notice: they are identical in output in either with or without parenthesis...2
3
4
5
<?php
print 'Tagabukid™<br/>';
echo 'Tagabukid™<br/>';
//returns 'Tagabukid™' on two separate lines.
?>
1
2
3
4
5<?php
print ('Tagabukid™<br/>');
echo ('Tagabukid™<br/>');
//returns 'Tagabukid™' on two separate lines.
?>
Which is faster?
Having learnt this, I decided to do some benchmarking to see which one is faster. In my tests, I loop a print/echo execution four million times. The reason for doing it so many times was to get large enough time values to make a significant comparison.
I ran each test a number of times and worked out a rough average in my head. However, the numbers produced were different enough in each iteration to clearly see that one method was faster than another.
Print, Executed in : 13.3383760452 seconds
Echo, Executed in : 9.9976708889 seconds
As a conclusion based on the above output, Echo is basically faster than print. In actuality, the speed difference between Print and Echo would be infinitesimal, it's only when we loop through thousands of times or more that we can see and measure any real difference.Echo, Executed in : 9.9976708889 seconds
Below is the benchmarking code I used to conduct this test.
1
2
3
4
5
6
7
8
9<?
$starttime=microtime(true);
for($i=0;$i<=4000000;$i++){
ob_start();
echo 'HelloWorld!<br/>';
ob_get_clean();
}
echo 'Executed in: ',microtime(true)-$starttime,' seconds';
?>
1
2
3
4
5
6
7
8
9<?
$starttime=microtime(true);
for($i=0;$i<=4000000;$i++){
ob_start();
print 'HelloWorld!<br/>';
ob_get_clean();
}
echo 'Executed in: ',microtime(true)-$starttime,' seconds';
?>
2
3
4
5
6
7
8
9<?
$starttime=microtime(true);
for($i=0;$i<=4000000;$i++){
ob_start();
echo 'HelloWorld!<br/>';
ob_get_clean();
}
echo 'Executed in: ',microtime(true)-$starttime,' seconds';
?>
1
2
3
4
5
6
7
8
9<?
$starttime=microtime(true);
for($i=0;$i<=4000000;$i++){
ob_start();
print 'HelloWorld!<br/>';
ob_get_clean();
}
echo 'Executed in: ',microtime(true)-$starttime,' seconds';
?>




0 Reactions to this post
Add CommentPost a Comment