# Re: IP to Country (#139)

**URL:** <https://www.ruby-forum.com/t/re-ip-to-country-139/115050>\
**Category:** Ruby\
**Created:** [September 20, 2007, 5:53am UTC](https://www.ruby-forum.com/t/re-ip-to-country-139/115050 "2007-09-20T05:53:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@steve\_d](https://www.ruby-forum.com/u/steve_d)\
**Post date:** [September 20, 2007, 5:53am UTC](https://www.ruby-forum.com/t/re-ip-to-country-139/115050/1 "2007-09-20T05:53:06Z")

</div>

I did some benchmarking of my own. This one tests out the performance  
of a single lookup. I only ran this on solutions which work with  
IpToCountry.csv out of the box (aka do no prep work).

This benchmark ran each solution 1000 times with random IPs (invoking a  
new ruby instance on each run).

-bash-2.05b$ ruby bm.rb  
sorted by real time  
[[“steved.rb”, 10.5210349559784],  
[“adam.rb”, 10.817939043045],  
[“justin.rb”, 11.6265380382538],  
[“james.rb”, 12.3434510231018],  
[“eugene.rb”, 12.9024469852448],  
[“jesus.rb”, 13.2812840938568],  
[“erik.rb”, 13.3421401977539],  
[“simon.rb”, 14.0733840465546]]

sorted by sys time  
[[“simon.rb”, 0.796875],  
[“steved.rb”, 1.3046875],  
[“adam.rb”, 1.5078125],  
[“justin.rb”, 1.515625],  
[“jesus.rb”, 1.765625],  
[“eugene.rb”, 2.265625],  
[“erik.rb”, 2.421875],  
[“james.rb”, 2.4609375]]

sorted by user time  
[[“simon.rb”, 0.078125],  
[“eugene.rb”, 0.09375],  
[“jesus.rb”, 0.109375],  
[“steved.rb”, 0.1171875],  
[“justin.rb”, 0.1171875],  
[“james.rb”, 0.125],  
[“adam.rb”, 0.125],  
[“erik.rb”, 0.1953125]]

Here’s the benchmark source:

require ‘benchmark’  
require ‘pp’

random\_ips = Array.new(1000).map { Array.new(4).map  
{rand(256)}.join(’.’) }

sols = Dir["\*.rb"] - [$0]

times = {}

sols.each do |fn|  
stats = Benchmark.measure do  
random\_ips.each do |ip|  
%x{ ruby #{fn} #{ip} }  
end  
end  
times[fn] = stats  
end

sort\_times = proc { |m| times.sort\_by{|(k,v)| v.send(m)}.map{|(k,v)| [k,  
v.send(m)]} }

puts “sorted by real time”  
pp sort\_times[:real]  
puts

puts “sorted by sys time”  
pp sort\_times[:stime]  
puts

puts “sorted by user time”  
pp sort\_times[:utime]

Also, Adam, your solution seems to give incorrect answers sometimes, I  
noticed 33.33.33.33 and 88.88.88.88 both gave Not Found where as all the  
other solutions gave US and NO respectively.

- steve

---

<div class="post-metadata">

**Author:** ![steve\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce73a5/32.png) [@steve\_d](https://www.ruby-forum.com/u/steve_d)\
**Post date:** [September 20, 2007, 7:01am UTC](https://www.ruby-forum.com/t/re-ip-to-country-139/115050/2 "2007-09-20T07:01:09Z")

</div>

From: “steve d” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I did some benchmarking of my own. This one tests out the  
> performance of a single lookup. I only ran this on solutions  
> which work with IpToCountry.csv out of the box (aka do no prep work).

Cool. I guess mine was overlooked but it also does no prep work:  
[http://groups.google.com/group/comp.lang.ruby/msg/6162dae2bcf1bc3f?dmode=source](http://groups.google.com/group/comp.lang.ruby/msg/6162dae2bcf1bc3f?dmode=source)

Regards,

Bill
