Skip to content
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

optimized GetCountryByNameConsiderTranslation in CountryProviderExtension.cs #29

Conversation

sebrandt1
Copy link

Benchmarks

Method CountryName Mean Error StdDev Execution Time Comparison
PreOptimization Danmark 24.68 ms 0.175 ms 0.146 ms +117479%
PreOptimization Denmark 25.15 ms 0.480 ms 0.658 ms +120062%
PreOptimization India 42.49 ms 0.592 ms 0.525 ms +116727%
PreOptimization Ireland 47.11 ms 0.792 ms 0.778 ms +122774%
PreOptimization Norge 74.14 ms 1.312 ms 1.227 ms +96233%
PreOptimization Norway 75.42 ms 0.702 ms 0.657 ms +94010%
PreOptimization Sverige 100.58 ms 1.421 ms 1.187 ms +82825%
PreOptimization Sweden 97.38 ms 1.846 ms 1.727 ms +84740%
Method CountryName Mean Error StdDev Execution Time Comparison
PostOptimization Danmark 20.99 us 0.411 us 0.422 us -99.91%
PostOptimization Denmark 20.93 us 0.257 us 0.241 us -99.91%
PostOptimization India 36.37 us 0.332 us 0.310 us -99.91%
PostOptimization Ireland 38.34 us 0.268 us 0.250 us -99.91%
PostOptimization Norge 78.00 us 0.869 us 0.813 us -99.89%
PostOptimization Norway 80.14 us 1.295 us 1.591 us -99.89%
PostOptimization Sverige 117.43 us 1.233 us 1.154 us -99.88%
PostOptimization Sweden 114.78 us 1.028 us 0.858 us -99.88%

@tinohager
Copy link
Member

What do you think of this variant?

using System;
using System.Linq;

namespace Nager.Country.Translation
{
    /// <summary>
    /// CountryProvider Extension
    /// </summary>
    public static class CountryProviderExtension
    {
        private static readonly Lazy<TranslationProvider> TranslationProvider = new Lazy<TranslationProvider>();

        ///<inheritdoc/>
        public static ICountryInfo GetCountryByNameConsiderTranslation(
            this ICountryProvider countryProvider,
            string countryName)
        {
            var countries = countryProvider.GetCountries();

            foreach (var country in countries)
            {
                if (country.CommonName.Equals(countryName, StringComparison.OrdinalIgnoreCase))
                {
                    return country;
                }

                if (country.OfficialName.Equals(countryName, StringComparison.OrdinalIgnoreCase))
                {
                    return country;
                }

                var countryTanslation = TranslationProvider.Value.GetCountryTranslation(country.Alpha2Code);
                if (countryTanslation.Translations.Any(translation => translation.Name.Equals(countryName, StringComparison.OrdinalIgnoreCase)))
                {
                    return country;
                }
            }

            return null;
        }
    }
}

@tinohager tinohager self-assigned this Aug 14, 2024
@sebrandt1
Copy link
Author

That looks good to me.

@tinohager
Copy link
Member

Optimize performance of GetCountryByNameConsiderTranslation
e4c8350

@tinohager tinohager closed this Aug 20, 2024
@sebrandt1 sebrandt1 deleted the feature/GetCountryByNameConsiderTranslation-optimization branch August 26, 2024 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants