可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
There are tons of PHP frameworks out there; some are pretty decent, others seem bloated and unnecessary. After watching Rasmus Lerdorf's presentation on PHP performance at Digg, I'm somewhat more concerned about the performance of the frameworks that I choose for building my applications with.
Two of the most popular frameworks that I'm aware of are CodeIgniter and CakePHP. From what I understand, CakePHP is a terrible resource hog. What about CodeIgniter? I hear that Zend Framework isn't all that slim, either.
Are there other (more performant) frameworks that I should be interested in? Would it be better to simply not use a framework at all? What considerations should I make towards choosing a PHP framework?
回答1:
Using a framework or not using a framework means you're making a choice between
Default Application Performance under load
Speed/Stability of Development
If you decide not to use a framework, you still need to do the things a framework would do. You're just coding them yourself in raw PHP, or developing your own framework that can remain lightweight since it only has to do what you want it to do, and not what the world wants it to do. You will get better performance, but you'll spend more time developing and debugging that code that a framework handles for you automatically.
What a framework buys you is speed in development time. You don't have to write out long complicated SQL queries, or debug someone else's long complicated SQL queries. You just need to create a table and instantiate a model. You don't need to decide where you're going to escape your SQL paramaters, because the framework defines where that happens. You don't need to get into huge political fights over where the business logic vs. presentation logic goes, because the framework defines this. A framework remove the need from having a system developer on your team, or removes you from having to think about/waste time on system development. You can get to coding your application faster and get measurable, visible results sooner.
Here's another way to think of it. PHP Frameworks are slower than PHP, but PHP itself is slower than C. Why not write your application directly in C?
There's no right answer here, it's one of those software engineering/development questions that's a matter of what your current situation demands. The default choice of the industry these days is to use a framework, because if you don't your competitors will release an application that has slower PHP processing than yours, but hits the market three months earlier.
Finally, one last thing to consider from that talk. Rasmus said that Most of the time the perceived performance of your application is in the frontend. Both the Javascript code and how the browser is caching the requests it makes back to your server. PHP is an awful, horrible language that's rarely the bottleneck. When it is the bottle neck, you can usually make a few adjustments (opt code cache, focused refactoring) that will remove the performance bottleneck.
回答2:
you might want to look into the lightweight frameworks category. there's fat-free, doophp, limonade, etc. they're lightweight, but not less-powerful than you might think. don't rely on published benchmarks. instead, benchmark against your own objectives.
回答3:
IMHO, the advantages of using a framework far outweigh the disadvantages of any overhead costs that may come with it. Of course there are always exceptions to the rule, these being:
- The project is small enough to not warrant a framework
- The framework itself is horribly designed
But for the majority of cases, you shouldn't worry about it. I've been using Symfony (versions 1.2 & 1.4) on a number of projects, and haven't once thought "this is too resource heavy". I will gladly sacrifice some resources in exchange for all the tools a framework provides that makes my life easier, and the job take less time ;)
回答4:
I have built some big applications with the Yii PHP framework. It's pretty speedy because it "lazy" auto-loads classes. I have no performance complaints.
If you run a PHP opcode cache/accelerator like APC or eAccelerator, and do all of the usual best practices for page loading speed on LAMP setups, then using a PHP framework will be WELL WORTH IT for the development time saved. Performance will not really be an issue unless you are serving an astronomical number of requests!
回答5:
I've only used CakePhp in anger over a period of several years. Yes, it is big and may seem bloated but, to be honest, I think that if I had used any other framework it would be as big by the time I've added everything that I have and use with Cake.
Speed-wise, we're talking small fractions of seconds difference between the contenders. Optimizing your site using tools like Yslow and gzip and, of course, sensible design, will realize greater improvements than the choice of framework.
http://www.grasset.es is a site I built about 4 years ago with Cake. It's big and complex, but I don't think it's slow.
回答6:
As far as I am aware CodeIgniter is a little bit of a lighter framework. Most people I know use CodeIgniter vs. CakePHP. But I've been using CakePHP for a number of years.
If you really worry about scale, take a look at using a NOSQL solution like MongoDB or CouchDB from the onset. (I use MongoDB and for most applications mongo can replace MYSQL). Other than front end, database calls are often what slows you down.
Another thing to consider is having a delayed worker queue. For this like processing images into thumbnails it is often best to have a separate process dealing with this and avoid having the user wait.
Nearly all big scale websites use caching. Database caching and HTML caching. You can look at MEMCACHED wich nearly everyone who's big uses and stores your cache in memory. MongoDB also has a reported memcached like speeds when calling data.
Like everyone else says on here, it's mostly not the framework that doesn't scale. It's your whole architecture. I haven't looked at the other lightweight frameworks but if you are picking between CakePHP and CodeIgniter, I would personally go with CodeIgniter. Also just make sure you continue to refactor your code over time this will give you the opportunity to make changes that speed up your app.