[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] IP to Country (#139

James Gray

9/14/2007 12:32:00 PM

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rub...

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

This week's Ruby Quiz is to write a simple utility. Your program should accept
an IP address as a command-line argument and print out the two letter code for
the country that IP is assigned in. You can find a database for the matching
at:

http://software77.net/cgi-bin/ip-country...

To keep the problem interesting though, let's write our programs with a focus on
speed and memory efficiency.

$ time ruby ip_to_country.rb 68.97.89.187
US

real 0m0.314s
user 0m0.259s
sys 0m0.053s

37 Answers

Simon Kröger

9/14/2007 7:19:00 PM

0

Ruby Quiz wrote:
> [...]
>
> $ time ruby ip_to_country.rb 68.97.89.187
> US
>
> real 0m0.314s
> user 0m0.259s
> sys 0m0.053s

Is an 'initialisation run' allowed to massage the data?
(we should at least split the benchmarks to keep it fair)

Is it motivating or a spoiler to post timings?

cheers

Simon



James Gray

9/14/2007 7:24:00 PM

0

On Sep 14, 2007, at 2:20 PM, Simon Kröger wrote:

> Ruby Quiz wrote:
>> [...]
>>
>> $ time ruby ip_to_country.rb 68.97.89.187
>> US
>>
>> real 0m0.314s
>> user 0m0.259s
>> sys 0m0.053s
>
> Is an 'initialisation run' allowed to massage the data?
> (we should at least split the benchmarks to keep it fair)

My script does need and initialization run, yes. I don't see any
harm in paying a one time penalty to set things up right.

> Is it motivating or a spoiler to post timings?

Motivating, definitely. :)

James Edward Gray II

Simon Kröger

9/14/2007 7:32:00 PM

0

James Edward Gray II wrote:
> On Sep 14, 2007, at 2:20 PM, Simon Kröger wrote:
>
>> Ruby Quiz wrote:
>>> [...]
>>>
>>> $ time ruby ip_to_country.rb 68.97.89.187
>>> US
>>>
>>> real 0m0.314s
>>> user 0m0.259s
>>> sys 0m0.053s
>>
>> Is an 'initialisation run' allowed to massage the data?
>> (we should at least split the benchmarks to keep it fair)
>
> My script does need and initialization run, yes. I don't see any harm
> in paying a one time penalty to set things up right.
>
>> Is it motivating or a spoiler to post timings?
>
> Motivating, definitely. :)
>
> James Edward Gray II

Ok, my script does not need any initialization, it uses the file
IpToCountry.csv exactly as downloaded.

----------------------------------------------------------------
$ ruby -v
ruby 1.8.4 (2005-12-24) [i386-cygwin]

$ time ruby quiz139.rb 68.97.89.187
US

real 0m0.047s
user 0m0.030s
sys 0m0.030s

$ time ruby quiz139.rb 84.191.4.10
DE

real 0m0.046s
user 0m0.046s
sys 0m0.015s
----------------------------------------------------------------

This is on a Pentium M 2.13GHz Laptop with 2GB RAM and rather slow HD.

cheers

Simon

James Gray

9/14/2007 7:38:00 PM

0

On Sep 14, 2007, at 2:35 PM, Simon Kröger wrote:

> James Edward Gray II wrote:
>> On Sep 14, 2007, at 2:20 PM, Simon Kröger wrote:
>>
>>> Ruby Quiz wrote:
>>>> [...]
>>>>
>>>> $ time ruby ip_to_country.rb 68.97.89.187
>>>> US
>>>>
>>>> real 0m0.314s
>>>> user 0m0.259s
>>>> sys 0m0.053s
>>>
>>> Is an 'initialisation run' allowed to massage the data?
>>> (we should at least split the benchmarks to keep it fair)
>>
>> My script does need and initialization run, yes. I don't see any
>> harm
>> in paying a one time penalty to set things up right.
>>
>>> Is it motivating or a spoiler to post timings?
>>
>> Motivating, definitely. :)
>>
>> James Edward Gray II
>
> Ok, my script does not need any initialization, it uses the file
> IpToCountry.csv exactly as downloaded.
>
> ----------------------------------------------------------------
> $ ruby -v
> ruby 1.8.4 (2005-12-24) [i386-cygwin]
>
> $ time ruby quiz139.rb 68.97.89.187
> US
>
> real 0m0.047s
> user 0m0.030s
> sys 0m0.030s
>
> $ time ruby quiz139.rb 84.191.4.10
> DE
>
> real 0m0.046s
> user 0m0.046s
> sys 0m0.015s
> ----------------------------------------------------------------

Wow, I'm impressed. Can't wait to see that code!

James Edward Gray II

Simon Kröger

9/14/2007 8:15:00 PM

0

> Wow, I'm impressed. Can't wait to see that code!

Thanks. :)

Because startup time startet to take over the benchmark:
----------------------------------------------------------------
$ time ruby quiz139.rb 68.97.89.187 84.191.4.10 80.79.64.128 210.185.128.123
202.10.4.222 192.189.119.1
US
DE
RU
JP
AU
EU

