Saturday, February 14, 2009

Upto 80% Speed Increase on Colnect with Symfony

I have managed to cut up to 80% in loading times for Colnect's pages. This is going to be a technical post that would hopefully help others using Symfony framework on their websites. Please mind that for many sites, caching can and should be enabled. On Colnect, however, a lot of pages (especially the heavy ones) cannot be cached since they need be calculated on every request from the same user. If your site is not very dynamic, using APC (if you have a single server) or memcached (when you have many) is the best thing you can do for performance.

Partials are evil


Maybe not that evil but they take their toll on your loading time. The worst is when using them inside a loop as the price increases linearly. Use helpers whenever possible but make sure you load only the necessary helpers on each call and don't try to re-load helpers when inside a loop.

Re-use function results


Symfony encourages you to use methods and functions repeatedly, for example sfContext::getInstance()->getModuleName();. Obviously, the more calculations, the longer things take so whenever you need to re-use results, save the variable content.

A good PHP structure for re-using results is:

function foo_calculate() {
static $result = null;
if (is_null($result)) {
# perform calculations
$result = calculation results...
}
return $result;
}


Escaping PHP and going back to PHP takes its toll


Using
?>xxx
is more costly than
echo 'xxx';
See code on the next paragraph.

Using many echo calls slows things down



It's better to accumulate output in a variable and call a single echo.
The following code performs simple tests so you can get a feeling of the differences in execution times. Run it a few times (when all other applications are closed) since results alter a bit every time.


public function executeCompareOutput(sfWebRequest $request) {
$times = 100000;
echo 'Looping for '.$times.' times - results in msec';
echo '<-div style="display:none">';
$start = microtime(true);
for ($x = 0; $x++ < $times;) {
echo ' '.$x;
}
echo '<-/div><-br/>'.round(1000 * (microtime(true) - $start));

$start = microtime(true);
echo '<-div style="display:none">';
for ($x = 0; $x++ < $times;) {
?> echo $x;
}
echo '<-br/>'.round(1000 * (microtime(true) - $start));

$start = microtime(true);
echo '<-div style="display:none">';
$sBuf = '';
for ($x = 0; $x++ < $times;) {
$sBuf .= ' '.$x;
}
echo $sBuf;
echo '<-br/>'.round(1000 * (microtime(true) - $start));

$start = microtime(true);
echo '<-div style="display:none">';
$sBuf = '';
for ($x = 0; $x++ < $times;) {
$sBuf .= ' ';
$sBuf .= $x;
}
echo $sBuf;
echo '<-br/>'.round(1000 * (microtime(true) - $start));

$start = microtime(true);
echo '<-div style="display:none">';
$GLOBALS['bufbuf'] = '';
for ($x = 0; $x++ < $times;) {
$GLOBALS['bufbuf'] .= ' '.$x;
}
echo $sBuf;
echo '<-br/>'.round(1000 * (microtime(true) - $start));

die('<-br/>bye');
}


Here is a sample output:

Looping for 10000 times - results in msec
3044
5503
10
15
16
bye


On this run using multiple echo calls + PHP escaping took 55 TIMES MORE than buffering the output in a variable. This clearly proves that the style suggested by Symfony templates, using many PHP echo blocks, is HIGHLY inefficient. If you have a few dozens of it in your templates and your content is cached, this is negligible. If your content is very dynamic, as is the case with Colnect, we're talking about something very worth noting.

Wednesday, February 11, 2009

Colnect's Alexa Ranking Keeps Going Up

Colnect's ranking on Alexa has risen again, now standing at 184,627 whereas ~3 months ago it was at ~360,000 and ~5 months ago ~500,000. I don't even know if it's linear or logarithmic although I guess the latter.

To those who may not know, Alexa ranks many (most?) websites out there of the big WWW. It does that by analyzing the traffic of (mostly unsuspecting?) users who install the Alexa toolbar on their browsers. This nice toolbar delivers the information back to their big servers, they crunch it whichever way they like and then rank the sites according to this information.

Why would anyone care about Alexa's ranking? Well, it has come to be relatively important in the WWW world. It allows you to have some (allegedly non-biased) objective information about how your website fairs on the Internet.

Are the results reliable? The short answer IMHO: no. The longer answer: perhaps, to some length. Since some website owners/operators/SEO personnel think of it as important, they are making an effort to get their ranking up. This isn't that hard, considering the fact the grand majority of users don't have the Alexa toolbar. There are many methods but they're basically about making people who use your site have the Alexa toolbar thus your site will have an improved ranking.

Another important disadvantage of Alexa is that ranking is on a per-domain basis. As such, this blog (which a few read) ranks the same as Colnect (which many frequently use). Personal websites on some free hosting all rank the same so you cannot tell them apart and so on.

Does Colnect try to improve its ranking? No. I did think about it for some time, since after all it may look better to some people, but have decided not to. The main two reasons would be not to spend my time on metrics that don't really matter and the second that since I wouldn't install it on my machine, I wouldn't ask others to do so.

Okay, let's hope this post won't cause Colnect's Alexa ranking to crash :)

Sunday, February 8, 2009

Numbers, Numbers, Numbers...

Many collectors (and non-collectors) really enjoy statistics and so finally the long awaited counters have now been added to Colnect. So when a collectors sees all coins in Colnect by country, there's now a small number indicating how many coins of that country are available on the database. The same goes for stamps and phonecards.

The big benefit becomes clearer when looking at a collector's collection, swap list or wish list. It's then very easy to know how many items the collector has of each country, company or even series.

Here's for example the information for an expert collector on Colnect, Czech RepublicDravec

Phonecards

Collection: 7,546 Phonecards
Swap list: 1,290 Phonecards (Match with my wish list)
Wish list: 6,199 Phonecards (Match with my swap list)

Stamps

Collection: 1,137 Stamps
Swap list: 3 Stamps (Match with my wish list)
Wish list: 5,678 Stamps (Match with my swap list)

Coins

Collection: 2,155 Coins
Swap list: 301 Coins (Match with my wish list)
Wish list: 10,938 Coins (Match with my swap list)



On the back end side of Colnect, the system is very flexible in supplying the given information so the real challenge was to try and create an as-intuitive-as-possible user interface. I've recently made a post about usability and the addition of counters and simplification of the user interface that followed is a big step forward.

Link and Search

Did you like reading it? Stay in the loop via RSS. Thanks :)