W
W
WebSpider2013-03-24 17:41:29
PHP
WebSpider, 2013-03-24 17:41:29

The fastest algorithm for determining the country by phone number

Good afternoon. The task is to determine the country, knowing the mobile phone number. The algorithm should be as optimal as possible in terms of speed, because There are a lot of numbers and everything should work quickly.
Please, tell me the algorithm, or some ready-made solutions or libraries (JP does not matter). Thanks in advance

Answer the question

In order to leave comments, you need to log in

6 answer(s)
J
jetman, 2013-03-24
@WebSpider

What could be faster than a function that extracts the country code from the phone number and looks up the country using the hash table “code” -> “country”? Hash tables in most languages ​​are implemented adequately, the sampling rate will be very high.

M
MikhailEdoshin, 2013-03-24
@MikhailEdoshin

Google has libphonenumber , won't it work?

E
egorinsk, 2013-03-24
@egorinsk

> The algorithm should be as optimal as possible in terms of speed, because There are a lot of numbers and everything should work quickly.
Use C/Java/.NET or drop the word "fast", neither Python nor PHP can do it that fast. How many millions of numbers should be processed per second? Where do they come from?

K
kmike, 2013-03-24
@kmike

Dictionary option is good. You can try to squeeze out more crumbs: do not make several queries to the hash table (and, most likely, in a python loop) to determine country codes, but instead store the mapping “country code” -> “country” in Trie (or DAWG) . github.com/kmike/datrie
has a longest_prefix_item method that can get the longest prefix of a given number and its corresponding object. We write the “prefix-country” mapping to datrie.Trie, call longest_prefix_item and you're done.
It may well turn out to be faster (mainly due to the fact that the iteration will be moved to the C-extension and there will be no need to create copies of strings of length 1, 2, 3, etc.). I cannot guarantee that it will be faster, but, in any case, with a Trie from a python, at least 1 million numbers / sec should be obtained.

N
numitus, 2013-03-24
@numitus

You can create a tree of numbers. With ++, it will be possible to safely process 10 million numbers per second.

B
barker, 2013-03-24
@barker

Obviously, only according to the prefix table. There are a lot of databases with country codes on the Internet.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question