Skip to content

Instantly share code, notes, and snippets.

@igorw
Created October 4, 2012 11:46
Show Gist options
  • Save igorw/3833123 to your computer and use it in GitHub Desktop.
Save igorw/3833123 to your computer and use it in GitHub Desktop.
Benchmark: Pimple vs Symfony/DependencyInjection
{
"require": {
"pimple/pimple": "1.0.*",
"symfony/dependency-injection": "2.1.*"
}
}
$ ./run-tests.sh
Pimple
265K
579K
0.00086s
DependencyInjection
267K
931K
0.006124s
DependencyInjection (dumped)
688K
738K
0.000495s
#!/bin/bash
FLAGS="-d xdebug.max_nesting_level=300"
echo "Pimple"
php $FLAGS test-pimple.php
echo "DependencyInjection"
php $FLAGS test-di.php
echo "DependencyInjection (dumped)"
php $FLAGS test-di-dumped.php
<?php
class Service {
private $service;
private $param;
public function __construct(Service $service, $param)
{
$this->service = $service;
$this->param = $param;
}
}
<?php
require 'vendor/autoload.php';
require 'service.php';
require 'dumped.php';
$start = microtime(true);
echo intval(memory_get_usage() / 1024).'K'."\n";
$container = new ProjectServiceContainer();
echo intval(memory_get_usage() / 1024).'K'."\n";
echo round(microtime(true) - $start, 6).'s'."\n";
<?php
require 'vendor/autoload.php';
require 'service.php';
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
$start = microtime(true);
echo intval(memory_get_usage() / 1024).'K'."\n";
$container = new ContainerBuilder();
$container->setParameter('param', 'foo');
$container
->register('service_1', 'Service')
->addArgument(null)
->addArgument('%param%');
foreach (range(2, 200) as $i) {
$container
->register('service_'.$i, 'Service')
->addArgument(new Reference('service_'.($i - 1)))
->addArgument('%param%');
}
echo intval(memory_get_usage() / 1024).'K'."\n";
echo round(microtime(true) - $start, 6).'s'."\n";
use Symfony\Component\DependencyInjection\Compiler\Compiler;
$compiler = new Compiler();
$compiler->compile($container);
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
$dumper = new PhpDumper($container);
file_put_contents('dumped.php', $dumper->dump());
<?php
require 'vendor/autoload.php';
require 'service.php';
$start = microtime(true);
echo intval(memory_get_usage() / 1024).'K'."\n";
$container = new Pimple();
$container['param'] = 'foo';
$container['service_0'] = null;
foreach (range(1, 200) as $i) {
$container['service_'.$i] = $container->share(function ($container) use ($i) {
return new Service($container['service_'.($i - 1)], $container['param']);
});
}
echo intval(memory_get_usage() / 1024).'K'."\n";
echo round(microtime(true) - $start, 6).'s'."\n";
@stof
Copy link

stof commented Oct 4, 2012

The code for the DI test is wrong. It does not run the compiler passes in the benchmark code, thus taking into account only part of the building

@stof
Copy link

stof commented Oct 4, 2012

and this does not benchmark the use of the container, only its creation

@igorw
Copy link
Author

igorw commented Oct 4, 2012

Compilation is an optional step. I did not want to benchmark it in this case. But fair enough, benchmarking with compilation and actually accessing a service would be interesting things to benchmark as well.

@hellboy81
Copy link

which container should I choose?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment