-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
performance of country_name_for_number #90
Comments
This probably has to do with the fact that The change that enforced no duplicates was added in 0a9a735, which points to r701 in the upstream libphonenumber. You can find that revision at google/libphonenumber@af00741, and based on the test case this was done because of NANP toll-free numbers. |
I understand that logic. I guess I could decorate it for my own needs.... |
Example for anyone else that may want a similar solution. from functools import wraps
def phonenumber_memoizer(e164_prefix_len=5):
"""e164_prefix_len should be long enough to uniquely identify region information
e164_prefix_len == 5 # +1NPA should be enough to identify a country
e164_prefix_len == 8 # +1NPANXX should be enough to identify a description/city
"""
def decorator(f):
memo = {}
@wraps(f)
def wrapper(numobj, *args, **kwargs):
#print("country_for_number_memoizer: country_code: {}".format(numobj.country_code))
if numobj.country_code == 1:
e164_num = format_number(numobj, phonenumbers.PhoneNumberFormat.E164)
npa = e164_num[:e164_prefix_len]
if npa not in memo:
memo[npa] = f(numobj, *args, **kwargs)
return memo[npa]
else:
return f(numobj, *args **kwargs)
return wrapper
return decorator
memo_number_type = phonenumber_memoizer(e164_prefix_len=8)(number_type)
memo_region_code_for_number = phonenumber_memoizer(e164_prefix_len=5)(region_code_for_number)
memo_country_name_for_number = phonenumber_memoizer(e164_prefix_len=5)(country_name_for_number)
memo_description_for_number = phonenumber_memoizer(e164_prefix_len=8)(description_for_number)
# setup p for test
p = parse('+13035551212', 'US')
print("parse_number:", timeit(lambda: parse('+13035551212','US'), number=10000))
print("format_number:", timeit(lambda: format_number(p, phonenumbers.PhoneNumberFormat.E164), number=10000))
print("is_possible_number:", timeit(lambda: is_possible_number(p), number=10000))
print("is_possible_number_with_reason:", timeit(lambda: is_possible_number_with_reason(p), number=10000))
print("is_valid_number:", timeit(lambda: is_valid_number(p), number=10000))
print
print("number_type:", timeit(lambda: number_type(p), number=10000))
print("region_code_for_number:", timeit(lambda: region_code_for_number(p), number=10000))
print("country_name_for_number:", timeit(lambda: country_name_for_number(p, 'en'), number=10000))
print("description_for_number:", timeit(lambda: description_for_number(p, 'en'), number=10000))
print
print("memo_number_type:", timeit(lambda: memo_number_type(p), number=10000))
print("memo_region_code_for_number:", timeit(lambda: memo_region_code_for_number(p), number=10000))
print("memo_country_name_for_number:", timeit(lambda: memo_country_name_for_number(p, 'en'), number=10000))
print("memo_description_for_number:", timeit(lambda: memo_description_for_number(p, 'en'), number=10000)) results
|
Why is country_name_for_number so slow compared to region_code_for_number?
I'd expect the a lookup for the region_code to country_name to be quite quick. It appears to check validity of the number. I haven't figured out where most time is spent yet.
Suppose there's any way to speed things up?
--Karl
results
The text was updated successfully, but these errors were encountered: