Jump to content
Nytro

PHP Output and Concatenation Speed Comparison

Recommended Posts

PHP Output and Concatenation Speed Comparison

Cateva 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 test

After 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:

<?php
function 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.0023348
Single quotes 0.0024924
Double quotes 0.0039244
Double quotes with brackets 0.0040088
Double quotes with concatenation 0.0027065
Single quotes with concatenation 0.0026829

Very, 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 quotes
echo 'these are some words'; //Echo with single quotes
print "these are some words"; //Print with double quotes
print 'these are some words'; //Print with single quotes
file_put_contents("php://output", 'these are some words'); //Output stream
file_put_contents("php://stdout", 'these are some words'); //Stdout stream
?>

The results:

Echo with double quotes       0.0020305
Echo with single quotes 0.001976
Print with double quotes 0.0021426
Print with single quotes 0.0021427
Output stream 0.0206471
Stdout stream 0.0518192

This 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 comparison

Now 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 double
echo "these are {$foo}"; //Echo double with brackets
echo "these are " . $foo; //Echo double with concatenation
echo 'these are ' . $foo; //Echo single with concatenation
print "these are $foo"; //Print double
print "these are {$foo}"; //Print double with brackets
print "these are " . $foo; //Print double with concatenation
print 'these are ' . $foo; //Print single with concatenation
printf('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 Concatenation

Echo with double quotes 	0.0043563
Echo with double quotes (brackets) 0.0043074
Echo with double quotes (concatenation) 0.0032709
Echo with single quotes (concatenation) 0.0032543
Print with double quotes 0.0045534
Print with double quotes (brackets) 0.0057546
Print with double quotes (concatenation) 0.0034584
Print with single quotes (concatenation) 0.0036391
Printf 0.0110374

Nothing 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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...