...

View Full Version : echo vs raw HTML - which is faster?



bauhsoj
06-28-2007, 06:12 PM
In PHP 5, which is faster or requires less CPU/memory overhead?


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


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>';

TheShaner
06-28-2007, 06:29 PM
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

bauhsoj
06-28-2007, 06:34 PM
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.

GJay
06-28-2007, 07:38 PM
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)

bauhsoj
06-28-2007, 07:44 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum