How python can be fast

So now that I’ve been working with python for a good 3-4 months, I’m starting to understand why people like it so much.. a bit part of it is that its just so damn simple. Its so easy to write code that works very quickly. The syntax is very elegant and easy to read. I’ve come to actually prefer the indent-based code grouping over most other language’s braces.

But one thing that has been plaguing me is how a dynamiclanguage could actually be faster than a static language like C++ or Java. I have faith in the theory, but its just that: faith. Until today, I didn’t have any real world examples. I needed something that you simply can’t do in a static language without bending over backwards. Conversely, I needed something that required the dynamicism of a language.

I just picked up the Python Cookbook the other day and I’ve found a very concrete example about how a dynamic language can be fast.

The example is from the beginning of the chapter on sorting and searching. They discuss the idea of decorate-sort-undecorate as a pattern for doing fast searching.

What I particularly like is the ‘key’ function that you can now pass to the sort method in Python 2.4. You can simply pass a method that retrieves the ‘key’ to a sort. Then when the sort is performed, the keys are more or less cached, and a fast internal comparison can be used.

For example:

MyList = [(2,3,4), (5,3,4), (1,2,3)]
def get_key(v):
  return v[2]


Now compare this to a traditional language, which might allow you to pass in a sort callback:

int *MyList[] = [ [2,3,4], [5,3,4], [1,2,3] ]

int MyCompare(void* a, void* b) {
  key1 = ((int*)a)[2]
  key2 = ((int*)b)[2]
  return key1 - key2

qsort(MyList, MyCompare)

Type definitions aside, aside what we see here is that MyCompare is going to be called much more than get_key. In this simple case, obviously it doesn’t matter much, but if you’re sorting something big and retrieving/calculating the key is slow, you’re going to retrieve and calculate the key way more often in C than in Python.

The reason this works only in a dynamic language is that a dynamic comparison function (MyCompare) in C++ is still not as dynamic as the key-retrieval function. The key retrieval function is a function who takes an arbitrary object as a parameter and returns an arbitrary object. Then the sort routine needs to arbitrarily compare two objects. You just can’t do that in C without a whole lot of work.

[As an aside, I know yes you could whack the hell out of a sort routine in C to do all sorts of crazy casting but nothing would be as general as the Python case and your code would be hopelessly unmaintainable.]

In the end, I think Python wins this one. It even gets extra points for implementing a readable get_key in one line.

  1. No comments yet.
(will not be published)