In my previous post I outlined the method by which one goes about profiling a Django application. I used a view from linkrdr as an example. That view is responsible of aggregating, ranking, and sorting all of the links in a user's feeds (RSS, atom, Twitter, etc). The code from the post was an early, simplistic implementation of the view. I have, however, a much more robust scoring algorithm, written in Python, which I planned to used on the site.
You may have caught the word 'planned' in there. The algorithm turned out to be too slow. Rather, my Python implementation of the algorithm
was slower than what I deemed acceptable. After thinking of various architectural changes that could be made to solve the problem, I settled on a somewhat radical solution for a Django developer: I implemented the view in C++.
I realize that not every Django developer knows C++, nor should they, but those that do should realize it's a viable tool available when Python is just too slow. Eventually, you may get to a point where you can't really optimize your Python code any more. In this case, profiling will show that most of your time is spent in Python library calls. Once you hit that point, you've either written a horribly inefficient algorithm or you've got a problem not suited for Python.
When I realized I had hit that point with my view code, I panicked.
'What more is there to do?' I wondered. Then I remembered a work
project where I had written some C++ code that interfaced with Python.
From a technical perspective, there was nothing stopping me from
implementing some aspects of my Django app in C++ (besides the fact
that it's excruciating to write in coming from Python). Since linkrdr
is a single-person project, there are no teammates who need to grok the
code. I'm free to implement it as I wish.
Read on →