Jump to content

Comparison printf to echo

- - - - -

  • Please log in to reply
6 replies to this topic

#1
xXAlphaXx

xXAlphaXx

    Learning Programmer

  • Members
  • PipPipPip
  • 85 posts
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? :)

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
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

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
echo is a language construct and does not return a value, thus it is faster.

#4
xXAlphaXx

xXAlphaXx

    Learning Programmer

  • Members
  • PipPipPip
  • 85 posts
Ah, so if I do not have to format much I will want to use echo?

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
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. :)

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
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

#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
Just a quick benchmark.

<?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