Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts

    echo vs raw HTML - which is faster?

    In PHP 5, which is faster or requires less CPU/memory overhead?

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    The date today is <?=date('l dS \of F Y h:i:s A'?>!
    </body>
    </html>
    versus

    PHP Code:
    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    The date today is '
    .date('l dS \of F Y h:i:s A').'!
    </body>
    </html>'


  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    This is just logically speaking (with no hard data or stats to prove this), but anything not within <? ?> tags is not processed by the PHP processor and raw HTML is processed by the user's web browser. So I would say that your first example of raw HTML with your small PHP code snippet for the date has less overhead. Not to mention that it's much cleaner! There's no need to be printing or echoing out all your HTML when server-side languages like PHP, ASP, and CF allow you to write in raw HTML.

    -Shane

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by TheShaner View Post
    So I would say that your first example of raw HTML with your small PHP code snippet for the date has less overhead.
    Yes, this is what I was thinking. We have a site that uses Smarty and that templating engine puts ALL text into echo statements when it compiles the template. I am wondering what kind of hit to performance that causes but I haven't figure out a way to soundly test it.

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    I used apache-bench to do a quick test of this, outputting 50 paragraphs of lorem-ipsum (lipsum.com) to the screen with the output of time() at the end, firstly by echoing the whole string and concatenating the time(), and again with the text outside of the php-tags and a simple 'echo time()' inside them at the end.

    I ran ab for 5000 requests (results stopped changing after about 3500) with concurrency of 20.

    The first set came back at 290 requests/second, with the average time/request being 70ms.
    The second set showed 400 request/second, with the average time/request being 50ms.

    This was done locally, so network overhead will be absolutely tiny, on my rather old machine.

    It would seem then that getting rid of the echos might be a good idea, except that the second you add in anything at all 'complex' (a single db query, or any kind of iteration) the difference will become insignificant. Rather than spending time rewriting smarty, you'd be better off installing apc (or similar), indexing your database, looking into page-caching or any number of other things that will give a more noticeable increase in performance on a 'real' website, that's doing more than this simple experiment.

    (as an aside, the same content as the 2 php scripts, but as a static html file manages 3000 request/second, with a 6ms time/request)
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Nice! Thanks for the benchmarks on that one.

    I also did not realize that the output of plain HTML was such a huge order of magnitude over PHP in speed.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •