5

I was wondering what the performance difference is between using plain python files to make web pages and using Django. I was just wondering if there was a significant difference between the two. Thanks

4 Answers 4

12

Django IS plain Python. So the execution time of each like statement or expression will be the same. What needs to be understood, is that many many components are put together to offer several advantages when developing for the web:

  • Removal of common tasks into libraries (auth, data access, templating, routing)
  • Correctness of algorithms (cookies/sessions, crypto)
  • Decreased custom code (due to libraries) which directly influences bug count, dev time etc
  • Following conventions leads to improved team work, and the ability to understand code
  • Plug-ability; Create or find new functionality blocks that can be used with minimal integration cost
  • Documentation and help; many people understand the tech and are able to help (StackOverflow?)

Now, if you were to write your own site from scratch, you'd need to implement at least several components yourself. You also lose most of the above benefits unless you spend an extraordinary amount of time developing your site. Django, and other web frameworks for every other language, are designed to provide the common stuff, and let you get straight to work on business requirements.

If you ever banged out custom session code and data access code in PHP before the rise of web frameworks, you won't even think of the performance cost associated with a framework that makes your job interesting and eas(y)ier.

Now, that said, Django ships with a LOT of components. It is designed in such a way that most of the time, they won't affect you. Still, a surprising amount of code is executed for each request. If you build out a site with Django, and the performance just doesn't cut it, you can feel free to remove all the bits you don't need. Or, you can use a 'slim' python framework.

Really, just use Django. It is quite awesome. It powers many sites millions times larger than anything you (or I) will build. There are ways to improve performance significantly, like utilizing caching, rather than optimizing a loop over custom Middleware.

Sign up to request clarification or add additional context in comments.

2 Comments

Personally, I wouldn't close with an unqualified "just use Django" -- if he has more advanced ORM needs, for instance, a framework with tighter integration to SQLAlchemy might be ideal. On the other hand, the overriding thing you were trying to convey (don't try to do yourself what umpteen tools exist to do for you) is spot on.
@Charles, that's a fair point. There are other frameworks and tools built with python that could be used, but I'm recommending Django without having used any of the others :P
5

Depends on how your "plain Python" makes web pages. If it uses a templating engine, for instance, the performance of that engine is going make a huge difference. If it uses a database, what kind of data access layer you use (in the context of the requirements for that layer) is going to make a difference.

The question, thus, becomes a question of whether your arbitrary (and presently unstated) toolchain choices have better runtime performance than the ones selected by Django. If performance is your primary, overriding goal, you certainly should be able to make more optimal selections. However, in terms of overall cost -- ie. buying more web servers for the slower-runtime option, vs buying more programmer-hours for the more-work-to-develop option -- the question simply has too many open elements to be answerable.

6 Comments

Well I was just gonna use .py files that display HTML etc from the server. I mean if the difference is not HUGE, than I would much rather use something like django for maintenance made easier with master pages etc.
+1. @Corey: Django is just python. How much of django you planned to implement in your "Plain python" I don't know, but in general I don't see the benefit of re-inventing the wheel -- you'd still be using the third party templating engines, database interfaces, databases themselves, etc.
@Corey: Even if your .py file just "prints" to stdout to display the HTML etc, some glue in the middle needs to be present to translates a request coming into a webserver into an input the Python interpreter will understand and correctly dispatches them to finally execute your .py file. You'd need to implement this middleware yourself to make a good comparion, then. But then where does the "script" end and the web framework begin? In the end, you are essentially trying to reinvent Django, or implement a new framework yourself.
"Plain" .py files can't display anything on a webserver - they need something that will invoke them and give them a chance to produce HTML output. About the closest to "plain" Python I can think of in this context would be a CGI script, and then the performance is still highly dependent on the web server and CGI gateway, plus you miss out on all of the niceties that Django can provide. The maxim "Premature optimisation is the root of all evil" applies to the selection of web tools as much as it does to any other kind of programming.
But that's what I meant - I was agreeing with Charles that "plain Python" doesn't really mean anything in this context. Whether it's Python+CGI, Python+mod_python, Python+mod_wsgi, Django, etc, there is something standing between the developer's Python code and the web server and the choice of gateway will affect how the developer interacts with the host web server, as well as the kind of performance they can expect to achieve.
|
3

Premature optimisation is the root of all evil.

Django makes things extremely convenient if you're doing web development. That plus a great community with hundreds of plugins for common tasks is a real boon if you're doing serious work.

Even if your "raw" implementation is faster, I don't think it will be fast enough to seriously affect your web application. Build it using tools that work at the right level of abstraction and if performance is a problem, measure it and find out where the bottlenecks are and apply optimisations. If after all this you find out that the abstractions that Django creates are slowing your app down (which I don't expect that they will), you can consider moving to another framework or writing something by hand. You will probably find that you can get performance boosts by caching, load balancing between multiple servers and doing the "usual tricks" rather than by reimplementing the web framework itself.

Comments

2

Django is also plain Python.

See the performance mostly relies on how efficient your code is.

Most of the performance issues of software arise from the inefficient code, rather than choice of tools and language. So the implementation matters. AFAIK Django does this excellently and it's performance is above the mark.

1 Comment

I'm not sure I can agree about Django's performance being above-average. Profiling my employer's very large app, I was surprised to see that an inordinate amount of our time is spent not in custom logic or database access but in template rendering (not application code called by the templates, but the rendering process itself)! To be sure, differently-designed templates might stress the engine less -- but even so, the result is hardly a vote of confidence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.