Super fast IP to lat/lng in Rails - Part 2 9

Posted by aaron
on Tuesday, October 30
In Super fast IP to lat/lng in Rails, I showed a solution for fast IP to lat/lng resolution in rails. I called it "Super fast" because it performed orders of magnitude faster the the RESTful interface, but it was also "Super fast" to implement. That being said, Kyle made a comment to check out the GeoIP gem. I had heard of MaxMind before, but I didnt want to spend hundreds of dollars to solve this problem. What I didnt know was they also have a free download of their "lite" datasource. They have a GeoLiteCountry and GeoLiteCity version, although only the City version has lat/lng info. They provide wrappers in most languages (including Ruby), and while Kyle's suggested geoip, I found geoip_city on RubyForge which I like a bit better.

[UPDATED]: I had forgotten to include the install instructions for getting the GeoIP C library. Install the C bindings, the gem (which isnt packaged as a gem for easy download) and get the data.
wget http://www.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
tar -zxvf GeoIP.tar.gz
cd GeoIP
./configure && make && sudo make install

wget http://rubyforge.org/frs/download.php/27077/geoip_city-0.1.gem
sudo gem install geoip_city-0.1.gem
wget http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
sudo mkdir /usr/local/share/GeoIP
sudo mv GeoLiteCity.dat /usr/local/share/GeoIP/GeoLiteCity.dat
Then the ruby part:
>> require 'geoip_city'
>> g = GeoIPCity::Database.new('/usr/local/share/GeoIP/GeoLiteCity.dat')
>> res = g.look_up('4.2.2.2')
>> puts "lat: #{res[:latitude]} lng: #{res[:longitude]}"
lat: 38.0 lng: -97.0
So the question is (although its probably obvious), which is faster?
>>  Benchmark.measure { 1000.times {Hostip.geocode('4.2.2.2')}}.total
=> 1.02
>>   Benchmark.measure { 100000.times {g.look_up('4.2.2.2')}}.total
=> 0.5
Conclusion? The C library is MUCH faster than my database version. 200k req/s vs. 1k req/s. As a side note, I also tested the "geoip" gem, and it was about 3x faster than my database version.

But all in all, for the 5 minutes it took me to write the original version, it was fast enough... but MaxMind GeoLiteCity + the geoip_city gem is Super fast-er.