Nytro Posted December 27, 2010 Report Posted December 27, 2010 PHP Output and Concatenation Speed ComparisonCateva teste simple si utile pentru cei mai "optimizationisti" dintre noi.There are a few commonly asked PHP questions:What is the difference between the print and echo constructs?What is the difference between single and double quotes?These questions are both answered very sufficiently by doing a quick search on Google. However, one thing often overlooked is the speed comparison between print, echo and single and double quotes. So, I thought I would do a bit of a test…Setting up my testAfter reading Adam Wright’s response to this question on Stack Overflow, I thought it was a great starting point and had some great potential to expand. The basics of my test will be based on the following code:<?phpfunction timefunc($function, $runs){ $times = array(); for ($i = 0; $i < $runs; $i++) { ob_start(); $time = microtime(); call_user_func($function); $times[$i] = microtime() - $time; ob_end_clean(); } return array_sum($times) / $runs;}?>It is almost the same as Adam’s, however I have introduced an output buffer so I am able to easily test my output speeds. My output will be an average speed for the function that has been run.To make my test fair, each of my functions to test are going to be based on the following code:function test1(){ $foo = 'some words'; for ($i = 0; $i < 10000; $i++) $t = "these are some words"; //Test in here}For a fair comparison, I am always going to declare the $foo variable even if it is not used in the test, this means the only thing that changes with each test will be my test line.What is quicker – Single or Double quotes?The first test I am going to carry out is the difference between single and double quotes when inserting variables into a string. My variable is to insert will be “some words”, with my starting string being “these are “. I am going to carry out the following tests:<?php/* Simple starting references */$t = "these are some words"; //Double quotes$t = 'these are some words'; //Single quotes/* Tests */$t = "these are $foo"; //Double quotes$t = "these are {$foo}"; //Double quotes with brackets$t = "these are " . $foo; //Double quotes with concatenation$t = 'these are ' . $foo; //Single quotes with concatenation?>As you should know, single quotes doesn’t allow parsing of variables straight into the string, and therefore I have only included it here with concatenation as that is the only way to get the variable into the output.The results are:Double quotes 0.0023348Single quotes 0.0024924Double quotes 0.0039244Double quotes with brackets 0.0040088Double quotes with concatenation 0.0027065Single quotes with concatenation 0.0026829Very, very interesting! From this, you can see that using concatenation with either double or single quotes, the speed decrease is only ~16%. There is also negligible difference between single and double quotes both in the reference test and also in concatenation, I would put the small differences in the test down to noise during my testing.I think the interesting thing found here is that placing the variable directly into double quotes causes a speed decrease of ~68%, again I would put the difference between bracketed and non–bracketed down to noise during my tests as it is a tiny difference.What is quicker – echo or print?Now, what is the quickest way to output information in PHP. It is commonly known that print and echo are used to output data. There are however, two other ways to output data directly to the output data stream, the output and stdout data streams. To output to these, you can use PHP’s normal file handling functions as they deal directly with streams.Firstly, I will run some simple output tests:<?php/* The tests */echo "these are some words"; //Echo with double quotesecho 'these are some words'; //Echo with single quotesprint "these are some words"; //Print with double quotesprint 'these are some words'; //Print with single quotesfile_put_contents("php://output", 'these are some words'); //Output streamfile_put_contents("php://stdout", 'these are some words'); //Stdout stream?>The results:Echo with double quotes 0.0020305Echo with single quotes 0.001976Print with double quotes 0.0021426Print with single quotes 0.0021427Output stream 0.0206471Stdout stream 0.0518192This time, I think the finding are even more interesting. As expected, print is slower than echo by ~6%. Also, writing straight to the output stream causes quite a large decrease in speed. This is probably caused by the fact we are having to use the file_put_contents function to write the output and not a built in constructor. I think very interesting is the fact that writing to stdout is ~150% slower than writing to the output stream.Let’s put them together – output and concatenation comparisonNow we have seen the speed differences between outputs and concatenations, now let’s put them together and see what happens. Here are the tests:<?php/* Tests */echo "these are $foo"; //Echo doubleecho "these are {$foo}"; //Echo double with bracketsecho "these are " . $foo; //Echo double with concatenationecho 'these are ' . $foo; //Echo single with concatenationprint "these are $foo"; //Print doubleprint "these are {$foo}"; //Print double with bracketsprint "these are " . $foo; //Print double with concatenationprint 'these are ' . $foo; //Print single with concatenationprintf('these are %s', $foo); //Printf?>Most of these should make perfect sense, the only addition I have made is the printf function. This allows a string replace functionality that outputs it’s result. Quite simply in the format above, it provides the same functionality as all the other tests. So, here are the results:Output Tests With ConcatenationEcho with double quotes 0.0043563Echo with double quotes (brackets) 0.0043074Echo with double quotes (concatenation) 0.0032709Echo with single quotes (concatenation) 0.0032543Print with double quotes 0.0045534Print with double quotes (brackets) 0.0057546Print with double quotes (concatenation) 0.0034584Print with single quotes (concatenation) 0.0036391Printf 0.0110374Nothing amazing here really, everything has already been explained previously. Probably the only thing to note is that the printf function takes ~92% longer than the next longest output (print with double quotes & brackets).So, I think this proves that the concatenation operator (.) in php provides the quickest way for placing variables into a string. It also, doesn’t seem to make much difference whether you use single or double quotes throughout your scripts. The important thing to note from these test I think, is that the output functions take a significant amount of time longer to run that the output constructs build into PHP.If you can think of any output methods I have forgotten, let me know in the comment. If you have found this post interesting, please read through some of my other posts and subscribe to my RSS feed. Thanks for reading!Sursa si articolul frumos aranjat: PHP Output and Concatenation Speed Comparison – Murray Picton Quote
50cent Posted December 27, 2010 Report Posted December 27, 2010 interesant, e mai ordonat sa afisezi cu echo si apostrof Quote
Rila_xp Posted December 29, 2010 Report Posted December 29, 2010 Foarte interesant!Ce alte metode mai sunt pentru optimizarea codului php?Intreb aici ca sa nu fac alt topic! Quote