Fast Echo

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Behzad
Forum Commoner
Posts: 28
Joined: Mon Jul 09, 2007 3:24 pm
Location: Tehran, Iran
Contact:

Fast Echo

Post by Behzad »

Hi,

We know that using single-quotes is faster than double-quotes,
and we know that concatenation is faster than interpolation (embeding
variables in strings directly).

But take a look at the following code:
Which one is faster?

Code: Select all

<?php

	echo $var1 . $var2;
	
	echo $var1, $var2;
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

I thought "." joined strings together, whereas a comma would not... so comma would probably very very slightly quicker??
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Fast Echo

Post by onion2k »

Behzad wrote:We know that using single-quotes is faster than double-quotes,
and we know that concatenation is faster than interpolation (embeding
variables in strings directly).
The difference is going to be so tiny it's not really worth considering.
Behzad wrote:But take a look at the following code:
Which one is faster?

Code: Select all

<?php

	echo $var1 . $var2;
	
	echo $var1, $var2;
Again, the difference (if there is any) is going to be microseconds at most - it's not worth thinking about. Code readability is much more important than a few thousands of a second of CPU time so use whichever you prefer to read.

If your script is slow and you're trying to optimise it then I'm sure this sort of change isn't going to fix the problem.
Behzad
Forum Commoner
Posts: 28
Joined: Mon Jul 09, 2007 3:24 pm
Location: Tehran, Iran
Contact:

Post by Behzad »

Please take a look at the following URL:
http://us2.php.net/manual/en/function.print.php#66392
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

That illustrates my point perfectly. The user is running a test script that does 10,000 echo statements and it takes 9,527ms (10 seconds) with the slowest one (printf). That's 1ms each. A normal script might output 100 things ... so optimising it to use echo instead of printf is going to save no more than a few milliseconds. If the script is noticeably slow it must be taking a few seconds to run - the print function used is not likely to be the problem.
Behzad
Forum Commoner
Posts: 28
Joined: Mon Jul 09, 2007 3:24 pm
Location: Tehran, Iran
Contact:

Post by Behzad »

Suppose that you have a page which printf-s 100 strings. What will happen if 10,000 people visit your page at the exact same time? I guess that these requests create a loop, and If you sum-up the milliseconds of every printf() for each user, you'll see that the server's CPU becomes more busy than echo(). Perhaps I'm wrong, Please correct me.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There are likely bigger bottlenecks. Focus your attention elsewhere until all others are exhausted.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Behzad wrote:Suppose that you have a page which printf-s 100 strings. What will happen if 10,000 people visit your page at the exact same time? I guess that these requests create a loop, and If you sum-up the milliseconds of every printf() for each user, you'll see that the server's CPU becomes more busy than echo(). Perhaps I'm wrong, Please correct me.
If that sort of thing is happening on your site then concentrating on which function your use to output text will be much less effective at speeding things up than things like buffering output, caching as much as possible, or even baking the content into a static page and serving that instead. You're not wrong that printf takes more CPU than echo, you're just wrong that it's a difference worth bothering about. It's not. Your coding time is better spent looking at other things.

Plus if 10,000 people are hitting your site at the same time, down to the second, you really ought to be looking at things like load balancing.
webaddict
Forum Commoner
Posts: 60
Joined: Wed Mar 14, 2007 6:55 am
Location: The Netherlands

Re: Fast Echo

Post by webaddict »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[quote="Behzad"]
But take a look at the following code:
Which one is faster?
[...]
[/quote]

Hi, I've just done some benchmarking (using PEAR's benchmark timer) and the results, to me, were unexpected. It seems concatenating and echoing to the screen is actually faster then echoing three seperate strings (with the comma):

Code: Select all

<?php
// I've done this code:
for ( $i = 0; $i < 100000; $i ++ )
{
  echo 'Hello world', PHP_EOL;
}

// And this code:
for ( $i = 0; $i < 100000; $i ++ )
{
  echo 'Hello world', PHP_EOL;
}
?>
echo with comma's : 2.2252590656281
echo concatenating: 2.1727521419525


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply