en EN   ru RU

Main

A2 OS

DRAKON

Arduino

Programs

Utilities

Web utilities

FontReg

Links

For webmaster

Contact info

CV


A2 OS forum

 
  Printable copy

Web utilities


Finding country by IP

SAGE


CountryByIPv4 utility prints out two-character country code and country name (based on ISO 3166) for supplied IPv4 address.

IP2Location™ IP-COUNTRY (DB1) database in CSV format is used. Free variant DB1.LITE of database available for downloading [1].

Utility was made for estimation of time needed for parsing of CSV file.

First variant of implementation used TStringList for reading CSV file, then strings of TStringList was parsed one by one. Same principle used, for example, in TjanSQL component.

Then I've replaced reading into TStringList by reading whole content into string with parsing directly from this string. This increased speed of loading the CSV base a bit more then 2 times.

Then I've figured out that too many time spent by copying of substrings from a string (Copy()) and by conversion of strings to numbers (Val()). I've ommited the conversion of strings to numbers, IP addresses leaved as is in string representation (as they stored in CSV). Finding by IP performed by comparison of strings instead of comparison of numbers. This requires of equal length of all IP addresses. All addresses that represented by strings shorter than 10 characters increased by leading zeros. Time measurements shows that this dicission practically didn't affect performance and finding by IP address takes about 0-2 ms.

As regarding of codes and names of countries, they required only when IP address gets found in base! So, why we need to copy all them during parsing? I've replaced fields for actual values of codes and names of countries by integer values of positions and length in original CSV base. This tricks again increased the speed of parsing a bit more then 2 times. In total, relatively to first variant this increased the speed approximately about to 4.2 times!

Source codes for all three variants of implementation for CodeTyphon present in archive.

Result of utility execution (system: AMD A10-7850K @ 3.70 GHz, DDR3 PC3-12800 @ 1.2 GHz):

c:\>CountryByIPv4.exe 202.186.13.4
CSV database parsed in 0,043 s, 130348 rows
IP address found in 0,002 s, country code: MY, name: Malaysia

c:\>CountryByIPv4.exe 19.5.10.1
CSV database parsed in 0,043 s, 130348 rows
IP address found in 0,000 s, country code: US, name: United States

c:\>CountryByIPv4.exe 25.5.10.2
CSV database parsed in 0,043 s, 130348 rows
IP address found in 0,000 s, country code: GB, name: United Kingdom

c:\>CountryByIPv4.exe 43.5.10.3
CSV database parsed in 0,043 s, 130348 rows
IP address found in 0,000 s, country code: JP, name: Japan

c:\>CountryByIPv4.exe 47.5.10.4
CSV database parsed in 0,043 s, 130348 rows
IP address found in 0,000 s, country code: CA, name: Canada

c:\>CountryByIPv4.exe 51.5.10.5
CSV database parsed in 0,042 s, 130348 rows
IP address found in 0,000 s, country code: DE, name: Germany

c:\>CountryByIPv4.exe 53.5.10.6
CSV database parsed in 0,043 s, 130348 rows
IP address found in 0,000 s, country code: DE, name: Germany

c:\>CountryByIPv4.exe 80.5.10.7
CSV database parsed in 0,043 s, 130348 rows
IP address found in 0,001 s, country code: GB, name: United Kingdom

c:\>CountryByIPv4.exe 81.5.10.8
CSV database parsed in 0,043 s, 130348 rows
IP address found in 0,001 s, country code: IL, name: Israel

c:\>CountryByIPv4.exe 83.5.10.9
CSV database parsed in 0,042 s, 130348 rows
IP address found in 0,001 s, country code: PL, name: Poland

c:\>CountryByIPv4.exe 85.5.10.0
CSV database parsed in 0,043 s, 130348 rows
IP address found in 0,001 s, country code: CH, name: Switzerland


Links:

[1] Free IP2Location LITE IP-COUNTRY.



Type

Name

Size

Downloads

zip

CountryByIPv4.zip

229 KiB

263

Last update: 7-2-16 18:18:51


 

alt CodeTyphon

Copyright © 2005-2017 SAGE. All rights reserved.