tyler-sommer/php-router-benchmark — pull request #4 by adambrett (2014-02-24 19:32:22+00:00)
Title: Benchmark Improvements

Hi,

I originally just wanted to test adding [my little router package](http://github.com/adambrett/php-router) to see how it compared, but when it totally blew all the others away I knew something was wrong because I wrote mine with absolutely no consideration for speed.

A bit on investigation showed that the issue was the way in which things are dispatched, and that my router does the matching when routes are added, and then discards anything else immediately. That lead me to the conclusion that a fairer comparison is the assembly of the router as well as dispatch process, that lead to the following figures:

Test Name | Time | + Interval | Change
--------- | ---- | ---------- | ------
AdamBrett\Router - unknown route (1000 routes) | 0.0313445032 | +0.0000000000 | baseline
AdamBrett\Router - last route (1000 routes) | 0.0320222467 | +0.0006777436 | 2% slower
FastRoute - last route (1000 routes) | 0.1440973312 | +0.1127528280 | 360% slower
FastRoute - unknown route (1000 routes) | 0.1482576191 | +0.1169131160 | 373% slower
Aura v2 - unknown route (1000 routes) | 0.1633502930 | +0.1320057899 | 421% slower
Aura v2 - last route (1000 routes) | 0.1638579160 | +0.1325134128 | 423% slower
Pux PHP - unknown route (1000 routes) | 0.2488152713 | +0.2174707681 | 694% slower
Pux PHP - last route (1000 routes) | 0.2528938979 | +0.2215493947 | 707% slower
Symfony2 - last route (1000 routes) | 0.4613442034 | +0.4299997002 | 1372% slower
Symfony2 - unknown route (1000 routes) | 0.4683836937 | +0.4370391905 | 1394% slower

Clearly this still isn't right, as FastRoute should be far faster than my package, which lead me to separate out the Dumped Symfony2 router, and write some rudimentary caching for the other routes, which lead to the following figures:

Test Name | Time | + Interval | Change
--------- | ---- | ---------- | ------
Symfony2 Dumped - unknown route (1000 routes) | 0.0011271983 | +0.0000000000 | baseline
Symfony2 Dumped - last route (1000 routes) | 0.0011997283 | +0.0000725299 | 6% slower
FastRoute Cached - last route (1000 routes) | 0.0056380630 | +0.0045108646 | 400% slower
FastRoute Cached - unknown route (1000 routes) | 0.0058212489 | +0.0046940505 | 416% slower
AdamBrett\Router - last route (1000 routes) | 0.0299270600 | +0.0287998617 | 2555% slower
AdamBrett\Router - unknown route (1000 routes) | 0.0303771615 | +0.0292499632 | 2595% slower
Pux PHP - last route (1000 routes) | 0.0309612066 | +0.0298340082 | 2647% slower
Pux PHP - unknown route (1000 routes) | 0.0314295948 | +0.0303023964 | 2688% slower
Aura v2 - last route (1000 routes) | 0.0978639692 | +0.0967367709 | 8582% slower
Aura v2 - unknown route (1000 routes) | 0.1023280859 | +0.1012008876 | 8978% slower

As the way my router is designed means it's impossible to cache I have left it uncached.

I guess this shows a few things:

1) The rankings are slightly different from the original benchmark because the way routes are assembled has an impact
2) It's important to cache your router in production