The title of this article could have been « The definitive guide to (not) responding to speed benchmarks and framework comparisons ». We regularly see framework benchmarks pop up somewhere on the net, comparing symfony to other frameworks, either in PHP or in other languages. The conclusion of some of them is « symfony takes more time than other frameworks to display a "hello, world", which makes it unsuitable for real-world web applications ». Comments on these articles are mixed, from the complete troll to the constructive discussion. But whatever is said on the subject, the question always reappears, and it seems that people are reinventing the wheel each time they see such conclusions. Contradicting this kind of statement requires some thinking, so get prepared for some reading.
Symfony is not slow in the meaning of "not optimized"
If some benchmarks show that symfony is slower, jumping to the conclusion that symfony is not optimized is a big mistake. I'd say that people who take this shortcut are either way smarter than us, or they don't know what profiling is, they didn't look at the symfony code, and they can't make the difference between efficient code and a bottle of beer.
The coding practices of symfony developers come from the experience of numerous web applications developed over almost 10 years. We have a quite accurate idea of which code is slow and which code is fast, because performance has always be one of our clients' main requirements. We tested many implementations, we read a lot about the current best practices, and we tried to put the best of what we've learned so far into the symfony code. So for instance, you will not find this code in symfony:
for ($i = 0; $i<count($my_array); $i++)
instead, we try to always do:
for ($i = 0, $count = count($my_array); $i<$count; $i++)
This is because we know it makes a difference.
How do we know it? Because we measured. We do use profiling tools a lot, on our own applications as well as on symfony itself. In fact, if you look at the symfony code, you can see that there are numerous optimizations already in place. And if you use a profiling tool yourself on a symfony application, you will probably see that there is no way to significantly optimize symfony without cutting through its features.
Of course, there might still be a lot of small optimizations possible here and there. If you find some, please open a ticket in the symfony trac system, and we'll add it to the trunk as long as it doesn't break BC or require a third-party component.
Symfony's speed is a choice
If symfony is well implemented (or so we think), then its speed is just the sum of the individual speeds of all the features it includes. Or, to put it differently, Symfony's speed is the consequence of a series of choices that we made to decide whether a feature should or should not make it to the core.
These choices were not easy to make, and you could easily discuss them. Symfony results from a vision of what would the perfect tool for developers, based on our experience. For instance, we decided that output escaping should be included by default, and that configuration should be written preferably in YAML. This is because output escaping protects applications from cross-site scripting (XSS) attacks, and because YAML files are much easier to read and write than XML. I could name similar arguments about security, validation, multiple environments, and all the other features of symfony. We didn't add them to symfony because it was fun. We added them because you need them in almost every web application.
Note that even if these features exist in symfony, you can always deactivate them, because of the great configurability of the framework and the ability to override its classes.
For each choice, we carefully weighed the benefits in functionality and ease of development, and the penalty in performance. Symfony embarks a lot of features activated by default (when you compare it to other frameworks) because we think that the cost of development is more important than the cost of hosting. It is very easy to add a new server to boost the performance of a website, it is very hard to add a new developer to an existing project to make it complete faster.
Benchmarking the speed of a "Hello, world" script makes little sense
So a framework has a certain speed because it has a certain set of features packaged by default. If you take this idea the other way around, you could conclude that the fastest framework on earth is no framework at all. Some very well-known developers actually defend this point of view: Using plain PHP will make your application way faster than using a framework. Nevertheless, none of the framework benchmarks actually compare frameworks to a naked language. This is because it doesn't make sense. If frameworks exist, it is not for the purpose of speed, it is for ease of programming, and to decrease the cost of a line of code. This cost not only consists of the time to write it, but also the time to test it, to refactor it, to deploy it, to host it, and to maintain it over several years.
It doesn't make much more sense to compare frameworks that don't propose the same number of features. The next time you see symfony compared with another framework on a "hello, world", try to check if the other framework has i18n, output escaping, Ajax helpers, validation, and ORM turned on. It will probably not be the case, so it's like comparing pears and apples.
Also, how often do you see pages displaying "hello, world" in real life web applications? I never saw one. Web applications nowadays rely on very dynamic pages, with a large amount of code dedicated to the hip features dealing with communities, mashups, Ajax and rich UI. Surprisingly, such pages are never tested in framework benchmarks. And besides, even if you had a rough idea of the difference in performance between two frameworks on a complex page, you would have to balance this result with the time necessary to develop this very page with each framework.
So the best way to display a "hello, world" in terms of performance is probably:
<?php echo "hello, world" ?>
If your application reduces to that, obviously, you don't need a framework. Otherwise, you need to base your decision on another type of benchmark.
The speed of a framework is not the most important argument
Said in a provocative way, the conclusion of the previous section could be: "symfony is slow on purpose". We could even defend this, because the raw speed of a framework only counts for a tiny share of the performance of an application as perceived by the end user, and as one argument between a lot of others in the choice of a development tool.
The performance perceived by the user depends on the raw speed of the framework user in the backend, but also on many other factors. The number of assets (images, CSS, scripts, etc.) to download for a given page can affect the download time dramatically. The user's bandwidth and number of possible concurrent requests are often the most important bottlenecks in Internet communications. The number of database queries and the quality of the indexation of the database involved can also impact performance a lot. The speed of the components used by the host (database vendor, op-code cache, disks, network interfaces, etc.) are also an important factor. These are just examples, but the point is that a properly configured platform, using a CDN, high performance components, carefully tweaked and well monitored, is much more important for the user's feeling of speed than the raw speed of the server-side scripts.
And between two framework alternatives with comparable speed, a company will look at other factors to make a good decision. The quality of the documentation, which is directly proportional to the number of good developers available on a framework, is decisive. The cost and availability of developers for a given language, for instance, push many deciders to give up Ruby on Rails or Django for PHP frameworks. The difficulty to host and monitor these solutions is also a key factor in the balance, since fast cgi is not as stable as mod_php. In addition, clients will have to maintain the applications you develop for them, and they will need developers for that. If you elect a not widespread solution, you may loose some of your clients.
Let's finish by saying that in most cases, symfony does not compete with RoR or Django. The choice is often between symfony, .Net and Java Struts, because these are the real alternatives for professional developments.
Conclusion
Dismissing symfony because the speed of a "hello, world" test is not as good as with FooBar framework is a mistake. Raw speed is not the key factor for professionals. Cost is the key factor. And the cost of development, hosting and maintenance of an application with symfony is less than what it is for other solutions. This is untrue if what you need is a simple CMS, a weblog, or a script that says: "hello, world". But in those cases, you don't need a framework.
If what you need is a reliable, documented, full-featured, well supported, largely adopted, well coded, free and extensible framework, then go for symfony. Of course, symfony has some defaults; it is still young and is not funded by a billion dollar company. But "hello, world" speed is not a default that you should pay too much attention to. Besides, symfony is optimized as much as possible for all that it offers.
And if you need a second opinion, because you can't believe what the creator of a framework says about his own framework, perhaps you could listen to other companies who did choose symfony. Yahoo! picked symfony for a 20 Million users application, and I bet they measured the speed of a "hello, world" and other factors before making that decision. Many other large companies picked the symfony framework for applications that are crucial to their business, and they continue to trust us.
Yeah, "Hello World" is a very heavy real world web 2.0 ajaxified stylish application, maybe even better than flickr. People doing such benchmarks just don't know what they're talking about. Cheers!
I already knew symfony was suitable for real-world usage. Now, I a sure that the framework's speed is only a metric for the web development. Now, with all those web apps, I'm sure that symfony will grow faster in adoption as the community (the brazilian is an example, wait for more news!). Thanks a lot by this kind of thing!
I've worked in many companies, from small businesses to big corporations who are on the stock exchange. Reliability is key for most, only few actually look at speed. And those that do, usually don't work with frameworks at all.
My choice for Symfony was also made on the basis of functionality, stability and reliability. It is much more important than speed.
This is a little resumé of the point of view of my company, we left our home made framework for symfony, when we saw in which pragmatic way this framework was build over years and years.
I think Symfony is one of the first PHP solution to adress our clients needs.
We try to contribute by using it. Business cases is the best proof of reliabilty.
I think the wider PHP community which is less "needs oriented" than the java's one will consume your energy Francois.
I think that now PHP with symfony is good alternative to Java with Struts. PHP become more powerful and even more easier language! Thank you for symfony, Francois!
Francois I don't think it's worth the effort to respond to such "benchmarks".
Actually I have been using symfony from the beginning of this year. And i was pretty amazed with it, i convinced my team mates about how powerful it is, and we discovered it's really power when we switched from windows to Linux, in dev environment symfony generates pages in 115ms... But the very important point of my comment is: which framework uses Yahoo, why these company that handles millions of visit a day trusted their applications to Symfony and not another framework. These must be the point of compare for the others frameworks
Well I agree with Bogdan. Who cares what ror and Django users say? Symfony is a great framework and it shows.
What profiling tools do you use - ADP, XDebug, Benchmark, etc?
Hey, let those guys have fun with doing their benchmarking stuff. Maybe some day, they'll find out that the value of a framework can't be measured in milliseconds.
I've developed a symfony app for my former company. Now they are running a reliable and easy to maintain business solution. And nobody ever complained about speed (and this would be a bad joke having response times between 100 and 300 ms in DEV)!
Keep it up, Francios, SF is great work!
As far as Yahoo! is concerned, speed is a key factor, but more important than speed is stability, flexibility and a clean design. symfony provides just that: a professional framework that was built using best practices and proven design patterns. Although not perfect, symfony has proven to be extremely fast, extremely stable, and flexible enough to adapt to our environment.
In the end symfony allows us to create scalable and maintainable web applications fast.
As for performance, hello world doesn't really say much. There are many factors in determining application performance latency and max queries per second are the start. While hello world is an indicator of base latency, when comparing frameworks you really need to account for the full spectrum of features being provided with that base latency. This is where most people go wrong. Most people see a side by side framework comparison and simply choose the top of the list. They usually learn later it wasn't a good idea. These are also the people that usually scream the loudest.
In the end it depends on your requirements.
As everybody say a "hello world" test don't reveal the real power of a framework but those guy like this benchmark so why not implement an helper which disconnect a lot of feature of symfony and display an hello world with the fastest way :-) Someone could tell that is cheating but if this test could be asume as a real life website it could be consider as a feature that any framework should provide. Ok i stop joking and i get back to work.
The symfony experienced users better know the performance.. who care about the all the Php frameworks around. Our one and only choice is symfony..
well, <?php echo 'Hello, world' ?> is the best way to display I belive, rather than <?php echo "Hello, world" ?>.
You made your point clear, since you are not going to compete with the other php frameworks, don't consider 'hello, world' performance test with other php frameworks out there, as far as I know Symfony is the stable frameworks with bunch of features than any other out there.
[quote]for ($i = 0; $i<count($my_array); $i++)
instead, we try to always do:
for ($i = 0, $count = count($my_array); $i<$count; $i++)
This is because we know it makes a difference. [/quote] Is there any compelling reason for count($my_array) not to be implemented as struct array {...} my_array; if (i < my_array.num_elements) ... ; behind the curtains?
CPU Cycles are cheap (c)
[quote]for ($i = 0; $i<count($my_array); $i++)
instead, we try to always do:
for ($i = 0, $count = count($my_array); $i<$count; $i++)
This is because we know it makes a difference. [/quote]
While you are at it why not exchange $i++ with ++$i also. Skipping even one more op-code instruction by using pre-increment instead of post-increment. As you probably know post-incrementing need to use an internal variable to store the current value of $i before the instruction is completed.
And I thought, foreach was the best practice these days. What if count($array) was 6, but $array[4] would not be present, instead $array[6]?
I am very satisfied with your confident answer Francois. Symfony rules!
Very well, i never have doubt symfony is great.
Well...
I'm not sure symfony is really the best optimized framework.
PLEASE EXPLAIN ME THIS :
$ php batch/load_data.php
PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 23040 bytes) in /home/www/...
Come on: 16Mo !?!
It's even more thant the size of my symfony project folder (without log/ and cache/ ...). How that is possible ???
But I don't think it's your fault François, symfony team made greate code, for sure.
But, maybe, before winning 10ms on a foreach loop, you should maybe optimzed the 3rd part librairies, or the memory use... As 16Mo takes kind of long to allocate.
Bye
I ran into similar issues with a complex model, I haven't investigated too deep, but I assume it has something to do with the long outstanding PHP issue regarding the cyclic references memory leak.
As I see there are a lot fanatics out there. Good for symfony :) I think if you take a "hello world" benchmark in relation to the features of a framework it is definitely a valid performance test. What kind of generation times are acceptable for a framework? I made one project with symfony on a standard windows XP with xampp installed it takes ~2000ms to display a page. There are going 700ms on configuration. I see there are differences from system to system. On my own machine (xp pro, 2ghz centrino duo 2, 2g ram) it takes ~300ms. That's also too slow for a bunch of users. I tried every advice I could find to improve performance, nothing worked yet.
You are refering to big companies, but you do not mention how they build fast reliable apps with symfony nor you provide links to their apps.
symfony is a great framework for sure, but I am still missing speed or at least reasons for the lack of it.
just my 2 cents