Web Hosting Talk







View Full Version : Good read, improving performance of your PHP code


etogre
04-11-2008, 02:11 PM
If you're anything like me you try hard as possible to improve your script's execution time.

Well, I stumbled across this little gem, which has 63+ ways to increase performance:

http://www.chazzuka.com/blog/?p=163

It has some good ideas, such as

Surrounding your string by ‘ instead of ” will make things interpret a little faster since php looks for variables inside “…” but not inside ‘…’. Of course you can only do this when you don’t need to have variables in the string.


There's a few no-brainers in there (like the above, yet you don't really think about that!), and a few little handy tips and tricks you would have never thought of.

Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.

str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4

Use echo’s multiple parameters instead of string concatenation.


Putting it to use.


$name = "Josh";

echo 'Hello, my name is' . $name . '!';

echo("Hello, my name is ", $name, "!");


The second example there is actually how you would do it in Python, so it makes sense.

Then again, I never echo anything these days, but still.

Perhaps I will run some tests locally on this and post some execution times.

etogre
04-11-2008, 02:59 PM
Well, I ran a quick test.
<?php

$name = "Josh";
echo '<div style="display:none">';
$benchmark1start = microtime(true);
for ($i = 0; $i < 1000; $i++)
{
echo 'Hello, my name is' . $name . '!';
}
$benchmark1end = microtime(true);
//
$benchmark2start = microtime(true);
for ($i = 0; $i < 1000; $i++)
{
echo 'Hello, my name is ', $name, '!';
}
$benchmark2end = microtime(true);
echo "</div>";

$time1 = $benchmark1end - $benchmark1start;
$time2 = $benchmark2end - $benchmark2start;
echo '<br />time1 ' . $time1; // 0.0011308193206787
echo '<br />time2 ' . $time2; // 0.00092887878417969
?>

Well, it is *slightly* faster (considering that is 1000 iterations each). Which I guess if you were trying to tweak every little bit of performance could pay off.

Interestingly, when I switched from single quotes to double quotes, the times shot up as well.

time1 0.001521110534668
time2 0.0012907981872559


Actually running the test a few dozen times, there are sometimes considerable leaps of time.

time1 0.0014848709106445
time2 0.00098586082458496

time1 0.001514196395874
time2 0.00069499015808105


Also sometimes (very rarely) the second one is a little bit higher.

time1 0.0015029907226562
time2 0.0015110969543457

Steve_Arm
04-11-2008, 03:29 PM
Man, this is optimizations for the paranoids.
A bit speed improvement is good but trying to do the ++$i thingy and other is
a waste of time. What is slow these days is downloading the web page to view not parsing the script.

On a side note object initialization is not that slow. If you also pass around
your objects by reference.
I have pages with 7-10 objects doing 2 queries and writing output under 3 ms on a server.

And strings are slow in any compiler.

Also you forgot to write the machine's specs.

etogre
04-11-2008, 03:36 PM
My machines specs are horrible (1.8gHz duo laptop), but it still shows the comparison between the different uses of echo (albeit it is only a tiny difference).

I agree there are a lot of little things on that page, but as they say little things add up.

Steve_Arm
04-11-2008, 03:42 PM
Try this for the first one:


$i = 0;
$benchmark1start = microtime(true);
while ($i < 1000)
{
echo 'Hello, my name is' . $name . '!';
++$i;
}

etogre
04-11-2008, 03:49 PM
Had almost no effect, but is a tad bit slower than using a for loop.

xzbackup
04-11-2008, 05:40 PM
Thanks for posting an interesting link. I think in most situations, these speed gains are negligible, but should you need that much tweaking it's good to have such a resource. Do you know if all of these speed optimizations apply across all versions of PHP? Or have some things perhaps changed say from 4 to 5 that negate some of these? I've only skimmed the list so far.

steven99
04-11-2008, 08:17 PM
When in a shared environment, speed counts. If you can execute the script faster and optimized then when it needs to handle tons of hits, the server wont stress and then the hosting company wont bother you. Put those under digg stresses and see how much better the script is compared to not have the tweaks.

Czaries
04-15-2008, 12:57 AM
These kinds of articles really don't help the PHP community. When programming, your main concern should NOT be performance. Your main concern should be long term maintainability and flexibility. Once you have those two down, you can go back and optimize only where needed. And I guarantee you, the needed optimizations won't be "use ' instead of " because it saves you 0.0021 seconds!". There are only a few things on the entire list that MIGHT someday become a bottleneck.

Do yourself a favor and profile your code using XDebug (http://xdebug.org/). Take those results and only optimize where needed. The rest won't make a difference at all, because they won't be bottlenecks to your application. Don't fall into the trap of premature optimization (http://c2.com/cgi/wiki?PrematureOptimization). The advice in this article is absolutely terrible.

streaky
04-16-2008, 10:52 PM
That first one, has never been an issue ever, it's completely made up.

In the past if did string concatenation like for example:

$string = "something $var something"

It would be slower by quite some margin, but to resolve that you'd do:

$string = "something {$var} something"

Which would speed it up so there would be no issue. Certainly it was an issue with PHP 4.3 and whatnot, but I tested it recently on 5.1.something and they were both as fast as each other, so no real need to do that even anymore.

Personally I like to use the braces still anyways, just for how it looks when you're scanning through code tbh.

I've seen that 'tip' before in a few places, I think somebody was getting a bit confused with it.

With hindsight the reason the two methods could be equal now is because I use an opcode cacher (xcache) on my dev server these days, might be needing to re-test that

arkin
04-17-2008, 09:25 AM
Come off it, these tips are alright, if your application is slow.

I haven't begun implementing performance improving methods yet because I don't need to, my CMS loads in 21ms, faster than you would ever notice.

Security > Performance Speed.

Keep your applications secure and your users won't care about a few milliseconds here and there :)

karlkatzke
04-22-2008, 06:00 PM
A whole ton of those things have been debunked. For versions of PHP from 4.something on, there's no performance hits for the single versus double quotes things.

If your application is slow, why bother going through and doing something pedantic? Implement APC, xCache, Zend_Cache, or something else that'll make a difference across all of your sites at once.

foobic
04-22-2008, 07:08 PM
Yep, or taking a wider view, if your application is slow then look at optimizing the database (structure and indexes) reducing the number of queries and / or caching large chunks of pure HTML.

gplhost
04-30-2008, 05:57 AM
FYI: using caching apps like Zend_Cache, APC, xCache and others have nothing to compare with using something like squid. We had a customer using a wordpress that had his site linked in Slashdot, and a ton of hits came. Using caching apps reduced ONLY by 50% the load, while using Squid in reverse proxy mode made it 10 times faster. Why? Simply because apache is very slow to serve these tiny little pictures you have everywhere on each sites, while Squid is optimized for it.

Of course, you can (and you should, whenever possible) use both a content proxy like Squid AND a script cache at the same time. It works even better when you have a lot of memory available.

Thomas