Skip to content

Reimplementation of country_code - why? #210

@Bost

Description

@Bost

The reimplementation of the country_code function in the 73f02fb does at least 3 dictionary lookups for any input: 3 lookups in the best-case and 5 lookups in worst-case scenarios:

                   # 1.                     # 2.
    if not country in is_3166_1 and country in synonyms:
        country = synonyms[country]     # 3.

    # Return code if country was found.
    if country in is_3166_1:            # 4.
        return is_3166_1[country]       # 5.

Where the original implementation does only 2 lookups in the best-case and only 4 lookups in worst-case scenarios:

    if country in is_3166_1:             # 1.
        return is_3166_1[country]        # 2.  
    else:
        if country in synonyms:          # 2.
            synonym = synonyms[country]  # 3.
            return is_3166_1[synonym]    # 4.
        else:
            if verbose:
                print ("No country_code found for '" + country + "'. Using '" + default_code + "'")
            return default_code

To me it looks like the old implementation is more efficient, right?

Yea i know, the code gets optimized, dictionaries get cached, processor instructions reordered, etc. by some "magic". Either way this function gets computed quite a lot, and one cannot predict "magic"... So then why such a change? Have you done any measurements?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions