PHP Dependency Injection Container Performance Benchmarks

Most frameworks and larger PHP applications utilize a Dependency Injection Container with the goal of a more maintainable codebase. However, this can have an impact on performance. As loading times matter, keeping sites fast is important as ever. Today I’m going to benchmark several PHP Dependency Injection containers to see what their relative performance is like.

For those unfamiliar with the concept, a Dependency Injection Container is a piece of software which automatically builds an object tree. For example, consider a User object which requires a Database instance.

    $user = new User(new Database());

A Dependency Injection Container can be used to automatically construct the object tree without needing to provide the parameters manually:

    $user = $container->get('User');

Each time this is called, a user object will be created with the database object “injected”.

There are several well known (and not so well known) containers available for PHP:

  • PHP-DI, a popular DI Container
  • Symfony\DependencyInjection, the the Dependency Injection Container provided by the Symfony framework
  • Zend\Di the Dependency Injection Container provided by Zend Framework
  • Orno\Di, a lesser known container with limited features but developed with performance in mind
  • Dice, another lesser known container with a focus on being lightweight. Full disclosure, I’m the author of this container, but I’ll be nothing short of entirely objective in this analysis.
  • Aura.Di, a fairly popular container with minimal features

A word on Pimple: Although Pimple is advertised as a Dependency Injection Container, retrieving an object from the container always returns the same instance, which makes Pimple a Service Locator rather than a Dependency Injection Container and as such, cannot be tested.

Although all the containers support different features, this benchmark will cover the basic functionality required by a Dependency Injection Container. That is, creating objects and injecting dependencies where they’re needed.

Which aspects of dependency injection will be measured?

  1. Execution time
  2. Memory Usage
  3. Number of files included. Although this has very little impact on performance it’s a good indicator of how lightweight and portable a library is. If you have to ship hundreds files with your project because of your DI choice, it can heavily impact the overall weight of your own application.

Testing environment

All tests were run on the same machine running Arch Linux (3.15 Kernel), PHP 5.5.13 and the latest versions of each container as of 03/07/2014.

All execution time numbers presented are an average of 10 runs after discarding any that are over 20% slower than the fastest.

Test 1 – Create an instance of an object

This test uses each container to create a simple object 10,000 times

Without a Dependency Injection Container, this would be written as:

    for ($i = 0; $i < 10000; $i++) {
      $a = new A;
    }

Test code (on github): Aura, Dice, Orno\Di, PHP-DI, Symfony\DependencyInjection, Zend\Di

Test 1 - Execution Time

As you can see, there’s two clear camps here. Aura, Dice and Orno being roughly ten times faster than PHP-DI, Symfony and Zend\DI.

Test 1 - Memory Usage

Similar to Execution Time, there are two distinct groups with Symfony sitting somewhere in the middle ground.

Test 1 - Number of Files

This is very telling of how lightweight each container is and goes some way towards explaining the memory usage differences. It should be noted that a lot of the files used by Zend\Di are common framework files so if you’re using Zend Framework, then using Zend\Di will not incur the same memory overhead as files will likely be reused elsewhere in your application.

Similarly, PHP-DI heavily relies on Doctrine libraries. If you’re using Doctrine in your project, then the memory overhead of PHP-DI is reduced.

However, It’s nice to see that Symfony\DependencyInjection, despite being part of the framework stack is entirely standalone and works without any dependencies from other Symfony projects.

Aura, Dice and Orno do not have any external dependencies and this helps keep their file counts down.

Test 2 – Ignoring autoloading

As loading files can impact performance and both Zend and PHP-DI loaded a significant number of files, the same test was conducted ignoring the autoloader time by first creating a single instance of the class, ensuring any required classes were autoloaded before measuring the time.

This may also have triggered any internal caching done by the container but the same treatment was applied to each container to keep it fair

Equivalent PHP code:

    for ($i = 0; $i < 10000; $i++) {
          $a = new A;
    }

Test code (on github): Aura, Dice, Orno\Di, PHP-DI, Symfony\DependencyInjection, Zend\Di

Test 2 - Execution Time

Test 2 - Execution Time

Test 2 - Execution Time

As expected, memory usage is unchanged and performance is slightly better as the autoloader time isn’t being measured. However, this shows that PHP-DI, even loading 42 files has a negligible impact on the total execution time and the relative performance remains the same, loading dozens of files is not the cause of PHP-DI and Zend\DI having relatively slow performance.

Even after ignoring the overhead of loading files, there are still two distinct ballparks here. Aura, Dice and Orno are very similar in performance and memory usage while PHP-DI, Zend and Symfony are only in competition with each other.

All the tests going forward will ignore the autoloading time to ensure it’s truly the container’s performance that is being measured.

Test 3 – Deep object graph

This test is done by having the containers construct this set of objects 10,000 times:

    for ($i = 0; $i < 10000; $i++) {
        $j = new J(new I(new H(new G(new F(new E(new D(new C(new B(new A())))))))));
    }

Test code (on github): Aura, Dice, Orno\Di, PHP-DI, Symfony\DependencyInjection, Zend\Di

Note: As you can see by looking at the test code, Symfony, PHP-DI and Aura require considerably more configuration code than the other containers to perform this test. The configuration time was not included in the test.

Test 3 - Execution Time

Again, there’s very little difference between the top 3, with Dice 20% faster than Aura and 70% faster than Orno. All three are considerably faster than Zend, PHP-DI and Symfony. The difference between the three top containers is so slight in real terms that you would never notice the speed difference outside an artificial benchmark like this.

Zend, PHP-DI and to a lesser extent Symfony are slow here. Zend takes 37 seconds to perform a task Dice manages in under 1 second; certainly not a trivial difference. Yet again, Symfony takes the lead among the big name containers.

Test 3 - Memory Usage

Test 3 - Memory Usage

Memory and file counts are consistent with what we’ve seen in other tests.

Test 4 – Fetching a Service from the container

DI Containers also have to store and retrieve services which will be reused throughout the application. This test fetches a single instance from the container repeatedly.

Pure PHP Equivalent:

    $a = new A;
    for ($i = 0; $i < 10000; $i++) {
      $a1 = $a;
    }

Test code (on github): Aura, Dice, Orno\Di, PHP-DI, Symfony\DependencyInjection, Zend\Di

Test 4 - Execution Time

This is unexpected based on previous results. All the containers except Zend and Symfony are roughly equal with just 0.01s separating the top 4 results. Symfony is not far behind, but Zend is well over ten times slower than the others.

Test 4 - Memory Usage

Test 4 - Number of Files

Memory usage and number of files results are becoming predictable with the same division between the containers that we’ve seen in execution time throughout.

Test 5 – Inject a service

The final test is to see how quickly an object can be constructed and have a service injected. This takes the format:

    $a = new A;
    for ($i = 0; $i < 10000; $i++) {
        $b = new B($a);
    }

Test code (on github): Aura, Dice, Orno\Di, PHP-DI, Symfony\DependencyInjection, Zend\Di

Test 5 - Execution Time

Interestingly, Aura has taken a slight lead in this test. However, it’s not quite a like-for-like test as Symfony and Aura require several lines of explicit configuration while the other containers automatically resolve the dependency. The time taken to configure the container was not part of the benchmark.

Surprisingly, PHP-DI is the slowest at this task, with Zend taking its position ahead of PHP-DI and Symfony for the first time.

Test 5 - Memory Usage

Test 5 - Number of Files

Conclusion

On performance alone, Dice, Aura and Orno are all strong competitors, Dice is fastest overall and Aura fastest in the final test. The difference between the two distinct groups is apparent but its interesting to compare features of each container. Number of features and performance do not quite correlate as you’d expect. Both PHP-DI and Dice contain unique features but PHP-DI takes a heavy performance hit for doing so. Aura, although fast, requires a lot of manual configuration and does, as you’d expect, have very minimal features whereas Dice and Orno have very similar performance but require a lot less code to configure.

Symfony is very much in the middle ground in all tests, although configuring it, as with Aura, is a much more difficult task as neither support type hinted parameters. If you’re looking for a container from a well known project, then Symfony has to be the container of choice if performance is important.

That said, if pure performance is what you’re after then Dice and Aura are the clear winners with Orno very close behind. However, it’s worth taking a look at configuration syntax and features of each to see which you would prefer working with as the performance difference between Dice, Aura and Orno is negligible for any real application.

