note 66391 added to function.echo

From: Date: Sun, 21 May 2006 02:39:56 +0000
Subject: note 66391 added to function.echo
Groups: php.notes 
Request: Send a blank email to php-notes+get-111749@lists.php.net to get a copy of this message
I have written a script to benchmark the several methods of outputting data in PHP: via single quotes, double quotes, heredoc, and printf. The script constructs a paragraph of text with each method. It performs this construction 10,000 times, then records how long it took. In total, it prints 160,000 times and records 16 timings. Here are the raw results. Outputted straight to browser-- Single quotes: 2,813 ms ...with concatenation: 1,179 ms Double quotes: 5,180 ms ...with concatenation: 3,937 ms heredoc: 7,300 ms ...with concatenation: 6,288 ms printf: 9,527 ms ...with concatenation: 8,564 ms Outputted to the output buffer-- Single quotes: 8 ms ...with concatenation: 38 ms Double quotes: 8 ms ...with concatenation: 47 ms heredoc: 17 ms ...with concatenation: 49 ms printf: 54 ms ...with concatenation: 52 ms A nice graph of the script's output can be found here: http://i3x171um.com/output_benchmarks/ob.gif So what should you choose to print your text? I found several things out writing this. First, it should be noted that the print and echo keywords are interchangeable, performance-wise. The timings show that one is probably an alias for the other. So use whichever you feel most comfortable with. Second, if you've ever wondered which was better, the definitive answer is single quotes. Single quotes are at least four times faster in any situation. Double quotes, while more convenient, do pose a debatably significant performance issue when outputting massive amounts of data. Third, stay away from heredoc, and absolutely stay away from [s]printf. They're slow, and the alternatives are there. The source of my script can be found here: http://i3x171um.com/output_benchmarks/ob.txt DO NOT RUN THE SCRIPT ON THE INTERNET! Run it instead from localhost. The script outputs ~45 megabytes of text in an html comment at the top of the page by default. Expect the benchmark to take ~45 seconds. If this is too long, you can change the amount of iterations to a lower number (the results scale accurately down to about 1,000 iterations). ---- Server IP: 216.194.113.175 Probable Submitter: 68.44.208.212 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.echo.php Edit -- http://master.php.net/note/edit/66391 Del: integrated -- http://master.php.net/note/delete/66391/integrated Del: useless -- http://master.php.net/note/delete/66391/useless Del: bad code -- http://master.php.net/note/delete/66391/bad+code Del: spam -- http://master.php.net/note/delete/66391/spam Del: non-english -- http://master.php.net/note/delete/66391/non-english Del: in docs -- http://master.php.net/note/delete/66391/in+docs Del: other reasons-- http://master.php.net/note/delete/66391 Reject -- http://master.php.net/note/reject/66391 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#111749) next »