46 points · by jcsalterego · 15y ago · 5 comments
If you are concerned with speed, a bloom filter is exactly the kind of thing I'd never implement in Python. Twiddling bits is orders of magnitude more expensive than in C.
If you care about the cost of the Python glue code, the PyPy project nicely optimizes that away. Unless you're writing for a Google production server, the programmer time writing this in C will likely never be paid back in saved CPU cycles.
I wrote a simple bloom filter in C. It took about an hour, including debugging. Here is my bloom filter vs. CPython and PyPy doing 1M lookups:
$ time ./bloom
real 0m0.027s
user 0m0.022s
sys 0m0.002s
$ time python bloom.py
real 0m32.876s
user 0m32.693s
sys 0m0.119s
$ time pypy bloom.py
real 0m42.280s
user 0m42.054s
sys 0m0.178s
In other words, C was 1216x (or 121,600%) faster than CPython, and 1564x (or 156,400%) faster than PyPy. If it took 1 second in C, it would take 20 minutes in Python. Put another way, Python runs this algorithm about as fast as a the C algorithm would run on an 8086 from 1978.Here is my C implementation: http://pastebin.com/TexbJjbB
Here is the Python program I compared against (using code from the article): http://pastebin.com/gqw7C68e
That being said, you've done a great job showing that it doesn't take long to correctly implement this algorithm in C. That is a nice win.