I understand that printf is like echo on steroids as it can do much more than echo, but because of this is their a reason why you will ever use echo because of the superior functionality of printf?
My guess:
Does echo use less memory and processing power so you will want to use it for printing a simple string?
Can someone clarify this for me please? :)
6 replies to this topic
#1
Posted 05 February 2011 - 10:53 AM
|
|
|
#2
Posted 05 February 2011 - 11:07 AM
echo let's php's internal string handling do the work with the (almost non-existing) formatting while printf enables a lot of special formatting, especially with number formatting, alignments and spaces and stuff as the original c-function printf have in it's pockets. (I have a guess that the f in printf stands for formatted, i.e. print formatted)
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#3
Posted 05 February 2011 - 11:44 AM
echo is a language construct and does not return a value, thus it is faster.
#4
Posted 05 February 2011 - 11:51 AM
Ah, so if I do not have to format much I will want to use echo?
#5
Posted 05 February 2011 - 12:22 PM
Well, you'll probably only be saving a few milliseconds over over hundreds of echos, but in the grand scheme of things, it is really negligible. I'm sure Alexander can answer this better than I can, but when your code gets sent through the php interpreter, I believe functions will be converted into language constructs. So, eventually, a printf statement will be represented internally as a bunch of echo statements.
It really comes down to is preference. I've been coding in PHP for 6 years, and there hasn't been an instance where I've had to choose between one or the other. I always use echo, simply because I like to save a few milliseconds. :)
It really comes down to is preference. I've been coding in PHP for 6 years, and there hasn't been an instance where I've had to choose between one or the other. I always use echo, simply because I like to save a few milliseconds. :)
#6
Posted 05 February 2011 - 02:24 PM
I think it's mainly there because PHP uses alot of basic C stuff and to let C coders use their beloved printf
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#7
Posted 05 February 2011 - 02:40 PM
Just a quick benchmark.
31.206846952438
34.929438114166
31.395988941193
40.841372013092
27.583498001099
212.38509106636
212.15578699112
182.63980221748
209.45250082016
202.31138110161
echo is about 6 times faster than printf.
<?php
for($i = 0; $i < 5; $i++) {
$start = microtime(true);
ob_start();
for($j = 0; $j < 100000000; $j++) {
echo " ";
}
ob_end_clean();
echo microtime(true) - $start . "\n";
}
echo "\n\n";
for($i = 0; $i < 5; $i++) {
$start = microtime(true);
ob_start();
for($j = 0; $j < 100000000; $j++) {
printf(" ");
}
ob_end_clean();
echo microtime(true) - $start . "\n";
}
?>
31.206846952438
34.929438114166
31.395988941193
40.841372013092
27.583498001099
212.38509106636
212.15578699112
182.63980221748
209.45250082016
202.31138110161
echo is about 6 times faster than printf.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









