{"id":536,"date":"2006-05-05T21:48:59","date_gmt":"2006-05-05T19:48:59","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2006\/05\/05\/speeding-things-with-php-output-buffer\/"},"modified":"2007-05-08T15:54:55","modified_gmt":"2007-05-08T13:54:55","slug":"speeding-things-with-php-output-buffer","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2006\/05\/speeding-things-with-php-output-buffer\/","title":{"rendered":"Speeding Things with PHP Output Buffer"},"content":{"rendered":"<p>While playing a bit with <a href=\"http:\/\/www.xdebug.org\/\">Xdebug<\/a> (a profiling tool for PHP), I&#39;ve come across something I didn&#39;t know and that I frankly found a bit odd : how to use output buffering (PHP function <em>ob_start()<\/em>) to speed up some stuff.<\/p>\n<p>In a few words, just so you know if this is obvious noob stuff for you and you can skip this article : <\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">ob_start();\r\necho a lot of stuff;\r\n$result = ob_get_contents();<\/code><\/pre><\/div>\n<p>can be a lot faster than a straight<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">$result = a lot of stuff<\/code><\/pre><\/div>\n<p><!--more--><\/p>\n<p>Say you have a very complex routine that errrr&#8230; prints a string with a lot of &#39;a&#39;.<\/p>\n<p>Straight and simple code would be :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-3\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">$s = &#039;&#039;;\r\nfor ($i=0; $i &lt; $loops; $i++) $s .= &#039;a&#039;;<\/code><\/pre><\/div>\n<p>Now, with output bufferization you could make it like :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-4\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">$s = &#039;&#039;;\r\nob_start();\r\nfor ($i=0; $i &lt; $loops; $i++) echo &#039;a&#039;;\r\n$s = ob_get_contents();\r\nob_end_clean();<\/code><\/pre><\/div>\n<p>Let&#39;s benchmark a bit these two snippets :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-5\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\n$iterations = 50000;\r\n\r\nfunction straight($loops = 100) {\r\n\tfor ($i=0; $i &lt; $loops; $i++) $s .= &#039;a&#039;;\r\n\treturn $s;\r\n}\r\n\r\nfunction buffer($loops = 100) {\r\n\tob_start();\r\n\tfor ($i=0; $i &lt; $loops; $i++) echo &#039;a&#039;;\r\n\t$s = ob_get_contents();\r\n\tob_end_clean();\r\n\treturn $s;\r\n}\r\n\r\n$lotsofa1 = straight($iterations);\r\n$lotsofa2 = buffer($iterations);\r\n?&gt;<\/code><\/pre><\/div>\n<p><strong>Results<\/strong> : on my test machine, straight code took about <em>60 ms<\/em> to execute while buffered code took <em>50 ms<\/em>. Holy beep, the weirdo syntax is 15% faster !? I guess you save time by manipulating once the result variable, when you get the buffer content, but that was quite surprising for my PHP beginner self.<\/p>\n<p>While I was at it, I pushed it a bit further and tried to think about interesting ways to use this. I thought : maybe it&#39;s faster for example to <em>include()<\/em> a file into a buffer than simply using <em>file_get_content()<\/em> ?<\/p>\n<p>To test this, I used the following :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-6\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\n\r\nfunction bench_readfile($file) {\r\n\tob_start();\r\n\treadfile($file);\r\n\t$s = ob_get_contents();\r\n\tob_end_clean();\r\n\treturn $s;\r\n}\r\n\r\nfunction bench_filegetcontents($file) {\r\n\treturn file_get_contents($file);\r\n}\r\n\r\nfunction bench_include($file) {\r\n\tob_start();\r\n\tinclude($file);\r\n\t$s = ob_get_contents();\r\n\tob_end_clean();\r\n\treturn $s;\r\n}\r\n\r\n$content1 = bench_readfile(&#039;file1.txt&#039;);\r\n$content2 = bench_filegetcontents(&#039;file2.txt&#039;);\r\n$content3 = bench_include(&#039;file3.txt&#039;);\r\n\r\n?&gt;<\/code><\/pre><\/div>\n<p>Files file1.txt, file2.txt and file3.txt were copies of the same file, but I wanted to be sure that no function would benefit from cached data.<\/p>\n<p><strong>Results<\/strong> : the include() block took <em>51 ms<\/em> to execute, <em>readfile()<\/em> took 7.3 ms and <em>file_get_contents()<\/em> won it in just 2 ms.<\/p>\n<p><strong>Conclusion<\/strong> : Well, no surprise here. Hopefully built-in function are sometimes optimized enough :\u00c3\u017e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While playing a bit with Xdebug (a profiling tool for PHP), I&#39;ve come across something I didn&#39;t know and that I frankly found a bit odd : how to use output buffering (PHP function ob_start()) to speed up some stuff. In a few words, just so you know if this is obvious noob stuff for you and you can skip\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[2,10,74,164],"class_list":["post-536","post","type-post","status-publish","format-standard","hentry","tag-code","tag-php","tag-stats","tag-xdebug"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=536"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/536\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}