
How can the ToLower method on the String type be made more efficient? If you have ASCII strings, you can reduce the ToLower operation to an arithmetic expression performed on an array. We explore an optimization on string instances for lowercase transformation.
In ASCII, every uppercase letter can be converted to lowercase by adding 32. We exploit this to perform a lowercase transformation on each character in a string. This is faster than the ToLower method because it does not handle Unicode or other special cases.
This C# example program contains an optimized version of the ToLower method.
ToLowerFast extension method [C#]
static class Ext
{
/// <summary>
/// Lowercase string.
/// </summary>
public static string ToLowerFast(this string value)
{
char[] output = value.ToCharArray();
for (int i = 0; i < output.Length; i++)
{
if (output[i] >= 'A' &&
output[i] <= 'Z')
{
output[i] = (char)(output[i] + 32);
}
}
return new string(output);
}
}
Benchmark harness [C#]
using System;
using System.Diagnostics;
class Program
{
const int _max = 1000000;
static void Main()
{
string value = "This is a REGULAR sentence.";
Console.WriteLine(value.ToLower() == value.ToLowerFast());
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
string test = value.ToLower();
}
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
string test = value.ToLowerFast();
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.Read();
}
}
Output
True
176.16 ns
129.94 nsResults. The ToLowerFast method is about 30-40 percent faster than ToLower in this example program. In my testing on strings between 1 and 1000 characters, ToLowerFast was consistently faster.
Typo correction: The original version of this article had a typo in the extension method. Sean McKay wrote in with the correction.

There are other approaches to optimizing ToLower depending on the nature of your program's data. If repeated data is continually lowercased, using a Dictionary cache would be effective. If data that is already lowercased is frequently encountered, using an IsLower method would be most effective.
String IsUpper and IsLower MethodsLookup tables. Another effective way you can lowercase characters is with lookup tables. This is more efficient than the if-expression shown in this article, but will fail on certain inputs. If your data is always in ASCII, a lookup table would be best.
Char Lookup TableUppercase. An uppercase method could be constructed very easily. You would need to change the if-expression and then subtract 32 instead of add 32.
It can be argued that lowercasing strings is not something that should be focused on by computer programmers. However, thinking about small issues like this helps us gain proficiency in a complex system and also demonstrates something about the nature of computation: there are many ways to compute something, but which ways are the best? And how can we decide this?
String Type