real 0m0.078s
user 0m0.046s
sys 0m0.031s
----------------------------------------------------------------

and by the way: thanks for telling me such a database exists!

cheers

Simon

diego scataglini

9/14/2007 9:59:00 PM

0

I think that the timing of the scripts are not a good index. It all
depends on what hardware/os you are running it on.
If we want to use speed as an index we should probably have J.E.
compare them all on the same machine.

Maybe we could also write a ruby script that runs all the entry
scripts and time them, and that could be another ruby quiz which will
also be voted on speed and then we could write a ruby script to time
those entries an then we could .... Just ignore this paragraph

Diego Scataglini

On Sep 14, 2007, at 3:35 PM, Simon Kröger <SimonKroeger@gmx.de> wrote:

> James Edward Gray II wrote:
>> On Sep 14, 2007, at 2:20 PM, Simon Kröger wrote:
>>
>>> Ruby Quiz wrote:
>>>> [...]
>>>>
>>>> $ time ruby ip_to_country.rb 68.97.89.187
>>>> US
>>>>
>>>> real 0m0.314s
>>>> user 0m0.259s
>>>> sys 0m0.053s
>>>
>>> Is an 'initialisation run' allowed to massage the data?
>>> (we should at least split the benchmarks to keep it fair)
>>
>> My script does need and initialization run, yes. I don't see any
>> harm
>> in paying a one time penalty to set things up right.
>>
>>> Is it motivating or a spoiler to post timings?
>>
>> Motivating, definitely. :)
>>
>> James Edward Gray II
>
> Ok, my script does not need any initialization, it uses the file
> IpToCountry.csv exactly as downloaded.
>
> ----------------------------------------------------------------
> $ ruby -v
> ruby 1.8.4 (2005-12-24) [i386-cygwin]
>
> $ time ruby quiz139.rb 68.97.89.187
> US
>
> real 0m0.047s
> user 0m0.030s
> sys 0m0.030s
>
> $ time ruby quiz139.rb 84.191.4.10
> DE
>
> real 0m0.046s
> user 0m0.046s
> sys 0m0.015s
> ----------------------------------------------------------------
>
> This is on a Pentium M 2.13GHz Laptop with 2GB RAM and rather slow HD.
>
> cheers
>
> Simon
>

James Gray

9/14/2007 10:16:00 PM

0

On Sep 14, 2007, at 4:58 PM, diego scataglini wrote:

> I think that the timing of the scripts are not a good index. It all
> depends on what hardware/os you are running it on.
> If we want to use speed as an index we should probably have J.E.
> compare them all on the same machine.

I view it that we are getting a rough idea of speeds, not exact
counts. Both scripts timed so far seem to be able to answer the
question in under a second on semi-current hardware. Good enough for
me.

> Maybe we could also write a ruby script that runs all the entry
> scripts and time them, and that could be another ruby quiz which
> will also be voted on speed and then we could write a ruby script
> to time those entries an then we could .... Just ignore this paragraph

Thank you for volunteering... ;)

James Edward Gray II

Bill Kelly

9/14/2007 11:45:00 PM

0


From: "Simon Kröger" <SimonKroeger@gmx.de>
>
> Ok, my script does not need any initialization, it uses the file
> IpToCountry.csv exactly as downloaded.

We probably did something similar. :) Mine also works on the
unmodified IpToCountry.csv file.

$ time ruby 139_ip_to_country.rb 67.19.248.74 70.87.101.66 205.234.109.18 217.146.186.221 62.75.166.87
US
US
US
GB
DE

real 0m0.122s
user 0m0.015s
sys 0m0.000s


(ruby 1.8.4 (2005-12-24) [i386-mswin32], timed from cygwin bash shell,
2GHz athlon64, winxp.)

I don't think the timings are very accurate on this system. It
didn't change much whether I looked up one IP or five.

. . Looking up 80 IPs on one command line resulted in:

real 0m0.242s
user 0m0.015s
sys 0m0.016s


Regards,

Bill


Clifford Heath

9/15/2007 7:44:00 AM

0

Ruby Quiz wrote:
> This week's Ruby Quiz is to write a simple utility. Your program should accept
> an IP address as a command-line argument and print out the two letter code for
> the country that IP is assigned in.

I assume that it's not ok for folk to use my Ruby GeoIP gem?
It has a reasonable compromise between speed and memory usage.
:-)

Clifford Heath.

James Gray

9/15/2007 3:48:00 PM

0

On Sep 15, 2007, at 2:45 AM, Clifford Heath wrote:

> Ruby Quiz wrote:
>> This week's Ruby Quiz is to write a simple utility. Your program
>> should accept
>> an IP address as a command-line argument and print out the two
>> letter code for
>> the country that IP is assigned in.
>
> I assume that it's not ok for folk to use my Ruby GeoIP gem?
> It has a reasonable compromise between speed and memory usage.
> :-)

Please do. I would love to see how it stacks up against the custom
solutions we will no doubt see.

James Edward Gray II