jscarle
jscarle

Reputation: 1315

Converting selected words to a specific casing

I have a list of words that I need to format to specific casing per word, however the current method I'm using is highly inefficient.

The list of words and the needed casing per word:

    private static readonly string[] ProperCaseHeaders = { "Accept", "Accept-Encoding", "Accept-Language", "Alert-Info", "Allow", "Authentication-Info", "Authorization", "Call-ID", "Call-Info", "Contact", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Length", "Content-Type", "CSeq", "Date", "Error-Info", "Expires", "From", "In-Reply-To", "Max-Forwards", "Min-Expires", "MIME-Version", "Organization", "Priority", "Proxy-Authenticate", "Proxy-Authorization", "Proxy-Require", "Record-Route", "Reply-To", "Require", "Retry-After", "Route", "Server", "Subject", "Supported", "Timestamp", "To", "Unsupported", "User-Agent", "Via", "Warning", "WWW-Authenticate" };

The method I'm using:

    public static string ToProperCaseHeader(this string header)
    {
        foreach (string properCaseHeader in ProperCaseHeaders)
            if (header.Equals(properCaseHeader, StringComparison.InvariantCultureIgnoreCase))
                return properCaseHeader;
        return header;
    }

And the performance profiling results:

enter image description here

As you can see, the majority of the time is spent on the comparaison using InvariantCultureIgnoreCase.

Its possible for headers to arrive in a variety of casings, an in all cases, I want them to be normalized. For example:

CALL-ID, call-id, Call-Id, CALL-id should all be normalized to Call-ID. CONTENT-TYPE, content-type, Content-type, CONTENT-type should all be normalized to Content-Type.

It's not necessarily a simple Proper Case conversion.

Anyone have any ideas?

Upvotes: 0

Views: 51

Answers (1)

Nayan
Nayan

Reputation: 164

Here is the code that you can replace with your code for performance improvement.

class Program
{
    private static readonly string[] ProperCaseHeaders = { "Accept", "Accept-Encoding", "Accept-Language", "Alert-Info", "Allow", "Authentication-Info", "Authorization", "Call-ID", "Call-Info", "Contact", "Content-Disposition", "Content-Encoding", "Content-Language", "Content-Length", "Content-Type", "CSeq", "Date", "Error-Info", "Expires", "From", "In-Reply-To", "Max-Forwards", "Min-Expires", "MIME-Version", "Organization", "Priority", "Proxy-Authenticate", "Proxy-Authorization", "Proxy-Require", "Record-Route", "Reply-To", "Require", "Retry-After", "Route", "Server", "Subject", "Supported", "Timestamp", "To", "Unsupported", "User-Agent", "Via", "Warning", "WWW-Authenticate" };

    static void Main(string[] args)
    {
        Dictionary<string, string> headers = new Dictionary<string, string>();

        foreach (var item in ProperCaseHeaders)
            headers.Add(item.ToLower(), item);

        Stopwatch stopwatch = Stopwatch.StartNew();
        Console.WriteLine($"Specific header for aleRt-Info {ProperCaseHeaders[3].ToProperCaseHeader(ProperCaseHeaders)}");
        Console.WriteLine($"Using Old Method End {stopwatch.ElapsedMilliseconds}");

        Console.WriteLine();

        stopwatch = Stopwatch.StartNew();
        Console.WriteLine($"Specific header for aleRt-Info {ProperCaseHeaders[3].GetProperCaseHeader(headers)}");
        Console.WriteLine($"Using New Method End {stopwatch.ElapsedMilliseconds}");

        Console.ReadLine();
    }

}

public static class Extension
{
    public static string ToProperCaseHeader(this string header, string[] ProperCaseHeaders)
    {
        foreach (string properCaseHeader in ProperCaseHeaders)
            if (header.Equals(properCaseHeader, StringComparison.InvariantCultureIgnoreCase))
                return properCaseHeader;
        return header;
    }

    public static string GetProperCaseHeader(this string header, Dictionary<string, string> headers)
    {
        var response = string.Empty;
        headers.TryGetValue(header.ToLower(), out response);
        return response;
    }
}

Result is following.

Specific header for aleRt-Info Alert-Info
Using Old Method End 48

Specific header for aleRt-Info Alert-Info
Using New Method End 1

Upvotes: 1

Related Questions