0

I have an Django application where the URL's to be handled shall have a specific pattern like /servername/alpha/beta/2/delta/10/pie/1 Now i will be be needing these parameters contained in the URL and will persist them to the database when a URL beginning from /servername/ is being called.
So i have 2 ways of doing it

  1. Pass parameters along with request to the relevant view.In this case my regex would ensure that i have param1 through param7 having values alpha,beta,2,delta,10,pie and 1 respectively.
  2. Pass only the request without passing the parameters.I will either parse using regex the request.path_info or split request.path_info on a "/" and obtain relevant entries

    Which of these two methods shall be preferred in order to have better performance in terms of CPU and memory or maybe other factors i am not aware of.
    I believe that one can compare the two using time functions but i believe it won't present an accurate picture.Theoretically which approach shall be preferred and why?
Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
  • 1
    Firstly, you should be sure that you need this optization. Are you sure? Is it a bottleneck of your app? If not, no optimization needed, choose any option, first looks more pythonic and clear. – DrTyrsa Dec 13 '11 at 13:47
  • At this stage i may not need optimization,but just to know,if in future,i may want to make this application a little more fast. – Nipun Batra Dec 13 '11 at 14:14
  • it seems pretty unlikely that url parsing is going to be even close to the slowest part if your request cycle – second Dec 13 '11 at 15:11

1 Answers1

1

Option two is inherently slower as your view would need to do this parsing each time, whereas Django's standard URL parser works off of compiled regular expressions. (The urlpatterns in urls.py is compiled once on first run.)

However, the speed difference in either approach is pretty negligible. This will never be the bottleneck of your application; focus on things like your database and queries thereof and any I/O operations in your app (anything that writes or reads extensively from the hard drive). Those are where apps get slowed down. Otherwise, you're talking in terms of saving a millisecond here or there, which is fruitlessly pointless.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thanks!I guess going through http://stackoverflow.com/questions/550632/favorite-django-tips-features shall help me find tips to optimise my application.Also there's this video http://blip.tv/pycon-us-videos-2009-2010-2011/django-deployment-workshop-3651591 from the Django founder which i guess shall be handy to others also and admin might wanna put this link in some thread – Nipun Batra Dec 13 '11 at 17:09