All the code for the tests is available on github. Please note: The github repository contains copies of the libraries tested rather than using composer to include them in the project, this is to ensure that you can run the code with the exact versions I tested and get the same results.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Maxime Fabre

    No love for Laravel’s ?

    • codeatbusiness

      +1 Laravel Dependency Injection management is great.

    • http://r.je Tom Butler

      Feel free to correct this if I’m wrong but unfortunately Laravel does not provide an accessible standalone instance of its DI container, it looks like it’s heavily coupled to the framework stack as it uses static methods in the App class, which itself is somewhat problematic as it implies the use of global state

  • Stephan Hochdörfer

    Pimple can be configured to return new instances for every call. You simply need to wrap your anonymous function with the factory() method. See the documentation on “Defining Factory Services” how that is done.

    • http://ocramius.github.io/ Marco Pivetta

      That still requires pimple to be backed by a proper injector

      • Dionis Spinei

        What do you mean a proper injector and why would Pimple need that? By default Symfony DI container also returns the same instance.

  • http://harikt.com/ Hari K T

    Good to see aura doing well :) .

  • http://mwop.net/ Matthew Weier O’Phinney

    Not all of the containers tested are pure DI; I would argue this is actually a benchmark of IoC containers. As such, I’d love it if you would extend the benchmarks to add ZendServiceManager to this, as a) it is an IoC container, and b) far more widely used than ZendDi (we actually discourage use of Di due to its known performance issues, as well as the difficulty in consumption). This would give a better comparison for those using these benchmarks to compare frameworks.

  • http://r.je Tom Butler

    Of course it can be taken out and used but there is no official documentation that I can see for using the container outside the framework stack. The framework documentation requires use of the framework to run the examples in the docs. For someone looking for a Dependency Injection Container to use in one of their projects it’s not going to matter how fast it is if there’s no examples of how to use it or even a list of features. ZendDi relies on several other framework libraries, but the example code can be run without the use of running the entire framework, which is a big difference.

    • Erik Wiesenthal

      Actually Laravel Container is as easy to use as Pimple. Both of them can be used to return new instances. I would like to see them added to the benchmarks.

      [quoted from pimple]
      By default, each time you get a service, Pimple returns the same instance of it. If you want a different instance to be returned for all calls, wrap your anonymous function with the factory() method

  • .Jan Rybařík

    And what about Nette framework. Do you know that? :-)

  • papayasoft

    Re: composer: The repository for the tests does not do need to contain all the libraries under test. You can point `composer.json` to the specific versions – whether by tag or even by specific SHA1 revision – of the dependencies to get the precise versions you want. This would maintain the same determinism in the test suite, while increasing flexibility to repeat the tests on different versions.

  • http://loige.com/ Luciano Mammino

    Such a great article, well done. I’ll wait for the update version that will include pimple, nette and laravel!

    I also discovered you are the author of Dice, so well done again!

  • happyaccidents

    I’d be interested to know what was considered limited about OrnoDi compared to the other containers?

    • http://r.je Tom Butler

      I’m planning to do another article that contains a feature comparison but PHP-DI, Dice and Zend all have features that OrnoDi does not, such as a lot more control over automatic injection.

      • http://filip-prochazka.com/ Filip Procházka

        If you need any info about Nette for the next article, just ping me, I can provide any info you may need :)

      • happyaccidents

        I may be a little biased but I wouldn’t say OrnoDi is limited in functionality compared to the other containers at all, your use case may not be documented, but I’d be happy for you to ping me an email when you’re comparing features so I can explain how to implement anything you need to do.

        • http://r.je Tom Butler

          To anyone looking for a Dependency Injection Container to use in their project, an undocumented feature is identical to a non-existent feature.

          For me doing a feature comparison with a goal of helping people make a choice on which container to use, I can only go by documented features.

          • happyaccidents

            Why do all comments from you (past and present) always seem so defensive? I’m simply asking for an example of when you found it “limited” so if it makes sense, then it can be implemented, and if the functionality is there I can point that out to you.

            “an undocumented feature is identical to a non-existent feature” this is simply untrue, there are intended use cases and unintended use cases, this doesn’t mean the feature isn’t there. For example, I would guess that none of these containers document that they can be used as service locators, does that mean that they cannot be used in that way?

          • http://r.je Tom Butler

            You chop off the important part of that quote then purposefully take it out of context, then ask me why I’m defensive? Of course the part you quoted is untrue but you missed out the preface: “to anyone looking for”. It’s disingenuous to imply new users who are choosing between two or more libraries that perform the same task would look through the code line by line to see what it may or may not be capable of.

          • happyaccidents

            Well your sentence is still untrue regardless of the preface existing or not.

            Control over something automatic would suggest that it is no longer automatic, therefore that is not really an example, I am asking for a specific example so I can either document it’s use, implement it if it makes sense or point out why it’s not implemented, but a vague comment like that gives me nothing to go on at all.

  • WooDzu

    Great work! Personally I’d like to see how PhalconDi stands among others.

  • http://www.adrianmiu.ro/ Adrian Miu

    I’ve looked at what the compiler does and it seems to convert every definition into a factory method. If that’s correct then the SymfonyDi would have to be compared only with factory based definitions on the other DIs.

  • liquorvicar

    Probably worth point out at the top of the article that you are the maintainer of Dice, is it not? Not suggesting you have anything but good intentions but for the sake of complete transparency…

  • http://www.nikolaposa.in.rs Nikola Poša

    http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.intro.html

    I haven’t examined your tests on github in details, but on first glance, I only see cases for testing behavior of service locator implementations. So I agree with @weierophinney:disqus’s observation.

  • Alex Stansfield

    Regarding pimple. Version 1.x returns new instances of services by default.

    Whilst 1.x is not the latest version it is the version that the stable versions of the micro frameworks silex and cilex are built on and might make it worth including.

  • http://paul-m-jones.com/ Paul M. Jones

    Iqbal: that’s a common misconception among Laravel folk. The term “facade” is used improperly there; the pattern on display is more like a static proxy to a service locator instance. The fact that you (and others!) continue to be led astray by the poor choice of name in Laravel (i.e., calling something a “facade” that is not in fact a facade) underlines the points I have been making about how important it is to use the correct names for things.

    • AmyStephen

      It’s also a matter of opinion, Paul, since it is, indeed, a Facade. In the big picture of things it seems mostly to matter to providers of frameworks other than Laravel, not the devs who use Laravel.

  • Larry Garfield

    “A word on Pimple: Although Pimple is advertised as a Dependency Injection Container, retrieving an object from the container always returns the same instance, which makes Pimple a Service Locator rather than a Dependency Injection Container and as such, cannot be tested.”

    I’m sorry, what?

    No, actually, I’m not sorry. That is a horribly misleading and incorrect statement. The difference between a DIC and an SL is the usage pattern. Whether it returns the same instance or reuses it on subsequent requests is completely irrelevant.

    Well, no, that’s not true either. Why would you want a new instance of the object on every request? Really, when? If I’m passing a DB connection or Redis connection object or anything containing a resource reference via DI, you better believe I want the object reused. I don’t even know what the use case is to not do so, frankly.

    Also, as others have noted Piimple is completely capable of making a new object on each request or reusing it on each request. The default switched in Pimple 2 but both are still possible. Both are also completely possible in Symfony’s DI component, too. Yet you said one is a DIC and the other isn’t?

    Also, as others have noted you forgot to use compilation on several of the containers, which automatically puts them at a disadvantage. That’s equivalent to comparing two frameworks but running one on PHP 5.2 with no opcode cache and the other on PHP 5.5 (with built-in opcode cache, plus tons of engine improvements). The resulting comparison is completely meaningless.

    All in all this is a horribly misleading article with a faulty premise and faulty testing methods. I expect better from SitePoint.

    • http://r.je Tom Butler

      “Well, no, that’s not true either. Why would you want a new instance of the object on every request? Really, when? If I’m passing a DB connection or Redis connection object or anything containing a resource reference via DI, you better believe I want the object reused. I don’t even know what the use case is to not do so, frankly.”

      Any object which isn’t a service needs to be a new instance on each request to the container, it makes no sense otherwise. Consider an MVC model/controller/view you may want two instances of these on a single page displaying different information, it breaks desired behaviour if the container returns the same instance on every request. In a real world application, the container will only return the same instance for objects which, as you say, are resources or shared services. This tends to be a small % of the total objects that exist within the application. If you are using the DIC as a Service Locator and manually fetching the services you want then it makes sense that you would only ever use the container to store reusable services, but that’s not what I’m testing here.

      I am doing a second set of benchmarks which address the issues you pointed out, and including Pimple and some of the other containers people suggested.

      • Larry Garfield

        We must be working on extremely different types of systems then. I’m one of the lead developers on Drupal 8. We have a huge number of objects registered in the Symfony Container we’re using. I don’t think more than one or two are “prototype” services (ie, re-created on each request rather than reused). The overwhelming majority of them are not taking the container as a parameter; they’re proper Dependency Injected services. A few are, for various reasons (some good, some bad). In that case we’re using the DIC as a service locator.

        You’re conflating “passing the container into an object so that it can pull in services” with “services are reused rather than repeatedly recreated”. Those are two entirely different attributes of a problem space/solution. There’s situations in which all combinations of those may be appropriate to do (and any good DIC can support all such combinations), but don’t conflate the two.

        • http://r.je Tom Butler

          I never suggested passing the container into anything. The reason a lot of web frameworks get away with treating a high proportion of objects as shared services is simply that the container (and the objects its managing) has to be recreated on every request. In any other programming language you’d realise that essentially creates global state because anything using the container gets a shared instance, the fact that a lot of objects are only used once per request lets you get away with treating them as shared services when they’re not, any object you’re only using once per request (Which is all the framework entry point stuff, all of the application’s controllers and views which you’re using once per page) should only be created as single instances. On the web you get away with treating them as shared objects but it’s a cludge.

          • http://paul-m-jones.com/ Paul M. Jones

            > object you’re only using once per request (Which is all the framework entry point stuff, all of the application’s controllers and views which you’re using once per page) should only be created as single instances.

            Preach the Word! The whole “controllers as services” thing never sat well with me and that’s a good description of why.

  • http://r.je Tom Butler

    Ray.Di looks interesting but it’s annotation based which is a DI antipattern, and secondly, the version on github doesn’t work, it fails with Fatal error: Call to undefined method RayAopMatcher::setAnnotationReader() when trying to run the example code.

    • http://harikt.com/ Hari K T

      I have asked the creator to ping here. He will have a look into the issue and address it .

      Thank you.

    • Richard McIntyre

      I see from your blog that you have a small amount of a dislike for annotations. They are a mature part of many languages now. I don’t see why they should be considered specifically a ‘DI anti-pattern’. 2 of the most famous DI frameworks (Spring and Guice) indeed make use of them.

      Fair point about the sample code not working though…

      • http://r.je Tom Butler

        The fact that other frameworks use annotations is no advocacy of their efficacy (argumentum ad populum), the use singletons and global state is also very common.

        With annotations, the developer of a given class is aware of the container and has to think about it when writing the class, this breaks encapsulation. More importantly, the annotation becomes part of the class API. Using annotations is no different than saying “To use this di framework, you must write your class with the method public function setDependencies(array $dependencies);”.

        • Richard McIntyre

          I will definitely chew over your points – you are thinking about things which is good. I like this over the `Annotations on services that configure DIC in java frameworks simply violate IoC.` comments which just sound like blanket statements. As for the annotations in Ray.DI you are not saying WHAT you are injecting – just marking it as an injection point. There is no `container`. You are right in your opinion of this where Spring is concerned but Guice/Ray.Di is different in this regard.

      • http://filip-prochazka.com/ Filip Procházka

        Annotations on services that configure DIC in java frameworks simply violate IoC.

  • Larry Garfield

    I never said there was perfect code. There’s always exceptions to best practices that have to be made. But you should at least call them what they are, honestly. Passing a whole container into an object and calling it a “Factory” is, er, wrong. It may be an appropriate thing to do or not, depending on the circumstances, but calling it a factory is still wrong.

    • AmyStephen

      I think you mean calling it a “Facade?” See how easy it is to use the wrong term?

      You are making my point. When leading PHP devs make such strong stands on nomenclature, it has a negative impact on acceptance but it does nothing to help developers use better approaches.

      What we should do is share the link to Taylor’s post which shows how to avoid the practice entirely. That actually helps people use Laraval in a better way and avoids the whole Facade -vs- Proxy -vs- Factory silliness. http://taylorotwell.com/response-dont-use-facades/

      • Larry Garfield

        No, I used the term Factory very specifically and deliberately to be an example different from the one in Laravel. There was no mistake.

        • AmyStephen

          OK, but I fear Larry we’ve kind of wandered off topic, then.

          Laravel devs read Taylor’s post
          http://taylorotwell.com/response-dont-use-facades/ – no statics, no
          injected factory, no injected container — it’s DI via constructor at
          it’s finest. =)

          • http://paul-m-jones.com/ Paul M. Jones

            Amy, I think it is you who has wandered. That link does not address the improper use of the word Facade.

  • http://paul-m-jones.com/ Paul M. Jones

    Note the beginning of this particular thread originates in my accurate observation that Laravel uses the term Facade improperly.

    http://www.sitepoint.com/php-dependency-injection-container-performance-benchmarks/#comment-1540768259

    The “little labeling argument” is the subject here, not statics or DI. So, to reiterate, it is *you* who has drifted off to other topics.

  • http://r.je Tom Butler

    ‘”The whole world is crazy; I’m the only sane one” kind of situations.’ That’s a little disingenuous. I have had multiple arguments with PHP Developers, but my own thoughts are in line with well known programmers and academics outside the PHP Community, it’s generally that PHP Developers do things differently (And yes, often wrongly) that misses the point of how/why something is done. Part of this, like Larry’s example is because PHP lets you get away with doing things you couldn’t in other languages without severe consequences, it doesn’t make it right/wrong but I tend to stand on the side of general programmers, rather than loooking through the very narrow PHP perspective.

    As for pimple it was pointed out that you can use it to construct new objects and I will be using it in the next set of benchmarks. However, something that can only ever one instance of a class cannot be considered a DIC because it can’t be used as such in a real world application