I am working to optimize my PHP code and found out that you can speed up echoing in these ways - exactly, you can replace echo "The name of the user is $name" . ".";
with:
echo 'The name of the user is '.$name.'.';
echo "The name of the user is", $name, ".";
echo sprintf("The name of the user is %s", $name);
Which one is the fastest? I'd like not only to see benchmarks, but also some technical explaination, if possible.
Firstly, this is micro optimization and you're probably better paying for a faster server and developing more product then spending hours and hours micro optimizing. However according to http://micro-optimization.com/ here are the results:
sprintf() is slower than double quotes by 138.68% (1.4 times slower)
and
sprintf() is slower than single quotes by 163.72% (1.6 times slower)
The above comments are relevant. There are better ways to optimize your code.
That said, the best ways to optimize strings is to pop them into a list and then concatenation the list. Have a look at this post as a good starting point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With