HomeSearch

C# Char Lowercase Optimization

This C# example page optimizes char lowercase and uppercase methods. A benchmark is provided.
Char lowercase. A character can be lowercased with various algorithms. Some approaches are faster than others. And some approaches handle special cases better than others.Optimization
An optimization. Here we see alternative lower and upper C# methods on char, with a benchmark. These methods can help certain algorithms go faster.Benchmark
This class can lowercase and uppercase a char in two different ways. The Framework methods, char.ToLower and char.ToUpper, work on a wider range of characters than these methods.char.ToLower

Important: You can tune these methods to work on other characters. An offset into a string can be provided.

C# program that implements lowercase optimization public static class Program { static string _lookupStringL; static string _lookupStringU; public static char ToLowerFast(char c) { // Use char lookup table. return _lookupStringL[c]; } public static char ToUpperFast(char c) { // Use char lookup table. return _lookupStringU[c]; } public static char ToLowerFastIf(char c) { // Use if-statement. if (c >= 'A' && c <= 'Z') { return (char)(c + 32); } else { return c; } } public static char ToUpperFastIf(char c) { // Use if-statement. if (c >= 'a' && c <= 'z') { return (char)(c - 32); } else { return c; } } public static void Main() { // Init the strings. char[] lData = new char[128]; char[] uData = new char[128]; for (int i = 0; i < 128; i++) { char value = (char)i; if (!char.IsLetter(value)) { lData[i] = '-'; uData[i] = '-'; } else { lData[i] = ToLowerFastIf(value); uData[i] = ToUpperFastIf(value); } } _lookupStringL = new string(lData); _lookupStringU = new string(uData); // Test the char transformation methods. string phrase = "Is it a BIRD?"; char[] array = phrase.ToCharArray(); for (int i = 0; i < array.Length; i++) { array[i] = ToLowerFast(array[i]); } string result = new string(array); System.Console.WriteLine("IN: " + phrase); System.Console.WriteLine("OUT: " + result); } }
Notes. The strings in the class contains the uppercase or lowercase versions of the characters corresponding to each index. You can use look up "a" and receive an "A".

And: When you lookup a value that cannot be uppercased, it is returned unchanged.

Tip: You can find more information on the char data type to explain this optimization.

CharROT13
Optimized ways to lowercase characters: char.ToLower: 10704 ms (Framework) ToLowerIf: 1231 ms (If-statement) ToLowerFast: 563 ms (Lookup table)
A summary. Using char.ToLower is slower than the method that tests each character with an if-statement. The method that uses a lookup table is somewhat faster still.
In most deployed C# applications, these timings are insignificant. But this optimization is sometimes useful. For methods that act upon characters, it can provide a speedup.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls