loading...
Cover image for New php router is 25 times faster then klein router

New php router is 25 times faster then klein router

alexdodonov profile image alexdodonov Updated on ・2 min read

php routes benchmarks (10 Part Series)

1) New php router is 25 times faster then klein router 2) New PHP router wich is 7 to 15 times faster then Symfony router 3 ... 8 3) New PHP router wich is 30 to 50 times faster then Laravel router 4) New PHP router wich is 24 to 42 times fatser then Yii2 router 5) Real life comparison of Symfony router and Mezon router 6) Real life comparison of Symfony router and Mezon router. Part 2 7) Benchmark of PHP Altorouter 8) New benchmark shows that Mezon PHP Router is up to 60 times faster then Slim one 9) Benchmark of Mezon PHP Router and LeagueRouter 10) Mezon PHP Router is up to 82 times fatser then Bramus Router

Hi, all! Recently I have decided to measure speed of my router

And results were quite interesting.

I have measured the productivity in two tests:

  1. Static routes without any regexp parsing or variables in URI

  2. URI with variables

The first test for the Mezon/Router:

// static routes

$routerTest1 = new \Mezon\Router\Router();

$routerTest1->addRoute('/static', function () {
  return 'static';
}, 'GET');

$iterationCount1 = 100000;

$startTime1 = microtime(true);

for ($i = 0; $i < $iterationCount1; $i ++) {
  $routerTest1->callRoute('/static');
}

$endTime1 = microtime(true);

And the second test:

// parametrized routes

$routerTest2 = new \Mezon\Router\Router();

$routerTest2->addRoute('/[i:id]', function () {
  return 'param';
}, 'GET');

$iterationCount2 = 100000;

$startTime2 = microtime(true);

for ($i = 0; $i < $iterationCount2; $i ++) {
  $routerTest2->callRoute('/1');
}

$endTime2 = microtime(true);

For the klein/klein router it is almost the same:

// static routes

$_SERVER['REQUEST_URI'] = '/static';

$routerTest1 = new \Klein\Klein();

$routerTest1->respond('GET', '/static', **function** () {
  return 'static';
});

$iterationCount1 = 10000;

$startTime1 = microtime(true);

for ($i = 0; $i < $iterationCount1; $i ++) {
  $routerTest1->dispatch(null,null,true,\Klein\Klein::DISPATCH_CAPTURE_AND_RETURN);
}

$endTime1 = microtime(true);

And the second one:

// parametrized routes

$_SERVER['REQUEST_URI'] = '/1';

$routerTest2 = new \Klein\Klein();

$routerTest2->respond('GET', '/[i:id]', function () {
  return 'static';
});

$iterationCount2 = 10000;

$startTime2 = microtime(true);

for ($i = 0; $i < $iterationCount2; $i ++) {
  $routerTest2->dispatch(null,null,true,\Klein\Klein::*DISPATCH_CAPTURE_AND_RETURN*);
}

$endTime2 = *microtime(true);

I have got the following results:

table

chart

As you can see - Mezon router is up to 25 times faster than Klein router.

What is mezon/router?

mezon/router now is:

  • framework for routing with 100% code coverage
  • 10.0 points on scrutinizer-ci.com
  • router is a part of the Mezon Project

Repo on github.com: https://github.com/alexdodonov/mezon-router

I'll be very glad if you'll press "STAR" button in Github repo )

php routes benchmarks (10 Part Series)

1) New php router is 25 times faster then klein router 2) New PHP router wich is 7 to 15 times faster then Symfony router 3 ... 8 3) New PHP router wich is 30 to 50 times faster then Laravel router 4) New PHP router wich is 24 to 42 times fatser then Yii2 router 5) Real life comparison of Symfony router and Mezon router 6) Real life comparison of Symfony router and Mezon router. Part 2 7) Benchmark of PHP Altorouter 8) New benchmark shows that Mezon PHP Router is up to 60 times faster then Slim one 9) Benchmark of Mezon PHP Router and LeagueRouter 10) Mezon PHP Router is up to 82 times fatser then Bramus Router

Posted on by:

Discussion

markdown guide
 

But why are there such a big differences in performance?

 

mezon/router provides only basic routing functionality. it made possible to use more simple URL parser and find necessary route handler faster.

 

When you say only basic routing functionality, What are you not providing. I like the idea of automatic url generation with action-suffix. I mainly use a rest call to graphql-rest hybrid endpoint. Which means that uri strings are dynamically generated, e.g books/authors/bio and book/authors/bio/contact. Currently I am using slim framework with its default fastroute. Is your router PSR-7 compliment?

It is not yet PSR-7 compliment. But I hope it will be. Soon )