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!
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?
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?
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.
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.
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.
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.
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):
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]